adds Budget::Group model to group headings
many refactors through budget related models
This commit is contained in:
@@ -4,9 +4,11 @@ class Budget < ActiveRecord::Base
|
||||
|
||||
validates :phase, inclusion: { in: VALID_PHASES }
|
||||
|
||||
has_many :investments
|
||||
has_many :ballots
|
||||
has_many :headings
|
||||
has_many :investments, dependent: :destroy
|
||||
has_many :ballots, dependent: :destroy
|
||||
has_many :groups, dependent: :destroy
|
||||
has_many :headings, through: :groups
|
||||
has_many :investments, through: :headings
|
||||
|
||||
def on_hold?
|
||||
phase == "on_hold"
|
||||
@@ -29,7 +31,6 @@ class Budget < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def heading_price(heading)
|
||||
return price unless heading.present?
|
||||
heading_ids.include?(heading.id) ? heading.price : -1
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,7 +16,7 @@ class Budget
|
||||
end
|
||||
|
||||
def amount_available(heading)
|
||||
budget.heading_price(heading) - amount_spent(heading.try(:id))
|
||||
budget.heading_price(heading) - amount_spent(heading.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
10
app/models/budget/group.rb
Normal file
10
app/models/budget/group.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class Budget
|
||||
class Group < ActiveRecord::Base
|
||||
belongs_to :budget
|
||||
|
||||
has_many :headings, dependent: :destroy
|
||||
|
||||
validates :budget_id, presence: true
|
||||
validates :name, presence: true
|
||||
end
|
||||
end
|
||||
@@ -1,11 +1,11 @@
|
||||
class Budget
|
||||
class Heading < ActiveRecord::Base
|
||||
belongs_to :budget
|
||||
belongs_to :group
|
||||
belongs_to :geozone
|
||||
|
||||
has_many :investments
|
||||
|
||||
validates :budget_id, presence: true
|
||||
validates :group_id, presence: true
|
||||
validates :name, presence: true
|
||||
validates :price, presence: true
|
||||
end
|
||||
|
||||
@@ -10,7 +10,6 @@ class Budget
|
||||
acts_as_paranoid column: :hidden_at
|
||||
include ActsAsParanoidAliases
|
||||
|
||||
belongs_to :budget
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||
belongs_to :heading
|
||||
belongs_to :administrator
|
||||
@@ -43,7 +42,6 @@ class Budget
|
||||
scope :with_supports, -> { where('cached_votes_up > 0') }
|
||||
|
||||
scope :by_heading, -> (heading_id) { where(heading_id: heading_id) }
|
||||
scope :by_budget, -> (budget_id) { where(budget_id: budget_id) }
|
||||
scope :by_admin, -> (admin_id) { where(administrator_id: admin_id) }
|
||||
scope :by_tag, -> (tag_name) { tagged_with(tag_name) }
|
||||
scope :by_valuator, -> (valuator_id) { where("budget_valuator_assignments.valuator_id = ?", valuator_id).joins(:valuator_assignments) }
|
||||
@@ -61,8 +59,7 @@ class Budget
|
||||
end
|
||||
|
||||
def self.scoped_filter(params, current_filter)
|
||||
budget = Budget.find!(params[:budget_id])
|
||||
results = self.by_budget(params[:budget_id])
|
||||
results = budget.investments
|
||||
if params[:max_for_no_heading].present? || params[:max_per_heading].present?
|
||||
results = limit_results(results, budget, params[:max_per_heading].to_i, params[:max_for_no_heading].to_i)
|
||||
end
|
||||
@@ -118,6 +115,10 @@ class Budget
|
||||
where(heading_id: heading == 'all' ? nil : heading.presence)
|
||||
end
|
||||
|
||||
def budget
|
||||
heading.group.budget
|
||||
end
|
||||
|
||||
def undecided?
|
||||
feasibility == "undecided"
|
||||
end
|
||||
@@ -139,7 +140,7 @@ class Budget
|
||||
end
|
||||
|
||||
def code
|
||||
"B#{budget_id}I#{id}"
|
||||
"B#{budget.id}I#{id}"
|
||||
end
|
||||
|
||||
def reason_for_not_being_selectable_by(user)
|
||||
|
||||
Reference in New Issue
Block a user