Adds methods for investments in models
This commit is contained in:
@@ -18,5 +18,13 @@ class Budget
|
|||||||
def amount_available(heading)
|
def amount_available(heading)
|
||||||
budget.heading_price(heading) - amount_spent(heading.try(:id))
|
budget.heading_price(heading) - amount_spent(heading.try(:id))
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,6 +3,27 @@ class Budget
|
|||||||
class Line < ActiveRecord::Base
|
class Line < ActiveRecord::Base
|
||||||
belongs_to :ballot
|
belongs_to :ballot
|
||||||
belongs_to :investment
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ class Budget
|
|||||||
scope :valuation_finished, -> { where(valuation_finished: true) }
|
scope :valuation_finished, -> { where(valuation_finished: true) }
|
||||||
scope :feasible, -> { where(feasibility: "feasible") }
|
scope :feasible, -> { where(feasibility: "feasible") }
|
||||||
scope :unfeasible, -> { where(feasibility: "unfeasible") }
|
scope :unfeasible, -> { where(feasibility: "unfeasible") }
|
||||||
|
scope :not_unfeasible, -> { where.not(feasibility: "unfeasible") }
|
||||||
scope :undecided, -> { where(feasibility: "undecided") }
|
scope :undecided, -> { where(feasibility: "undecided") }
|
||||||
scope :with_supports, -> { where('cached_votes_up > 0') }
|
scope :with_supports, -> { where('cached_votes_up > 0') }
|
||||||
|
|
||||||
|
|||||||
@@ -88,6 +88,11 @@ class User < ActiveRecord::Base
|
|||||||
voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value }
|
voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value }
|
||||||
end
|
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)
|
def comment_flags(comments)
|
||||||
comment_flags = flags.for_comments(comments)
|
comment_flags = flags.for_comments(comments)
|
||||||
comment_flags.each_with_object({}){ |f, h| h[f.flaggable_id] = true }
|
comment_flags.each_with_object({}){ |f, h| h[f.flaggable_id] = true }
|
||||||
|
|||||||
Reference in New Issue
Block a user