2
\$\begingroup\$
class Submission < ActiveRecord::Base

  def get_status_css()
    if self.status == IN_PROGRESS
      "info"
    elsif self.status == FAILED
      "error"
    elsif self.status == COMPLETED
      "success"
    else 
      "warning"
    end
  end

end

Submission is a model class and it has a method that returns css variables based on its status. Is this correct in the MVC architecture? I was thinking that since get_status_css is a method that is related to view, it shouldn't belong to a model class. Perhaps it should be put in the controller?

\$\endgroup\$

2 Answers 2

7
\$\begingroup\$

This could should not be in either the model or the view. Rather is should be contained in the submission_helper, or if you really wanted to look into making it less procedural and more OO, then it should be handled by a presenter, a pattern in rails that steps in between the model and the view.

Jeff Casimir has developed a presenter for RoR called Draper (https://github.com/jcasimir/draper) which is definetly worth checking out, but in the mean time, the proper place for this is a helper method.

Its best to avoid logic in your view code, as its likely to change, and be distributed amongst many files, making it hard to maintain. Putting it in a helper at least allows for a single source of that knowledge, so that if it has to change in the future it is easy to update.

Hope this helps.

\$\endgroup\$
1
\$\begingroup\$

It actually should be put in the view. I don't know how RoR implements it, but the submission status should be retrieved by the controller and transferred to your view. The view will then decide which CSS class to apply.

You could use a substitution test to know whether it should belong to the view or not. If you were to substitute your current view by an entirely different one (eg. an iPhone app), would get_status_css belong in your model? I guess not. This is not too important when you only have one view or if your views are all web applications, but can help separate concerns.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.