2010-08-29 27 views
0

你會如何重構這個邏輯填充部分?Rails部分超載邏輯需要重構

<%- 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 -%> 

注: post.firstcomments僅返回3個最新的帖子。 使用Rails 3和Ruby 1.9.2。 我還沒有看過代碼的清理部分,我意識到Rails 3默認情況下會將所有內容都轉義出來,所以現在可以安全地忽略它,除非有人知道如何轉換它。

我的模型很乾淨,我的控制器很體面,但這部分很糟糕。它做它需要做的事情,但它刷新頁面時佔用大部分渲染時間。意見,建議和代碼非常感謝。感謝您閱讀我的問題。

回答

1

我不知道我是否想通過這種方式來迎接,但我會讓你開始。

<%- 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 -%> 

可重構爲這樣的事:

<%= 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 

簡單的事情是這樣的:

<%- 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 -%> 

是一樣的:

<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> 

也有幫助。像我在第一個例子中那樣,將你的空註釋邏輯放在輔助方法中。

真的沒有這會幫助渲染時間。重構視圖邏輯主要是爲了您自己的利益以及任何必須閱讀您的代碼並且不可能在速度上產生任何影響的人。