Adds valid_heading validation to budget line. Renames validation methods.
This commit is contained in:
@@ -9,28 +9,24 @@ class Budget
|
|||||||
|
|
||||||
validates :ballot_id, :investment_id, :heading_id, :group_id, :budget_id, presence: true
|
validates :ballot_id, :investment_id, :heading_id, :group_id, :budget_id, presence: true
|
||||||
|
|
||||||
validate :insufficient_funds
|
validate :check_selected
|
||||||
#needed? validate :different_geozone, :if => :district_proposal?
|
validate :check_sufficient_funds
|
||||||
validate :unselected
|
validate :check_valid_heading
|
||||||
|
|
||||||
before_validation :set_denormalized_ids
|
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
|
errors.add(:money, "insufficient funds") if ballot.amount_available(investment.heading) < investment.price.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
def different_geozone
|
def check_valid_heading
|
||||||
errors.add(:heading, "different heading assigned") if (ballot.heading.present? && investment.heading != ballot.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
|
end
|
||||||
|
|
||||||
def unselected
|
def check_selected
|
||||||
errors.add(:investment, "unselected investment") unless investment.selected?
|
errors.add(:investment, "unselected investment") unless investment.selected?
|
||||||
end
|
end
|
||||||
|
|
||||||
def heading_proposal?
|
|
||||||
investment.heading_id.present?
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_denormalized_ids
|
def set_denormalized_ids
|
||||||
|
|||||||
@@ -23,29 +23,35 @@ describe Budget::Ballot do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "returns the amount spent on all investments assigned to a specific heading" do
|
it "returns the amount spent on all investments assigned to a specific heading" do
|
||||||
heading = create(:budget_heading)
|
budget = create(:budget)
|
||||||
budget = heading.group.budget
|
group1 = create(:budget_group, budget: budget)
|
||||||
inv1 = create(:budget_investment, :selected, price: 10000, heading: heading)
|
group2 = create(:budget_group, budget: budget)
|
||||||
inv2 = create(:budget_investment, :selected, price: 20000, heading: create(:budget_heading, group: heading.group))
|
heading1 = create(:budget_heading, group: group1, price: 100000)
|
||||||
inv3 = create(:budget_investment, :selected, price: 40000, heading: heading)
|
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 = create(:budget_ballot, budget: budget)
|
||||||
ballot.investments << inv1 << inv2
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#amount_available" do
|
describe "#amount_available" do
|
||||||
it "returns how much is left after taking some investments" do
|
it "returns how much is left after taking some investments" do
|
||||||
budget = create(:budget)
|
budget = create(:budget)
|
||||||
group = create(:budget_group, budget: budget)
|
group1 = create(:budget_group, budget: budget)
|
||||||
heading1 = create(:budget_heading, group: group, price: 1000)
|
group2 = create(:budget_group, budget: budget)
|
||||||
heading2 = create(:budget_heading, group: group, price: 300)
|
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)
|
inv1 = create(:budget_investment, :selected, price: 100, heading: heading1)
|
||||||
inv2 = create(:budget_investment, :selected, price: 200, heading: heading2)
|
inv2 = create(:budget_investment, :selected, price: 200, heading: heading2)
|
||||||
inv3 = create(:budget_investment, :selected, price: 400, heading: heading1)
|
inv3 = create(:budget_investment, :selected, price: 400, heading: heading1)
|
||||||
|
|||||||
Reference in New Issue
Block a user