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:
@@ -204,7 +204,7 @@ class Budget
|
|||||||
end
|
end
|
||||||
|
|
||||||
def recalculate_heading_winners
|
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
|
end
|
||||||
|
|
||||||
def set_responsible_name
|
def set_responsible_name
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ describe Budget::Result do
|
|||||||
investment4 = create(:budget_investment, :selected, heading: heading, price: 500, ballot_lines_count: 600, winner: false)
|
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)
|
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])
|
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
|
||||||
end
|
end
|
||||||
@@ -29,7 +29,7 @@ describe Budget::Result do
|
|||||||
investment4 = create(:budget_investment, :winner, heading: heading, price: 500, ballot_lines_count: 600)
|
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)
|
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])
|
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
|
||||||
end
|
end
|
||||||
@@ -49,6 +49,20 @@ describe Budget::Result do
|
|||||||
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
|
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user