Recalculate heading winners on incompatibility change

Why:

* We should recalculate winners also when an incompatible investment is flagged as compatible again

How:

* Removing the condition to recalculate that was checking only for a winner investment flagged as incompatible
* Extending the Budget::Result model spec to cover that new scenario
This commit is contained in:
Bertocq
2017-07-10 14:24:55 +02:00
parent 08bc32defb
commit 2b85deabec
2 changed files with 18 additions and 4 deletions

View File

@@ -204,7 +204,7 @@ class Budget
end
def recalculate_heading_winners
Budget::Result.new(budget, heading).calculate_winners if incompatible_changed? && winner? && incompatible?
Budget::Result.new(budget, heading).calculate_winners if incompatible_changed?
end
def set_responsible_name

View File

@@ -15,7 +15,7 @@ describe Budget::Result do
investment4 = create(:budget_investment, :selected, heading: heading, price: 500, ballot_lines_count: 600, winner: false)
investment5 = create(:budget_investment, :selected, heading: heading, price: 100, ballot_lines_count: 500, winner: false)
Budget::Result.new(budget, heading).calculate_winners
described_class.new(budget, heading).calculate_winners
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
end
@@ -29,7 +29,7 @@ describe Budget::Result do
investment4 = create(:budget_investment, :winner, heading: heading, price: 500, ballot_lines_count: 600)
investment5 = create(:budget_investment, :winner, heading: heading, price: 100, ballot_lines_count: 500)
Budget::Result.new(budget, heading).calculate_winners
described_class.new(budget, heading).calculate_winners
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
end
@@ -49,6 +49,20 @@ describe Budget::Result do
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
end
end
end
context "When an incompatible is flagged as compatible again" do
it "recalculates winners taking it in consideration" do
investment1 = create(:budget_investment, :winner, heading: heading, price: 200, ballot_lines_count: 900)
investment2 = create(:budget_investment, :winner, heading: heading, price: 300, ballot_lines_count: 800)
investment3 = create(:budget_investment, :incompatible, heading: heading, price: 500, ballot_lines_count: 700)
investment4 = create(:budget_investment, :winner, heading: heading, price: 500, ballot_lines_count: 600)
investment5 = create(:budget_investment, :winner, heading: heading, price: 100, ballot_lines_count: 500)
investment3.incompatible = false
investment3.save
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment3.id])
end
end
end
end