add validations
- M exercisetracker/trunk/app/models/activity.rb view
- M exercisetracker/trunk/app/models/exercise.rb view
| 33 | 39 | |
|---|---|---|
| 1 | 1 | class Activity < ActiveRecord::Base |
| 2 | 2 | has_many :exercises, :order => 'created_at desc', :dependent => :destroy |
| 3 | 3 | |
| 4 | ||
| 5 | validates_presence_of :name, :noun | |
| 6 | ||
| 7 | validates_inclusion_of :use_reps, :use_weight, :use_distance, :use_duration, :double_weight, | |
| 8 | :in => [true, false], :allow_nil => false | |
| 9 | ||
| 10 | ||
| 4 | 11 | def last_active_date |
| 5 | 12 | exercises.find(:first).created_at.to_date |
| 6 | 13 | end |
| 27 | 39 | |
|---|---|---|
| 1 | 1 | class Exercise < ActiveRecord::Base |
| 2 | 2 | belongs_to :activity |
| 3 | 3 | |
| 4 | validates_numericality_of :reps, :only_integer => true, :allow_nil => false | |
| 5 | validates_numericality_of :weight, :distance, :duration, :allow_nil => true | |
| 6 | validates_presence_of :activity_id | |
| 7 | ||
| 8 | %w[reps weight distance duration].each do |property| | |
| 9 | validates_presence_of property.intern, :if => Proc.new { |exercise| exercise.activity.send("use_#{property}?") } | |
| 10 | end | |
| 11 | ||
| 12 | ||
| 4 | 13 | def created_at_string |
| 5 | 14 | created_at.to_s(:long) |
| 6 | 15 | rescue ArgumentError |
