From 0e8bd2153c4937f25039192f361153df48279b0c Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Fri, 16 Jan 2009 15:33:42 -0800 Subject: [PATCH] Prevent Rates from being destroyed if they are locked. #1919 --- app/models/rate.rb | 1 + spec/models/rate_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/rate.rb b/app/models/rate.rb index 71256d2..a0d2f3e 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -7,6 +7,7 @@ class Rate < ActiveRecord::Base validates_presence_of :date_in_effect before_save :unlocked? + before_destroy :unlocked? named_scope :history_for_user, lambda { |user| { diff --git a/spec/models/rate_spec.rb b/spec/models/rate_spec.rb index 4698d94..859bd5b 100644 --- a/spec/models/rate_spec.rb +++ b/spec/models/rate_spec.rb @@ -100,3 +100,24 @@ describe Rate, 'save' do rate.save.should eql(false) end end + +describe Rate, 'destroy' do + include RateSpecHelper + + it 'should destroy the Rate if it is not locked' do + rate = Rate.create(rate_valid_attributes) + rate.stub!(:locked?).and_return(false) + proc { + rate.destroy + }.should change(Rate, :count).by(-1) + + end + + it 'should not delete the Rate if it is locked' do + rate = Rate.create(rate_valid_attributes) + rate.stub!(:locked?).and_return(true) + proc { + rate.destroy + }.should_not change(Rate, :count) + end +end