I’ve always been annoyed at the lack of maintainability that comes with
using multiple resource controllers in my Rails apps. Each generated
resource controller clocks in at 85 lines, and most of mine only differ
from each other by a line or two - an added before_filter or a change
in the url that the users is redirected to after the creation of a new
Widget. Not very
DRY. When coming
back to each one of these controllers to add or adjust features, it
takes me entirely too much time to sift through the stock 85 lines and
find my application-specific behavior.
Enter resource_this
resource_this
aims to solve this maintainability problem by making your stock resource
controllers look like this:
Behind the scenes, this code is generated:
Nested resources like so:
This generates a very similar controller to the one above with adjusted
redirects and one additional before_filter / loader method pair to grab
the parent resource. In this case:
The separation of logic - DB operations in before_filters, rendering in
the standard resource controller methods - makes this approach
ridiculously easy to customize. Need to load an additional object for
the :show action? Slap another before_filter on it. Need to change
the path that the :update action redirects to? Override the :update
action with your new rendering behavior. And this customized behavior
sticks out like a sore thumb - making it infinitely easier to maintain.
Oh, there’s also a generator:
This works just like the resource generator, with the addition of the
resource_this line to your controller and a functional test. No views
are generated, so the test focuses on the XML behavior of this
controller.