From f5dcb51d37bcbf0df40424f947b0b7185f58ec5a Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 4 Jan 2017 13:56:42 +0100 Subject: [PATCH] Adds valid_heading validation to budget line. Renames validation methods. --- app/models/budget/ballot/line.rb | 18 +++++++----------- spec/models/budget/ballot_spec.rb | 26 ++++++++++++++++---------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/app/models/budget/ballot/line.rb b/app/models/budget/ballot/line.rb index 83e388ee1..23c6aaef2 100644 --- a/app/models/budget/ballot/line.rb +++ b/app/models/budget/ballot/line.rb @@ -9,28 +9,24 @@ class Budget validates :ballot_id, :investment_id, :heading_id, :group_id, :budget_id, presence: true - validate :insufficient_funds - #needed? validate :different_geozone, :if => :district_proposal? - validate :unselected + validate :check_selected + validate :check_sufficient_funds + validate :check_valid_heading before_validation :set_denormalized_ids - def insufficient_funds + def check_sufficient_funds errors.add(:money, "insufficient funds") if ballot.amount_available(investment.heading) < investment.price.to_i end - def different_geozone - errors.add(:heading, "different heading assigned") if (ballot.heading.present? && investment.heading != ballot.heading) + def check_valid_heading + errors.add(:heading, "This heading's budget is invalid, or a heading on the same group was already selected") unless ballot.valid_heading?(self.heading) end - def unselected + def check_selected errors.add(:investment, "unselected investment") unless investment.selected? end - def heading_proposal? - investment.heading_id.present? - end - private def set_denormalized_ids diff --git a/spec/models/budget/ballot_spec.rb b/spec/models/budget/ballot_spec.rb index e99a2276d..259a4a4f4 100644 --- a/spec/models/budget/ballot_spec.rb +++ b/spec/models/budget/ballot_spec.rb @@ -23,29 +23,35 @@ describe Budget::Ballot do end it "returns the amount spent on all investments assigned to a specific heading" do - heading = create(:budget_heading) - budget = heading.group.budget - inv1 = create(:budget_investment, :selected, price: 10000, heading: heading) - inv2 = create(:budget_investment, :selected, price: 20000, heading: create(:budget_heading, group: heading.group)) - inv3 = create(:budget_investment, :selected, price: 40000, heading: heading) + budget = create(:budget) + group1 = create(:budget_group, budget: budget) + group2 = create(:budget_group, budget: budget) + heading1 = create(:budget_heading, group: group1, price: 100000) + heading2 = create(:budget_heading, group: group2, price: 200000) + inv1 = create(:budget_investment, :selected, price: 10000, heading: heading1) + inv2 = create(:budget_investment, :selected, price: 20000, heading: heading2) + inv3 = create(:budget_investment, :selected, price: 40000, heading: heading1) ballot = create(:budget_ballot, budget: budget) ballot.investments << inv1 << inv2 - expect(ballot.amount_spent(heading)).to eq 10000 + expect(ballot.amount_spent(heading1)).to eq 10000 + expect(ballot.amount_spent(heading2)).to eq 20000 ballot.investments << inv3 - expect(ballot.amount_spent(heading)).to eq 50000 + expect(ballot.amount_spent(heading1)).to eq 50000 + expect(ballot.amount_spent(heading2)).to eq 20000 end end describe "#amount_available" do it "returns how much is left after taking some investments" do budget = create(:budget) - group = create(:budget_group, budget: budget) - heading1 = create(:budget_heading, group: group, price: 1000) - heading2 = create(:budget_heading, group: group, price: 300) + group1 = create(:budget_group, budget: budget) + group2 = create(:budget_group, budget: budget) + heading1 = create(:budget_heading, group: group1, price: 1000) + heading2 = create(:budget_heading, group: group2, price: 300) inv1 = create(:budget_investment, :selected, price: 100, heading: heading1) inv2 = create(:budget_investment, :selected, price: 200, heading: heading2) inv3 = create(:budget_investment, :selected, price: 400, heading: heading1)