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   <%= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道我是否想费力地完成这个任务,但我会让你开始的。
可以重构为这样的东西:
像这样的简单事情:
与:
也有帮助。正如我在第一个示例中所做的那样,将空注释逻辑粘贴到辅助方法中。
实际上,这些都不会有助于缩短渲染时间。重构视图逻辑主要是为了您自己和任何必须阅读您的代码的人的利益,并且不太可能对速度产生任何影响。
I don't know if I want to wade my way through that but I'll get you started.
could be refactored to something like this:
Simple things like this:
is the same as:
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.