From da891cc60716246d40d5a8e0164d5fa46905e029 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Fri, 16 Jan 2009 16:34:38 -0800 Subject: [PATCH] Refactored Rate#for_user_project_and_date to allow nil projects. #1920 --- app/models/rate.rb | 7 ++++++- spec/models/rate_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/models/rate.rb b/app/models/rate.rb index 84f6cb0..ec9d5e3 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -34,14 +34,19 @@ class Rate < ActiveRecord::Base 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, + project_id, date ]) end + + def self.for_user_and_date(user, date) + self.for_user_project_and_date(user, nil, date) + end end diff --git a/spec/models/rate_spec.rb b/spec/models/rate_spec.rb index 7e060ad..670462f 100644 --- a/spec/models/rate_spec.rb +++ b/spec/models/rate_spec.rb @@ -178,6 +178,18 @@ describe Rate, 'for' do Rate.for(@user, @project).should eql(@rate.amount) end end + + describe 'with a user' do + it 'should find all the rates without a project for a user on the project before today' do + Rate.should_receive(:for_user_project_and_date).with(@user, nil, Date.today.to_s).and_return(@rate) + Rate.for(@user) + end + + it 'should return the value of the most recent rate found' do + Rate.should_receive(:for_user_project_and_date).with(@user, nil, Date.today.to_s).and_return(@rate) + Rate.for(@user).should eql(@rate.amount) + end + end end describe Rate, 'for_user_project_and_date (private)' do @@ -212,4 +224,16 @@ describe Rate, 'for_user_project_and_date (private)' do }).and_return(@rate1) Rate.send(:for_user_project_and_date, @user, @project, @date).should eql(@rate1) end + + 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 <= ?", + @user.id, + nil, + @date + ], + :order => 'date_in_effect DESC' + }).and_return(@rate1) + Rate.send(:for_user_project_and_date, @user, nil, @date).should eql(@rate1) + end end