Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a refactored version. I've made the following changes:</p> <ul> <li>pulled out the status logic into user#progress_status and refactored it to make it more reusable and to cleanup the view</li> <li>used for loop in the view since it is more readable</li> <li>renamed user#add_hours to total_hours so it doesn't imply addition (command) but just a virtual attribute (which is a query)</li> <li>removed the order scope in the method since you don't need one this case</li> <li>made the total_hours cache its result on the user, so multiple subsequent calls don't hit the DB multiple times (as we do inside progress status)</li> </ul> <p>First the view. I've included only the content inside tbody:</p> <pre><code>&lt;% for user in @users %&gt; &lt;tr class="&lt;%= user.progress_status.to_s %&gt;"&gt; &lt;td&gt;&lt;%= user.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= user.email %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= user.total_hours %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= user.roles.first and user.roles.first.name.titleize %&gt;&lt;/td&gt; &lt;td&gt; &lt;a data-toggle="modal" href="#role-options-&lt;%= user.id %&gt;" class="btn btn-mini" type="button"&gt;Change role&lt;/a&gt; &lt;%= render user %&gt; &lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Timesheet', user_timesheets_path(user, @timesheets), class: "btn btn-mini" %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre> <p>And the refactored user model methods:</p> <pre><code>def total_hours @total_hours ||= timesheets .where('day BETWEEN ? AND ?', Date.today.beginning_of_week, Date.today.end_of_week) .sum {|p| p.teacher + p.conversation + p.study} end def progress_status if has_role? :staff if total_hours &gt;= 10 :success elsif (7..9).include? total_hours :warning else :error end elsif has_role? :new_staff if total_hours &gt;= 15 :success elsif (12..14).include? total_hours :warning else :error end end end </code></pre> <p>You could go a bit further with the last method (when more context provided), but it should be OK for now.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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