avatar

46

docs

by chrisk, 25 Jun, 2007 09:55 AM
25 46  
11 # Filters added to this controller apply to all controllers in the application.
22 # Likewise, all the methods added will be available for all controllers.
3 
43 class ApplicationController < ActionController::Base
54   helper :all # include all helpers, all the time
65 end
76 
7 
8 
9 
10 # Extend built-in arrays with some useful math functions.
811 class Array
12   # Extends Array. The sum works using the + operator, so it should works for objects like strings, too.
913   def sum
1014     inject(0) { |sum, x| sum + x }
1115   end
16   
17   # Extends Array. This relies on the sum method and Array#size. If the array is empty, returns 0.
1218   def mean
1319     (size > 0) ? sum.to_f / size : 0
1420   end
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.
13 class Exercise < ActiveRecord::Base
24   belongs_to :activity
35   
------
1012   end
1113 
1214   
15   # Accessor to retrieve created_at in a human-readable format.
1316   def created_at_string
1417     created_at.to_s(:long)
1518   rescue ArgumentError
1619     Time.now.to_s(:long)
1720   end
1821   
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.
1924   def created_at_string=(value)
2025     self.created_at = Time.parse(value)
2126   rescue ArgumentError
2227     @created_at_invalid = true
2328   end
2429   
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.
2932   def total_weight
3033     if activity.use_reps? && activity.use_weight?
3134       reps * weight * ((activity.double_weight?) ? 2 : 1)
------
3336       0
3437     end
3538   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   
3649 end