Rails: Refactoring, views, helpers: how does it all go together? Rails: Refactoring, views, helpers: how does it all go together? ruby-on-rails ruby-on-rails

Rails: Refactoring, views, helpers: how does it all go together?


Refactoring makes your views easier to maintain. The problem is choosing where the refactored code goes.

Your two choices are partials and helpers. There's no stone-set rules dictating which should be used where. There are a couple of guidelines floating around like the one stating that helpers should not contain HTML.

Generally partials are better suited for refactoring sections that are more HTML/ERB/HAML than ruby. Helpers on the other hand are used for chunks of ruby code with minimal HTML or generating simple HTML from parameters.

However, I don't agree with the sentiment that helpers should contain no HTML at all. A little is ok, just don't over do it. The way helpers are processed hinder their use for producing large amounts of HTML. Which is why it's suggested that your helpers contain minimal amounts of HTML. If you look at the source the helpers that ship with rails you will notice that most of them generate html. The few that don't, are mainly used to generate parameters and evaluate common conditions.

For example, any of the form helpers or link_to variants fit the first form of helpers. While things like url_for and logged_in? as supplied by various authentication models are of the second kind.

This is the decision chain I use to determine whether to factor code from a view into a partial or helper.

  1. Repeating or nearly identical statements producing a single shallow html tag? => helper.
  2. Common expression used as an argument for another helper? => helper.
  3. Long expression (more than 4 terms) used as an argument for another helper? => helper.
  4. 4 or more lines of ruby (that is not evaluated into HTML)? => helper.
  5. Pretty much everything else => partial.

I'm going to use the code you're looking to refactor as an example:

I would refactor the view in the question this way:

app/helpers/beast_helper.rb:

def beast_action(beast)  if beast.dead?    link_to "bury", bury_beast_path(beast)  else    link_to "kill!", kill_beast_path(beast)  endend

app/views/beasts/_beast.html.erb:

<%= beast.body %><%= beast_action(beast) %>

app/views/beasts/index.html.erb:

<%= render :partial => "beast", :collection => @beasts %>

It's technically more complicated, because it's 3 files, and 10 lines total as opposed to 1 file and 10 lines. The views are now only 3 lines combined spread over 2 files. The end result is your code is much more DRY. Allowing you to reuse parts or all of it in other controllers/actions/views with minimal added complexity.

As for your body tag id. You should really be using content_for/yield. For that kind of thing.

app/views/layouts/application.html.erb

...<body id="<%= yield(:body_id) %>">...

app/views/beasts/index.html.erb

<% content_for :body_id, controller_action %>...

This will allow you to override the id of the body in any view that requires it. Eg:

app/views/users/preferences.html.erb

<% content_for :body_id, "my_preferences" %>


The first thing I'd do would be this:

#index.html.erb<%= render @beasts %>#_beast.html.erb<%= beast.body %><%= link_to_next_beast_action(beast) %>    #beast_helper.rbdef link_to_next_beast_action(beast)  if beast.dead?    link_to "bury", bury_beast_path( :id => beast.id )   else    link_to "kill!", kill_beast_path( :id => beast.id )  endend

What I've done is separate out the rendering of the beast into a partial which uses collection semantics.

Then I've moved the logic for showing the kill/bury links into a beast helper. This way if you decide to add another action (for example, 'bring back from dead'), you'll only have to change your helper.

Does this help?


A third choice is to use a view model from the Cells gem. This is a very popular framework that brings object-orientation to the view layer in Rails.

# app/cells/beast/cell.rbclass Beast::Cell < Cell::Concept  def show    return dead if model.dead?    kill  endprivate  def dead    link_to "bury", bury_beast_path( :id => model.id )     # you could render a view here, too!  end  def kill    link_to "kill!", kill_beast_path( :id => model.id )  endend

You then render a view model using a helper (in the view or controller).

# app/views/beasts/index.erb<%= concept(:beast, @beast).call %><%-# this returns the link content %>

That's all! You can test this cell isolated in a separate test. Cells also give you view rendering, view inheritance and many more things.

As an example, you could use a view for the kill link.

# app/cells/beast/cell.rbclass Beast::Cell < Cell::Concept  # ..  def kill    render :kill  endend

This renders the cell's killer view.

# app/cells/beast/views/index.erb<%= link_to "kill!", kill_beast_path( :id => model.id ) %>

Note the location of the view, it's nicely packaged into the cell directory.

And, yes, cells can do HAML and any other template engine supported by AbstractController.