Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My attempt:</p> <pre><code>def punctuality_report @date = params[:date] ? Date.strptime(params[:date], "%m/%d/%Y") : Date.today @through = params[:through] ? Date.strptime(params[:through], "%m/%d/%Y") : @date + 1 time_range = @date.to_formatted_s(:db)..@through.to_formatted_s(:db) @orders = Order.daily.includes(:store).where('orders.created_at' =&gt; time_range).group_by{|o| o.store} @orders.each_key.each do |s| all_early = @orders[s].collect{|o| o if o.early?}.compact all_late = @orders[s].collect{|o| o if o.late?}.compact self.instance_variable_set("@s#{s.id}_early", all_early) self.instance_variable_set("@s#{s.id}_avg_early", all_early.count &gt; 0 ? all_early.collect{|o| o.earliness}.sum / all_early.count : 0) self.instance_variable_set("@s#{s.id}_late", all_late) self.instance_variable_set("@s#{s.id}_avg_late", all_late.count &gt; 0 ? all_late.collect{|o| o.lateness}.sum / all_late.count : 0) self.instance_variable_set("@s#{s.id}_on_time", @orders[s] - (all_early | all_late) ) end end </code></pre> <p>In short: in the <code>eval</code> section only instance variables were added, and we can use <code>instance_variable_get</code> and <code>instance_variable_set</code> instead. Almost all occurences of <code>eval</code> can be avoided, but I also believe that <code>eval</code> sometimes can be extremely useful and very powerful. </p> <p>The code more clearly expresses its intent: it will add a set of instance variables, which is immediately visible on sight.</p> <p>I definitely do not consider the use of <code>group_by</code> to be a code smell.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload