adds funds and feasibility validations to lines

This commit is contained in:
Juanjo Bazán
2016-06-11 15:01:51 +02:00
parent ab008ed4e9
commit 56a358a638
2 changed files with 77 additions and 0 deletions

View File

@@ -8,6 +8,19 @@ class Budget
belongs_to :investment
validates :ballot_id, :budget_id, :group_id, :heading_id, :investment_id, presence: true
validate :insufficient_funds
validate :unfeasible
def insufficient_funds
return unless errors.blank?
errors.add(:money, "") if ballot.amount_available(heading) < investment.price.to_i
end
def unfeasible
return unless errors.blank?
errors.add(:unfeasible, "") unless investment.feasible?
end
end
end
end

View File

@@ -36,5 +36,69 @@ describe "Budget::Ballot::Line" do
expect(ballot_line).to_not be_valid
end
describe 'Money' do
it "should not be valid if insufficient funds" do
budget = create(:budget)
group = create(:budget_group, budget: budget)
heading = create(:budget_heading, group: group, price: 10000000)
investment = create(:budget_investment, :feasible, price: heading.price + 1, heading: heading)
ballot = create(:budget_ballot, budget: budget)
ballot_line = build(:budget_ballot_line, ballot: ballot, budget: budget, group: group, heading: heading, investment: investment)
expect(ballot_line).to_not be_valid
end
it "should be valid if sufficient funds" do
budget = create(:budget)
group = create(:budget_group, budget: budget)
heading = create(:budget_heading, group: group, price: 10000000)
investment = create(:budget_investment, :feasible, price: heading.price - 1, heading: heading, )
ballot = create(:budget_ballot, budget: budget)
ballot_line = build(:budget_ballot_line, ballot: ballot, budget: budget, group: group, heading: heading, investment: investment)
expect(ballot_line).to be_valid
end
end
describe 'Feasibility' do
it "should not be valid if investment is unfeasible" do
budget = create(:budget)
group = create(:budget_group, budget: budget)
heading = create(:budget_heading, group: group, price: 10000000)
investment = create(:budget_investment, :feasible, price: 20000, feasibility: "unfeasible")
ballot = create(:budget_ballot, budget: budget)
ballot_line = build(:budget_ballot_line, ballot: ballot, budget: budget, group: group, heading: heading, investment: investment)
expect(ballot_line).to_not be_valid
end
it "should not be valid if investment feasibility is undecided" do
budget = create(:budget)
group = create(:budget_group, budget: budget)
heading = create(:budget_heading, group: group, price: 10000000)
investment = create(:budget_investment, price: 20000, feasibility: "undecided")
ballot = create(:budget_ballot, budget: budget)
ballot_line = build(:budget_ballot_line, ballot: ballot, budget: budget, group: group, heading: heading, investment: investment)
expect(ballot_line).to_not be_valid
end
it "should be valid if investment is feasible" do
budget = create(:budget)
group = create(:budget_group, budget: budget)
heading = create(:budget_heading, group: group, price: 10000000)
investment = create(:budget_investment, price: 20000, feasibility: "feasible")
ballot = create(:budget_ballot, budget: budget)
ballot_line = build(:budget_ballot_line, ballot: ballot, budget: budget, group: group, heading: heading, investment: investment)
expect(ballot_line).to be_valid
end
end
end
end