Refactored Rate#for_user_project_and_date to allow nil projects. #1920

This commit is contained in:
Eric Davis
2009-01-16 16:34:38 -08:00
parent 4599e21dfb
commit da891cc607
2 changed files with 30 additions and 1 deletions
+6 -1
View File
@@ -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
+24
View File
@@ -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