fat-code-refactoring-techniques icon indicating copy to clipboard operation
fat-code-refactoring-techniques copied to clipboard

Earlier Draft: Single purpose controller

Open justin808 opened this issue 11 years ago • 3 comments

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 MicropostController into the MicropostCreateController to 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, ProfanityChecker to handle logic for a potentially complex validation.
  • Demonstrates simple tests of this ProfanityChecker class.

Review on Reviewable

justin808 avatar Apr 09 '14 09:04 justin808

Might be worth taking a look at focused_controller gem.

gylaz avatar Apr 12 '14 18:04 gylaz

@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.

justin808 avatar Apr 12 '14 18:04 justin808

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.

gylaz avatar Apr 12 '14 18:04 gylaz