

Rails controllers and OOP
source link: https://jonleighton.name/2012/focused-controller/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rails controllers and OOP
20 April 2012
I have written a much better explanation, please read that instead: Explaining Focused Controller
Rails controllers violate the Single Responsibility Principle. Each “action” has a different responsibility and should be contained in a separate object. Controllers are also hard to unit test, so we end up using slow “functional tests”.
This realisation lead me to experiment with altering the conventions for controller code, and today I gave a talk on the topic at Railsberry called You Hate Your Codebase & It’s Your Fault.
I wrote a plugin named Focused Controller a while back, as part of this experiment, but have kept quiet about it until now, partly because it was undocumented.
But now I’m ready to announce it and I’m really keen for people to try it out and share their thoughts about this idea. I’ve gone into detail about the problems and solutions in the README, so I won’t repeat that here. Please just check it out!
Comments
I'd love to hear from you here instead of on corporate social media platforms! You can also contact me privately.
Alexey Muranov
I personally do not understand very well why controller actions need to be object methods and not simply namespaced functions (i know they would be object methods anyway). Is there other use for instance variables than to store the request parameters and memoize the values used in the views?
Wouldn't it be simpler to view each controller action as a function that gets a "request" object, does something to the models, and returns a "response" object, possibly first initializing some "helper" objects?
Update: i am wondering about a possibility of the following realization of MVC: model -- class, controller -- module, view -- collection of templates (or maybe a class).
julian7
Yep, FocusedController should be in Rails 4.0, keeping the old controller format as soon to be obsoleted.
Etienne Massip
Agreed, what about famous RoR magic?
I used to like the way V and C mixed together in RoR, it's also kind of the way how JSF works.
One class per action is overkill IMHO.
Alexey Muranov
Here is a situation i've encountered where the usual un-focused controllers look a bit unnatural. I have a nested resource controller, which is not going to have the `index` action because the main resource controller `show` action is going to show the nested resource index. I have an impression that my controllers overlap here. I also anticipate some code duplication.
Add your comment
Name:
Email (kept private):
Message (Markdown allowed):
Recommend
-
49
I’ve always viewed functional programming ( FP ) from afar,...
-
9
Cohesion and Coupling in OOPDecember 15th 2020 new story4
-
7
Inheritance and Abstract Class Pattern for Ruby on Rails Controllers Updated Jan 7, 2020 4 minute read
-
15
A categorical comparison between FP and OOP Jan 20, 2017 • Marco Perone Category theory is the branch of mathematics abstracting on the concept of
-
9
RAGANWALD.COM OOP, JavaScript, and so-called Classes Programming with objects and classes began in Norway in the late 1960s with the
-
7
RAGANWALD.COM When FP? And when OOP? Very roughly speaking, functional programming (“FP”) and object-oriented programming (“OOP”) have si...
-
7
Intermediate Rails: Understanding Models, Views and Controllers I’m glad people liked the introduction to Rails; now you scallawags get to...
-
4
How to write a Rails Plugin (for controllers) Posted: 2007-07-06 - Last updated: 2019-06-05 Tagged
-
6
Home Menu Rails...
-
4
Rails 7, Rodauth, BootRails, Nested Resources and Testing Controllers Jun 10, 2022 I'm working on a new project and, as I am front end challenged, I started by purchasin...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK