Adds valid_heading validation to budget line. Renames validation methods.

This commit is contained in:
kikito
2017-01-04 13:56:42 +01:00
parent 08586d521b
commit f5dcb51d37
2 changed files with 23 additions and 21 deletions

View File

@@ -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

View File

@@ -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)