diff --git a/app/models/budget.rb b/app/models/budget.rb index b35fa07..2f869ba 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -103,16 +103,8 @@ class Budget def profit return 0.0 unless self.deliverables.size > 0 - profit = 0.0 - # Fixed profit - profit += self.deliverables.collect(&:profit).delete_if { |d| d.blank?}.inject { |sum, n| sum + n } || 0.0 - - # Percentage Rate - self.deliverables.delete_if { |d| d.profit_percent.blank?}.each do |deliverable| - profit += deliverable.budget * (deliverable.profit_percent.to_f / 100 ) - end - - return profit + # Covers fixed and percentage profit though the +profit+ method being overloaded on the Deliverable types + return self.deliverables.collect(&:profit).delete_if { |d| d.blank?}.inject { |sum, n| sum + n } || 0.0 end # Dollar amount of time that has been logged to the project itself diff --git a/spec/models/budget_spec.rb b/spec/models/budget_spec.rb index e584eae..69a1f19 100644 --- a/spec/models/budget_spec.rb +++ b/spec/models/budget_spec.rb @@ -340,8 +340,8 @@ describe Budget, '.profit' do it 'should be calculated by the total profit of the deliverables with % amounts' do @project = mock_model(Project) - @deliverable1 = mock_model(HourlyDeliverable, :project_id => @project, :profit_percent => 10, :budget => 2000.0, :profit => nil) - @deliverable2 = mock_model(HourlyDeliverable, :project_id => @project, :profit_percent => 10, :budget => 1000.0, :profit => nil) + @deliverable1 = mock_model(HourlyDeliverable, :project_id => @project, :profit_percent => 10, :budget => 2000.0, :profit => 200) + @deliverable2 = mock_model(HourlyDeliverable, :project_id => @project, :profit_percent => 10, :budget => 1000.0, :profit => 100) Deliverable.stub!(:find_all_by_project_id).and_return([@deliverable1, @deliverable2]) Project.stub!(:find).with(@project.id).and_return(@project)