Stu*_*ssa 5 ruby refactoring ruby-on-rails
我有多ActiveRecord个子类实例数组Item,我需要根据最早的事件循环打印.在这种情况下,我需要打印打印付款和维护日期如下:
项目5天内需要维护
项目B 6天内
需要付款
项目7天内需要付款项目B维护需要8天
我目前有两个查询maintenance和payment项目查询(非独占查询)和以下输出它们:
<%- item_p = nil -%>
<%- item_m = nil -%>
<%- loop do -%>
<% item_p ||= @items_p.shift %>
<% item_m ||= @items_m.shift %>
<%- if item_p.nil? and item_m.nil? then break -%>
<%- elsif item_p and (item_m.nil? or item_p.paymt < item_m.maint) then -%>
<%= item_p.name %> payment required in ...
<%- elsif item_m and (item_p.nil? or item_m.maint < item_p.paymt) then -%>
<%= item_m.name %> maintenance required in ...
<%- end -%>
<%- end -%>
Run Code Online (Sandbox Code Playgroud)
有什么方法可以清除上面(丑陋)代码的可读性?
拥抱鸭子打字并确保你的对象是多态的.您希望您的付款项目与维护项目相当,以便对其进行排序.
所以,假设你有一个Payment和一个Maintenance类:
module Due
include Comparable
# Compare this object with another. Used for sorting.
def <=>(other)
self.due <=> other.due
end
end
class Payment < ActiveRecord::Base
include Due
alias_method :due, :payment
def action
"#{name} requires payment"
end
end
class Maintenance < ActiveRecord::Base
include Due
alias_method :due, :maintenance
def action
"#{name} requires maintenance"
end
end
Run Code Online (Sandbox Code Playgroud)
看看我们如何创建一个action,due并<=>在方法两个班?我们还注意包含Ruby内置模块Comparable.这允许我们执行以下操作:
# Assuming 'payment' and 'maintenance' are date fields...
a = Payment.new :payment => 3.days.from_now
b = Maintenance.new :maintenance => 2.days.from_now
[a, b].sort
#=> [b, a]
Run Code Online (Sandbox Code Playgroud)
然后视图变得如此简单:
<% (@payment_items + @maintenance_items).sort.each do |item| %>
<%= item.action %> in <%= distance_of_time_in_words_to_now(item.due) %><br/>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我确定我没有正确的实施细节,但我希望这能让您了解如何处理您的问题.
这是快速且肮脏的(即未优化):
# In your controller:
@items = @items_p.map{ |item| {:item => item, :days => item.paymt, :description => "payment"} }
@items += @items_m.map{ |item| {:item => item, :days => item.maint, :description => "maintenance"} }
@items = @items.sort_by{ |item| item[:day] }
# In your view:
<% @items.each do |item| %>
<%= item[:item].name %> <%= item[:description] %> required in <%= item[:days] %> days
<% end %>
Run Code Online (Sandbox Code Playgroud)