Wrapped Budget.budget, .labor_budget, and .spent with checks for nil values in the deliverables.
#1444
This commit is contained in:
@@ -38,12 +38,12 @@ class Budget
|
||||
|
||||
# Total budget all of the deliverables
|
||||
def budget
|
||||
return self.deliverables.collect(&:budget).inject { |sum, n| sum + n} || 0.0
|
||||
return self.deliverables.collect(&:budget).delete_if { |d| d.blank?}.inject { |sum, n| sum + n} || 0.0
|
||||
end
|
||||
|
||||
# Total labor budget all of the deliverables
|
||||
def labor_budget
|
||||
return self.deliverables.collect(&:labor_budget).inject { |sum, n| sum + n} || 0.0
|
||||
return self.deliverables.collect(&:labor_budget).delete_if { |d| d.blank?}.inject { |sum, n| sum + n} || 0.0
|
||||
end
|
||||
|
||||
# Amount of the budget spent. Expressed as as a percentage whole number
|
||||
@@ -58,7 +58,7 @@ class Budget
|
||||
|
||||
# Total amount spent for all the deliverables
|
||||
def spent
|
||||
self.deliverables.collect(&:spent).inject { |sum, n| sum + n } || 0.0
|
||||
self.deliverables.collect(&:spent).delete_if { |d| d.blank?}.inject { |sum, n| sum + n } || 0.0
|
||||
end
|
||||
|
||||
# Amount of budget left on the deliverables
|
||||
|
||||
@@ -223,6 +223,55 @@ describe Budget,'.budget' do
|
||||
|
||||
@budget.budget.should eql(0.0)
|
||||
end
|
||||
|
||||
it 'should skip deliverables with a nil budget' do
|
||||
@deliverable1 = mock_model(Deliverable, :project_id => @project, :budget => 2000.00)
|
||||
@deliverable2 = mock_model(Deliverable, :project_id => @project, :budget => nil)
|
||||
|
||||
@project = mock_model(Project)
|
||||
Deliverable.stub!(:find_all_by_project_id).and_return([@deliverable1, @deliverable2])
|
||||
Project.stub!(:find).with(@project.id).and_return(@project)
|
||||
|
||||
@budget = Budget.new(@project.id)
|
||||
@budget.budget.should eql(2000.0)
|
||||
end
|
||||
end
|
||||
|
||||
describe Budget,'.labor_budget' do
|
||||
it 'should be the sum of all the deliverables labor_budget' do
|
||||
@deliverable1 = mock_model(Deliverable, :project_id => @project, :labor_budget => 2000.00)
|
||||
@deliverable2 = mock_model(Deliverable, :project_id => @project, :labor_budget => 3000.00)
|
||||
|
||||
@project = mock_model(Project)
|
||||
Deliverable.stub!(:find_all_by_project_id).and_return([@deliverable1, @deliverable2])
|
||||
Project.stub!(:find).with(@project.id).and_return(@project)
|
||||
|
||||
@budget = Budget.new(@project.id)
|
||||
@budget.labor_budget.should eql(5000.0)
|
||||
end
|
||||
|
||||
it 'should be 0 if there are no deliverables' do
|
||||
Deliverable.stub!(:find_all_by_project_id).and_return([])
|
||||
|
||||
@project = mock_model(Project)
|
||||
Project.stub!(:find).with(@project.id).and_return(@project)
|
||||
|
||||
@budget = Budget.new(@project.id)
|
||||
|
||||
@budget.labor_budget.should eql(0.0)
|
||||
end
|
||||
|
||||
it 'should skip deliverables with a nil budget' do
|
||||
@deliverable1 = mock_model(Deliverable, :project_id => @project, :labor_budget => 2000.00)
|
||||
@deliverable2 = mock_model(Deliverable, :project_id => @project, :labor_budget => nil)
|
||||
|
||||
@project = mock_model(Project)
|
||||
Deliverable.stub!(:find_all_by_project_id).and_return([@deliverable1, @deliverable2])
|
||||
Project.stub!(:find).with(@project.id).and_return(@project)
|
||||
|
||||
@budget = Budget.new(@project.id)
|
||||
@budget.labor_budget.should eql(2000.0)
|
||||
end
|
||||
end
|
||||
|
||||
describe Budget,'.budget_ratio' do
|
||||
@@ -322,6 +371,19 @@ describe Budget, '.spent' do
|
||||
|
||||
@budget.spent.should eql(0.0)
|
||||
end
|
||||
|
||||
it 'should skip deliverables with a nil budget' do
|
||||
@deliverable1 = mock_model(Deliverable, :project_id => @project, :spent => 2000.00)
|
||||
@deliverable2 = mock_model(Deliverable, :project_id => @project, :spent => nil)
|
||||
|
||||
@project = mock_model(Project)
|
||||
Deliverable.stub!(:find_all_by_project_id).and_return([@deliverable1, @deliverable2])
|
||||
Project.stub!(:find).with(@project.id).and_return(@project)
|
||||
|
||||
@budget = Budget.new(@project.id)
|
||||
@budget.spent.should eql(2000.0)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe Budget, '.profit' do
|
||||
|
||||
Reference in New Issue
Block a user