diff --git a/app/models/budget/ballot.rb b/app/models/budget/ballot.rb index 9b9a089d6..1638b7afd 100644 --- a/app/models/budget/ballot.rb +++ b/app/models/budget/ballot.rb @@ -18,5 +18,13 @@ class Budget def amount_available(heading) budget.heading_price(heading) - amount_spent(heading.try(:id)) end + + def has_lines_with_no_heading? + investments.no_heading.count > 0 + end + + def has_lines_with_heading? + self.heading_id.present? + end end end diff --git a/app/models/budget/ballot/line.rb b/app/models/budget/ballot/line.rb index 804ee3a2c..887951922 100644 --- a/app/models/budget/ballot/line.rb +++ b/app/models/budget/ballot/line.rb @@ -3,6 +3,27 @@ class Budget class Line < ActiveRecord::Base belongs_to :ballot belongs_to :investment + + validate :insufficient_funds + validate :different_geozone, :if => :district_proposal? + validate :unfeasible + + def insufficient_funds + errors.add(:money, "") if ballot.amount_available(investment.heading) < investment.price.to_i + end + + def different_geozone + errors.add(:heading, "") if (ballot.heading.present? && investment.heading != ballot.heading) + end + + def unfeasible + errors.add(:unfeasible, "") unless investment.feasible? + end + + def heading_proposal? + investment.heading_id.present? + end + end end end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 329c2fce2..43b0fde2f 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -39,6 +39,7 @@ class Budget scope :valuation_finished, -> { where(valuation_finished: true) } scope :feasible, -> { where(feasibility: "feasible") } scope :unfeasible, -> { where(feasibility: "unfeasible") } + scope :not_unfeasible, -> { where.not(feasibility: "unfeasible") } scope :undecided, -> { where(feasibility: "undecided") } scope :with_supports, -> { where('cached_votes_up > 0') } diff --git a/app/models/user.rb b/app/models/user.rb index 4839d3ced..8da62dd0d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -88,6 +88,11 @@ class User < ActiveRecord::Base voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value } end + def budget_investment_votes(budget_investments) + voted = votes.for_budget_investments(budget_investments) + voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value } + end + def comment_flags(comments) comment_flags = flags.for_comments(comments) comment_flags.each_with_object({}){ |f, h| h[f.flaggable_id] = true }