views:

36

answers:

1

How would you refactor this logic filled partial?

<%- for post in @posts -%>
  <div class="post">
    <%= link_to post.title, post %>

    <%- if post.name.empty? -%>

    <%- else -%>   
      <span class="name">
        by 
        <%- if post.email.blank? -%>
          <%= post.name %>
        <%- else -%>
          <a href="mailto:<%= post.email %>"><%= post.name %></a>
        <%- end -%>
      </span>
    <%- end -%>

    <span class="time">
      active &#32; <%= timeago(post.updated_at) %>
    </span>

    <%- if post.comments.empty? -%>
      <span class="reply">
        <%= link_to 'reply', :controller => 'posts', :action => 'show', :id => post %>
      </span>
    <% else %>
      <span class="reply">
        <%= link_to pluralize(post.comments.count, 'reply'), :controller => 'posts', :action => 'show', :id => post %>
      </span>
    <%- end -%>

    <p><%= sanitize post.content,
 :tags => %w(a embed img object p param),
 :attributes => %w(allowfullscreen allowscriptaccess href name src type value) %></p>

    <%- unless controller.controller_name == "tags" -%>
      <%- unless post.tag_list.nil? || post.tag_list.empty? -%>
        <%- post.tags.each do |t| -%>
          <div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
        <%- end -%>
      <%- end -%>
    <%- end -%>

    <%- unless post.comments.empty? -%>
      <div class="comments">
        <%= render :partial => post.firstcomments %>
        <%- if post.comments.count >= 4 -%>
          <%= link_to 'more...', :action => 'show', :id => message %>
        <%- end -%>
      </div>
    <%- end -%>
  </div>
<%- end -%>

Notes: post.firstcomments just returns the 3 latest posts. Using Rails 3 and Ruby 1.9.2. I haven't looked into the sanitize portion of the code, and I realize Rails 3 escapes everything by default, so it can safely be ignored for now unless someone knows how to convert it.

My models are clean and my controllers are decent but this partial is awful. It does what it needs to do but it takes up most of the rendering time when refreshing the page. Comments, suggestions and code are much appreciated. Thank you for reading my question.

A: 

I don't know if I want to wade my way through that but I'll get you started.

<%- if post.name.empty? -%>
<%- else -%>   
  <span class="name">
    by 
    <%- if post.email.blank? -%>
      <%= post.name %>
    <%- else -%>
      <a href="mailto:<%= post.email %>"><%= post.name %></a>
    <%- end -%>
  </span>
<%- end -%>

could be refactored to something like this:

<%= by_post_name post %>

#post_helper.rb
def by_post_name post
  post.name.empty? ? "" : "<span>#{post.email.blank? ? post.name : mail_to post.email, post.name}</span>"
end

Simple things like this:

<%- if post.comments.empty? -%>
  <span class="reply">
    <%= link_to 'reply', :controller => 'posts', :action => 'show', :id => post %>
  </span>
<% else %>
  <span class="reply">
    <%= link_to pluralize(post.comments.count, 'reply'), :controller => 'posts', :action => 'show', :id => post %>
  </span>
<%- end -%>

is the same as:

<span class="reply">
  <%- if post.comments.empty? -%>
    <%= link_to 'reply', :controller => 'posts', :action => 'show', :id => post %>
  <% else %>
    <%= link_to pluralize(post.comments.count, 'reply'), :controller => 'posts', :action => 'show', :id => post %>   
  <%- end -%>
</span>

also help. Stick your empty comment logic in a helper method as I did in my first example.

Really none of this is going to help with the rendering time. Refactoring view logic is primarily for the benefit of yourself and anyone who has to read your code and is unlikely to make any difference in speed.

mark