This post originated from an RSS feed registered with Ruby Buzz
by Jake Scruggs.
Original Post: I'm Thinking of Putting View Logic into a Model
Feed Title: Jake Scruggs
Feed URL: http://jakescruggs.blogspot.com/feeds/posts/default
Feed Description: Ruby, Rails, Metrics, Testing, and pursuit of clean code.
As I am clearly crazy, but hear me out. Say there's a Car object that uses single table inheritance and all the objects that descended from it are routed through the car method in the CarController. The CarController looks like this:
So I'm not very happy about this and ultimately I'd like to refactor the app to use different controllers for different objects (crazy huh?), but that is a major undertaking as the whole app assumes this architecture. It occurred to me the other day that there's an in-between step. I could do something like this:
1 2 3 4 5 6 7 8 9 10 11
classCar < ActiveRecord::Base deflayout 'two_column' end defstylesheets ['two_column_layout.css', 'two_column_theme.css'] end defscripts [] end end
And then the Ford class would look like this:
1 2 3 4 5 6 7 8 9 10 11
classFord < Car deflayout 'ford' end defstylesheets ['ford_layout.css', 'ford_theme.css'] end defscripts ['discount.js'] end end
So the CarController could then be shortened to this:
Any model that needs to overwrite the default layout, scripts, or stylesheets can.
So should I take this step and put view information in the models, or should I just hold out for the big refactor into separate controllers? Keep in mind the big refactor is so big that it may never happen.