Adds methods for investments in models

This commit is contained in:
kikito
2016-06-03 18:44:34 +02:00
parent d57a6c510f
commit c6c3e22a40
4 changed files with 35 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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