diff --git a/init.rb b/init.rb index 8247e8a..c791cb2 100644 --- a/init.rb +++ b/init.rb @@ -2,10 +2,12 @@ require 'redmine' # Patches to the Redmine core. require 'dispatcher' +require 'overhead_deliverable_patch' require 'overhead_issue_patch' require 'overhead_time_entry_patch' require 'overhead_time_entry_activity_patch' Dispatcher.to_prepare do + Deliverable.send(:include, OverheadDeliverablePatch) Issue.send(:include, OverheadIssuePatch) TimeEntry.send(:include, OverheadTimeEntryPatch) TimeEntryActivity.send(:include, OverheadTimeEntryActivityPatch) diff --git a/lib/overhead_deliverable_patch.rb b/lib/overhead_deliverable_patch.rb new file mode 100644 index 0000000..00519fa --- /dev/null +++ b/lib/overhead_deliverable_patch.rb @@ -0,0 +1,26 @@ +require_dependency 'deliverable' + +module OverheadDeliverablePatch + def self.included(base) + base.send(:include, InstanceMethods) + + base.class_eval do + unloadable + end + end + + module InstanceMethods + # Cost of time logged to overhead activities + def overhead_spent + time_logs = issues.collect(&:time_entries).flatten + + return time_logs.collect {|time_entry| + if time_entry.billable? + 0 + else + time_entry.cost + end + }.sum + end + end +end diff --git a/spec/lib/overhead_deliverable_patch_spec.rb b/spec/lib/overhead_deliverable_patch_spec.rb new file mode 100644 index 0000000..4582847 --- /dev/null +++ b/spec/lib/overhead_deliverable_patch_spec.rb @@ -0,0 +1,42 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe Deliverable, '#overhead_spent' do + before(:each) do + @deliverable = Deliverable.new + end + + it 'should equal the product of overhead timelogs by their cost' do + issue1 = mock_model(Issue) + issue1.stub!(:time_entries).and_return do + [ + mock_model(TimeEntry, :cost => 100.0, :billable? => true), + mock_model(TimeEntry, :cost => 200.0, :billable? => false) + ] + end + issue2 = mock_model(Issue) + issue2.stub!(:time_entries).and_return do + [ + mock_model(TimeEntry, :cost => 10.0, :billable? => false), + mock_model(TimeEntry, :cost => 40.0, :billable? => false) + ] + end + + issues = [issue1, issue2] + + @deliverable.stub!(:issues).and_return(issues) + @deliverable.overhead_spent.should eql(250.0) + end + + it 'should be 0.0 if there are no issues assigned' do + @deliverable.stub!(:issues).and_return([]) + @deliverable.overhead_spent.should eql(0) + end + + it 'should be 0.0 if there are no time entries' do + issue1 = mock_model(Issue, :time_entries => []) + issue2 = mock_model(Issue, :time_entries => []) + + @deliverable.stub!(:issues).and_return([issue1, issue2]) + @deliverable.overhead_spent.should eql(0) + end +end