|
|
| 39 |
46 |
|
| 1 | # An exercise is the instantiation of an activity. We might otherwise call this a "set", |
| 2 | # except that this is a reserved word in Rails. A workout consists of multiple exercises/sets. |
| 1 | 3 | class Exercise < ActiveRecord::Base |
| 2 | 4 | belongs_to :activity |
| 3 | 5 | |
| --- | --- | |
| 10 | 12 | end |
| 11 | 13 | |
| 12 | 14 | |
| 15 | # Accessor to retrieve created_at in a human-readable format. |
| 13 | 16 | def created_at_string |
| 14 | 17 | created_at.to_s(:long) |
| 15 | 18 | rescue ArgumentError |
| 16 | 19 | Time.now.to_s(:long) |
| 17 | 20 | end |
| 18 | 21 | |
| 22 | # Accessor to set created_at using a human-readable string, entered by the user. This falls back |
| 23 | # on <tt>Time.parse</tt>. If <tt>Time.parse</tt> cannot convert the string to a Time object, validation fails. |
| 19 | 24 | def created_at_string=(value) |
| 20 | 25 | self.created_at = Time.parse(value) |
| 21 | 26 | rescue ArgumentError |
| 22 | 27 | @created_at_invalid = true |
| 23 | 28 | end |
| 24 | 29 | |
| 25 | | def validate |
| 26 | | errors.add(:created_at, 'is invalid') if @created_at_invalid |
| 27 | | end |
| 28 | | |
| 30 | # Calculate the total weight lifted during this exercise, for exercises that |
| 31 | # use reps and weight. If the exercise does not fit this description, it returns 0. |
| 29 | 32 | def total_weight |
| 30 | 33 | if activity.use_reps? && activity.use_weight? |
| 31 | 34 | reps * weight * ((activity.double_weight?) ? 2 : 1) |
| --- | --- | |
| 33 | 36 | 0 |
| 34 | 37 | end |
| 35 | 38 | end |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | protected |
| 44 | |
| 45 | def validate #:nodoc: |
| 46 | errors.add(:created_at, 'is invalid') if @created_at_invalid |
| 47 | end |
| 48 | |
| 36 | 49 | end |