Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Refactoring Block in Views - can I write this more efficiently?
    primarykey
    data
    text
    <p>I'm trying to setup logic for my tables in rails. Basically its a timesheet application and what I want to do is set it up show it shows green if you hit your goal. Yellow if you are almost there. And red if you aren't even close. The code I have below works. However looking at my logs, it seems horribly inefficient. How can I write this better?</p> <p>Here is the view. users/hours.html.erb</p> <pre><code> &lt;div class="page-header"&gt;&lt;h1&gt;Weekly Timesheets&lt;/h1&gt;&lt;/div&gt; &lt;table class="nicetable table-condensed"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Email&lt;/th&gt; &lt;th&gt;Total Hours&lt;/th&gt; &lt;th&gt;Role&lt;/th&gt; &lt;th&gt;Change Role&lt;/th&gt; &lt;th&gt;Timesheet&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;% @users.each do |user| %&gt; &lt;tr class="&lt;%= 'success' if user.has_role? :staff and user.add_hours &gt;= 10 %&gt;&lt;%= 'warning' if user.has_role? :staff and user.add_hours &lt; 10 and user.add_hours &gt; 6 %&gt;&lt;%= 'error' if user.has_role? :staff and user.add_hours &gt;= 0 and user.add_hours &lt;= 6 %&gt;&lt;%= 'success' if user.has_role? :new_staff and user.add_hours &gt;= 15 %&gt;&lt;%= 'warning' if user.has_role? :new_staff and user.add_hours &lt; 15 and user.add_hours &gt;= 12 %&gt;&lt;%= 'error' if user.has_role? :new_staff and user.add_hours &lt; 12 and user.add_hours &gt;= 0 %&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.add_hours %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= user.roles.first.name.titleize unless user.roles.first.nil? %&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;/tr&gt; </code></pre> <p>And here is my Users Model.</p> <pre><code> def add_hours self.timesheets.where('day BETWEEN ? AND ?', Date.today.beginning_of_week, Date.today.end_of_week).order('created_at DESC').sum{|p| p.teacher + p.conversation + p.study} end </code></pre>
    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.
 

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