diff --git a/app/models/rate.rb b/app/models/rate.rb index 33a1e2f..85e80fc 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -35,23 +35,38 @@ class Rate < ActiveRecord::Base Rate.check_date_string(date) rate = self.for_user_project_and_date(user, project, date) - + # Check for a default (non-project) rate + rate = self.default_for_user_and_date(user, date) if rate.nil? && project + return nil if rate.nil? return rate.amount end private def self.for_user_project_and_date(user, project, date) - project_id = project.nil? ? nil : project.id - return Rate.find(:first, - :order => 'date_in_effect DESC', - :conditions => [ - "user_id IN (?) AND project_id IN (?) AND date_in_effect <= ?", - user.id, - project_id, - date - ]) - + if project.nil? + return Rate.find(:first, + :order => 'date_in_effect DESC', + :conditions => [ + "user_id IN (?) AND date_in_effect <= ?", + user.id, + date + ]) + + else + return Rate.find(:first, + :order => 'date_in_effect DESC', + :conditions => [ + "user_id IN (?) AND project_id IN (?) AND date_in_effect <= ?", + user.id, + project.id, + date + ]) + end + end + + def self.default_for_user_and_date(user, date) + self.for_user_project_and_date(user, nil, date) end # Checks a date string to make sure it is in format of +YYYY-MM-DD+, throwing diff --git a/spec/models/rate_for_spec.rb b/spec/models/rate_for_spec.rb new file mode 100644 index 0000000..5537eb7 --- /dev/null +++ b/spec/models/rate_for_spec.rb @@ -0,0 +1,32 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe Rate, 'for' do + before(:each) do + @user = User.new(:mail => 'metest@example.com', :lastname => 'Test', :firstname => 'Mr') + @user.login = 'mr-test' + @user.save! + end + + after(:each) do + User.destroy_all + end + + describe 'a user with no Rates' do + it 'should return nil' do + Rate.for(@user).should be_nil + end + end + + describe 'a user with one default Rate' do + it 'should return the Rate amount if the Rate is in effect' do + rate = Rate.create!({ :user_id => @user.id, :amount => 100.0, :date_in_effect => Date.today}) + Rate.for(@user).should eql(rate.amount) + end + + it 'should return nil if the Rate is not in effect yet' do + Rate.for(@user).should be_nil + end + + it 'should return the same default Rate on a proejct' + end +end diff --git a/spec/models/rate_spec.rb b/spec/models/rate_spec.rb index 11accf4..7a72c29 100644 --- a/spec/models/rate_spec.rb +++ b/spec/models/rate_spec.rb @@ -169,8 +169,15 @@ describe Rate, 'for' do Rate.for(@user, @project, @date).should eql(@rate.amount) end - it 'should return nil if no rate is found' do + it 'should check for a default rate if no rate is found' do Rate.should_receive(:for_user_project_and_date).with(@user, @project, @date).and_return(nil) + Rate.should_receive(:default_for_user_and_date).with(@user, @date).and_return(@rate) + Rate.for(@user, @project, @date).should eql(@rate.amount) + end + + it 'should return nil if no set or default rate is found' do + Rate.should_receive(:for_user_project_and_date).with(@user, @project, @date).and_return(nil) + Rate.should_receive(:default_for_user_and_date).with(@user, @date).and_return(nil) Rate.for(@user, @project, @date).should be_nil end end @@ -186,8 +193,9 @@ describe Rate, 'for' do Rate.for(@user, @project).should eql(@rate.amount) end - it 'should return nil if no rate is found' do + it 'should return nil if no set or default rate is found' do Rate.should_receive(:for_user_project_and_date).with(@user, @project, Date.today.to_s).and_return(nil) + Rate.should_receive(:default_for_user_and_date).with(@user, Date.today.to_s).and_return(nil) Rate.for(@user, @project).should be_nil end end @@ -203,7 +211,7 @@ describe Rate, 'for' do Rate.for(@user).should eql(@rate.amount) end - it 'should return nil if no rate is found' do + it 'should return nil if no set or default rate is found' do Rate.should_receive(:for_user_project_and_date).with(@user, nil, Date.today.to_s).and_return(nil) Rate.for(@user).should be_nil end @@ -279,9 +287,8 @@ describe Rate, 'for_user_project_and_date (private)' do it 'should search rates without a project when +project+ is nil' do Rate.should_receive(:find).with(:first, { - :conditions => ["user_id IN (?) AND project_id IN (?) AND date_in_effect <= ?", + :conditions => ["user_id IN (?) AND date_in_effect <= ?", @user.id, - nil, @date ], :order => 'date_in_effect DESC'