fat-code-refactoring-techniques
fat-code-refactoring-techniques copied to clipboard
Earlier Draft: Single purpose controller
Single Purpose Controller
This is a preferable alternative to pull request https://github.com/justin808/fat-code-refactoring-techniques/pull/6, which uses a Service Object pattern.
Objectives:
- Splitting the
MicropostControllerinto theMicropostCreateControllerto so that a complicated controller method can be refactored to have many private methods that relate to the only public method, on the "single purpose controller". I.e. conform to the Sandi rules of small methods. - Demonstrate changing the routing for a custom named controller.
- Demonstrate a controller spec that covers everything the service object spec covered, but covers it more accurately because it actually verifies that the controller is doing the right thing.
- Demonstrate following features of controller spec testing in rspec:
- testing flash.now
- checking instance variable
- checking rendered template
- Demonstrate using a model object,
ProfanityCheckerto handle logic for a potentially complex validation. - Demonstrates simple tests of this ProfanityChecker class.
Might be worth taking a look at focused_controller gem.
@gylaz I've seen it, but have not tried it. Any experience with it on real projects? Any distinct advantage over basic rails constructs of distinct controllers and using concerns for the common elements.
I have not used it (or know of anyone that has). I remember reading about it a while back, and your proposal stirred that memory. Here's a blog post from the creator of that gem.