diff --git a/app/models/rate.rb b/app/models/rate.rb index bbe0347..71256d2 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -6,6 +6,8 @@ class Rate < ActiveRecord::Base validates_presence_of :user_id validates_presence_of :date_in_effect + before_save :unlocked? + named_scope :history_for_user, lambda { |user| { :conditions => { :user_id => user.id }, @@ -16,4 +18,8 @@ class Rate < ActiveRecord::Base def locked? return self.time_entries.length > 0 end + + def unlocked? + return !self.locked? + end end diff --git a/spec/models/rate_spec.rb b/spec/models/rate_spec.rb index 6601601..4698d94 100644 --- a/spec/models/rate_spec.rb +++ b/spec/models/rate_spec.rb @@ -70,3 +70,33 @@ describe Rate, 'locked?' do end end + +describe Rate, 'locked?' do + it 'should be false if a Time Entry is associated' do + rate = Rate.new + rate.time_entries << mock_model(TimeEntry) + rate.unlocked?.should be_false + end + + it 'should be true if no Time Entries are associated' do + rate = Rate.new + rate.unlocked?.should be_true + end + +end + +describe Rate, 'save' do + include RateSpecHelper + + it 'should save normally if a Rate is not locked' do + rate = Rate.new(rate_valid_attributes) + rate.stub!(:locked?).and_return(false) + rate.save.should eql(true) + end + + it 'should not save if a Rate is locked' do + rate = Rate.new(rate_valid_attributes) + rate.stub!(:locked?).and_return(true) + rate.save.should eql(false) + end +end