diff --git a/init.rb b/init.rb index 7330fd7..f303fa7 100644 --- a/init.rb +++ b/init.rb @@ -2,6 +2,7 @@ require 'redmine' require 'rate_users_helper_patch' require 'rate_sort_helper_patch' +require 'rate_time_entry_patch' require 'rate_project_hook' diff --git a/lib/rate_time_entry_patch.rb b/lib/rate_time_entry_patch.rb new file mode 100644 index 0000000..1aa1048 --- /dev/null +++ b/lib/rate_time_entry_patch.rb @@ -0,0 +1,38 @@ +require_dependency 'time_entry' + +module RateTimeEntryPatch + def self.included(base) # :nodoc: + base.extend(ClassMethods) + + base.send(:include, InstanceMethods) + + # Same as typing in the class + base.class_eval do + unloadable # Send unloadable so it will not be unloaded in development + belongs_to :rate + + end + + end + + module ClassMethods + + end + + module InstanceMethods + # Returns the current cost of the TimeEntry based on it's rate and hours + def cost + if self.rate.nil? + amount = Rate.amount_for(self.user, self.project, self.spent_on.to_s) + else + amount = rate.amount + end + + return 0.0 if amount.nil? + + return amount.to_f * hours.to_f + end + end +end + +TimeEntry.send(:include, RateTimeEntryPatch) diff --git a/spec/lib/rate_time_entry_patch_spec.rb b/spec/lib/rate_time_entry_patch_spec.rb new file mode 100644 index 0000000..56469fd --- /dev/null +++ b/spec/lib/rate_time_entry_patch_spec.rb @@ -0,0 +1,29 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe TimeEntry, 'cost' do + before(:each) do + @user = mock_model(User) + @project = mock_model(Project) + @date = Date.today.to_s + @time_entry = TimeEntry.new({:user => @user, :project => @project, :spent_on => @date, :hours => 10.0}) + end + + it 'should return 0.0 if there are no rates for the user' do + Rate.should_receive(:amount_for).with(@user, @project, @date).and_return(nil) + @time_entry.cost.should eql(0.0) + end + + describe 'should return the product of hours by' do + it 'the results of Rate.amount_for' do + Rate.should_receive(:amount_for).with(@user, @project, @date).and_return(200.0) + @time_entry.cost.should eql(200.0 * @time_entry.hours) + end + + it 'the assigned rate' do + rate = mock_model(Rate, :amount => 100.0) + @time_entry.should_receive(:rate).at_least(:twice).and_return(rate) + @time_entry.cost.should eql(rate.amount * @time_entry.hours) + end + + end +end