drier total weight calculations in Activity model; display total weight per activity on today's workout page
- M exercisetracker/trunk/app/models/activity.rb view
- M exercisetracker/trunk/app/views/workouts/today.html.erb view
| 32 | 33 | |
|---|---|---|
| 1 | 1 | class Activity < ActiveRecord::Base |
| 2 | 2 | has_many :exercises, :order => 'created_at desc', :dependent => :destroy |
| 3 | 3 | |
| 4 | def total_weight_from_previous_day | |
| 5 | prev = self.previous_active_date | |
| 6 | sets = exercises.find(:all, :conditions => ["created_at >= ? AND created_at <= ?", prev.beginning_of_day, prev.end_of_day]) | |
| 7 | sets.collect(&:total_weight).sum | |
| 8 | end | |
| 9 | ||
| 10 | 4 | def last_active_date |
| 11 | 5 | exercises.find(:first).created_at.to_date |
| 12 | 6 | end |
| --- | --- | |
| 14 | 8 | def previous_active_date |
| 15 | 9 | previous_date = exercises.find(:first, :conditions => ["created_at < ?", last_active_date.beginning_of_day]).created_at.to_date |
| 16 | 10 | end |
| 11 | ||
| 12 | def total_weight_on(date) | |
| 13 | sets = exercises.find(:all, :conditions => ["created_at >= ? AND created_at <= ?", date.beginning_of_day, date.end_of_day]) | |
| 14 | sets.collect(&:total_weight).sum | |
| 15 | end | |
| 17 | 16 | end |
| 32 | 33 | |
|---|---|---|
| 8 | 8 | <% if cur_activity != exercise.activity.name %> |
| 9 | 9 | <tr><td colspan="4" style="padding-top: 1em"> |
| 10 | 10 | <strong><%= link_to((cur_activity = exercise.activity.name), activity_path(exercise.activity)) %></strong> |
| 11 | <small>(<%= exercise.activity.previous_active_date.to_s(:short) %>: <%= exercise.activity.total_weight_from_previous_day %> lbs.)</small> | |
| 11 | <small>(today: <strong><%= exercise.activity.total_weight_on(Date.today) %> lbs.</strong>, | |
| 12 | <%= exercise.activity.previous_active_date.to_s(:short) %>: <strong><%= exercise.activity.total_weight_on(exercise.activity.previous_active_date) %> lbs.</strong>) | |
| 13 | </small> | |
| 12 | 14 | </td></tr> |
| 13 | 15 | <% end %> |
| 14 | 16 | |
