Skip to content

Move code into model

Richard Huang edited this page Aug 15, 2010 · 3 revisions

Please go to http://rails-bestpractices.com/posts/25-move-code-into-model

Before:


<% if current_user && (current_user == @post.user ||
                       @post.editors.include?(current_user)) %>
  <%= link_to 'Edit this post', edit_post_url(@post) %>
<% end %>

After:


<% if @post.editable_by?(current_user)) %>
  <%= link_to 'Edit this post', edit_post_url(@post) %>
<% end %>

class Post < ActiveRecord::Base

  def editable_by?(user)
    user && (user == self.user || self.editors.include?(user))
  end

end