Rails 部分重载逻辑需要重构

发布于 2024-09-16 13:17:02 字数 2195 浏览 4 评论 0原文

您将如何重构这个逻辑填充部分?

<%- 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 默认情况下会转义所有内容,因此现在可以安全地忽略它,除非有人知道如何转换它。

我的模型很干净,我的控制器也不错,但这部分很糟糕。它做了它需要做的事情,但是刷新页面时它占用了大部分渲染时间。非常感谢评论、建议和代码。感谢您阅读我的问题。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独难免 2024-09-23 13:17:02

我不知道我是否想费力地完成这个任务,但我会让你开始的。

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

也有帮助。正如我在第一个示例中所做的那样,将空注释逻辑粘贴到辅助方法中。

实际上,这些都不会有助于缩短渲染时间。重构视图逻辑主要是为了您自己和任何必须阅读您的代码的人的利益,并且不太可能对速度产生任何影响。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文