Fixed a bug where default rate wasn't getting used. #1918
This commit is contained in:
1 parent
2b7cfa4dbc
commit
5e774eb378
3 files changed
+70
-16
No files matched your search
+26
-11
@@ -35,23 +35,38 @@ class Rate < ActiveRecord::Base
|
|||||||
Rate.check_date_string(date)
|
Rate.check_date_string(date)
|
||||||
|
|
||||||
rate = self.for_user_project_and_date(user, project, 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 nil if rate.nil?
|
||||||
return rate.amount
|
return rate.amount
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def self.for_user_project_and_date(user, project, date)
|
def self.for_user_project_and_date(user, project, date)
|
||||||
project_id = project.nil? ? nil : project.id
|
if project.nil?
|
||||||
return Rate.find(:first,
|
return Rate.find(:first,
|
||||||
:order => 'date_in_effect DESC',
|
:order => 'date_in_effect DESC',
|
||||||
:conditions => [
|
:conditions => [
|
||||||
"user_id IN (?) AND project_id IN (?) AND date_in_effect <= ?",
|
"user_id IN (?) AND date_in_effect <= ?",
|
||||||
user.id,
|
user.id,
|
||||||
project_id,
|
date
|
||||||
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
|
end
|
||||||
|
|
||||||
# Checks a date string to make sure it is in format of +YYYY-MM-DD+, throwing
|
# Checks a date string to make sure it is in format of +YYYY-MM-DD+, throwing
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -169,8 +169,15 @@ describe Rate, 'for' do
|
|||||||
Rate.for(@user, @project, @date).should eql(@rate.amount)
|
Rate.for(@user, @project, @date).should eql(@rate.amount)
|
||||||
end
|
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(: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
|
Rate.for(@user, @project, @date).should be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -186,8 +193,9 @@ describe Rate, 'for' do
|
|||||||
Rate.for(@user, @project).should eql(@rate.amount)
|
Rate.for(@user, @project).should eql(@rate.amount)
|
||||||
end
|
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(: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
|
Rate.for(@user, @project).should be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -203,7 +211,7 @@ describe Rate, 'for' do
|
|||||||
Rate.for(@user).should eql(@rate.amount)
|
Rate.for(@user).should eql(@rate.amount)
|
||||||
end
|
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.should_receive(:for_user_project_and_date).with(@user, nil, Date.today.to_s).and_return(nil)
|
||||||
Rate.for(@user).should be_nil
|
Rate.for(@user).should be_nil
|
||||||
end
|
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
|
it 'should search rates without a project when +project+ is nil' do
|
||||||
Rate.should_receive(:find).with(:first, {
|
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,
|
@user.id,
|
||||||
nil,
|
|
||||||
@date
|
@date
|
||||||
],
|
],
|
||||||
:order => 'date_in_effect DESC'
|
:order => 'date_in_effect DESC'
|
||||||
|
|||||||
Reference in new issue
Block a user