Currently when I need to share a method like processParams(params)
between different controllers, I use either inheritance or services. Both solution has some inconvenients :
So my question is : is there any other way to use some common methods accessible for multiple controllers ?
One option I like is to write the common methods as a category, then mix it into the controllers as necessary. It gives a lot more flexibility than inheritance, has access to stuff like params, and the code is simple and understandable.
Here's a tiny example:
@Category(Object) class MyControllerCategory { def printParams() { println params } } @Mixin(MyControllerCategory) class SomethingController { def create = { printParams() ... } def save = { printParams() } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With