Changed the API so Rate#for will return a Rate object and Rate#amount_for

will return the rate amount.

  #1921
This commit is contained in:
Eric Davis
2009-01-20 15:26:01 -08:00
parent 8e89e69532
commit 8c5e58de04
3 changed files with 21 additions and 15 deletions
+6
View File
@@ -37,6 +37,12 @@ class Rate < ActiveRecord::Base
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
rate
end
# API to find the amount for a +user+ on a +project+ at a +date+
def self.amount_for(user, project = nil, date = Date.today.to_s)
rate = self.for(user, project, date)
return nil if rate.nil?
return rate.amount
+6 -6
View File
@@ -20,7 +20,7 @@ describe Rate, 'calculated for' do
describe 'a user with one default Rate' do
it 'should return the Rate if the Rate is effective today' do
rate = Rate.create!({ :user_id => @user.id, :amount => 100.0, :date_in_effect => Date.today})
Rate.for(@user).should eql(rate.amount)
Rate.for(@user).should eql(rate)
end
it 'should return nil if the Rate is not effective yet' do
@@ -30,7 +30,7 @@ describe Rate, 'calculated for' do
it 'should return the same default Rate on all projects' do
project = mock_model(Project)
rate = Rate.create!({ :user_id => @user.id, :amount => 100.0, :date_in_effect => Date.today})
Rate.for(@user, project).should eql(rate.amount)
Rate.for(@user, project).should eql(rate)
end
end
@@ -38,7 +38,7 @@ describe Rate, 'calculated for' do
it 'should return the newest Rate before the todays date' do
rate = Rate.create!({ :user_id => @user.id, :amount => 100.0, :date_in_effect => Date.yesterday})
rate2 = Rate.create!({ :user_id => @user.id, :amount => 300.0, :date_in_effect => Date.today})
Rate.for(@user).should eql(rate2.amount)
Rate.for(@user).should eql(rate2)
end
end
@@ -47,14 +47,14 @@ describe Rate, 'calculated for' do
project = mock_model(Project)
rate = Rate.create!({ :user_id => @user.id, :project => project, :amount => 100.0, :date_in_effect => Date.yesterday})
rate2 = Rate.create!({ :user_id => @user.id, :amount => 300.0, :date_in_effect => Date.today})
Rate.for(@user, project).should eql(rate.amount)
Rate.for(@user, project).should eql(rate)
end
it 'should return the default Rate if the project Rate isnt effective yet but the default Rate is' do
project = mock_model(Project)
rate = Rate.create!({ :user_id => @user.id, :project => project, :amount => 100.0, :date_in_effect => Date.tomorrow})
rate2 = Rate.create!({ :user_id => @user.id, :amount => 300.0, :date_in_effect => Date.today})
Rate.for(@user, project).should eql(rate2.amount)
Rate.for(@user, project).should eql(rate2)
end
it 'should return nil if neither Rate is effective yet' do
@@ -70,7 +70,7 @@ describe Rate, 'calculated for' do
project = mock_model(Project)
rate = Rate.create!({ :user_id => @user.id, :project => project, :amount => 100.0, :date_in_effect => Date.yesterday})
rate2 = Rate.create!({ :user_id => @user.id, :project => project, :amount => 300.0, :date_in_effect => Date.today})
Rate.for(@user, project).should eql(rate2.amount)
Rate.for(@user, project).should eql(rate2)
end
end
end
+9 -9
View File
@@ -148,9 +148,9 @@ describe Rate, 'for' do
end
describe 'returns' do
it 'a decimal when there is a rate' do
it 'a Rate object when there is a rate' do
Rate.stub!(:for_user_project_and_date).with(@user, @project, @date).and_return(@rate)
Rate.for(@user, @project, @date).should eql(50.50)
Rate.for(@user, @project, @date).should eql(@rate)
end
it 'a nil when there is no rate' do
@@ -164,15 +164,15 @@ describe Rate, 'for' do
Rate.for(@user, @project, @date)
end
it 'should return the value of the most recent rate found' do
it 'should return the most recent rate found' do
Rate.should_receive(:for_user_project_and_date).with(@user, @project, @date).and_return(@rate)
Rate.for(@user, @project, @date).should eql(@rate.amount)
Rate.for(@user, @project, @date).should eql(@rate)
end
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)
Rate.for(@user, @project, @date).should eql(@rate)
end
it 'should return nil if no set or default rate is found' do
@@ -188,9 +188,9 @@ describe Rate, 'for' do
Rate.for(@user, @project)
end
it 'should return the value of the most recent rate found' do
it 'should return the most recent rate found' do
Rate.should_receive(:for_user_project_and_date).with(@user, @project, Date.today.to_s).and_return(@rate)
Rate.for(@user, @project).should eql(@rate.amount)
Rate.for(@user, @project).should eql(@rate)
end
it 'should return nil if no set or default rate is found' do
@@ -206,9 +206,9 @@ describe Rate, 'for' do
Rate.for(@user)
end
it 'should return the value of the most recent rate found' do
it 'should return 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)
Rate.for(@user).should eql(@rate)
end
it 'should return nil if no set or default rate is found' do