uses direct associations when managing investments

This commit is contained in:
Juanjo Bazán
2016-09-05 13:01:46 +02:00
committed by Juanjo Bazán
parent ee2e0b864f
commit 6d06a59403
2 changed files with 8 additions and 14 deletions

View File

@@ -12,6 +12,8 @@ class Budget
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
belongs_to :heading belongs_to :heading
belongs_to :group
belongs_to :budget
belongs_to :administrator belongs_to :administrator
has_many :valuator_assignments, dependent: :destroy has_many :valuator_assignments, dependent: :destroy
@@ -58,16 +60,14 @@ class Budget
end end
def self.scoped_filter(params, current_filter) def self.scoped_filter(params, current_filter)
results = budget.investments results = Investment.where(budget_id: params[:budget_id])
if params[:max_for_no_heading].present? || params[:max_per_heading].present? results = results.where(group_id: params[:group_id]) if params[:group_id].present?
results = limit_results(results, budget, params[:max_per_heading].to_i, params[:max_for_no_heading].to_i)
end
results = results.by_heading(params[:heading_id]) if params[:heading_id].present? results = results.by_heading(params[:heading_id]) if params[:heading_id].present?
results = results.by_admin(params[:administrator_id]) if params[:administrator_id].present? results = results.by_admin(params[:administrator_id]) if params[:administrator_id].present?
results = results.by_tag(params[:tag_name]) if params[:tag_name].present? results = results.by_tag(params[:tag_name]) if params[:tag_name].present?
results = results.by_valuator(params[:valuator_id]) if params[:valuator_id].present? results = results.by_valuator(params[:valuator_id]) if params[:valuator_id].present?
results = results.send(current_filter) if current_filter.present? results = results.send(current_filter) if current_filter.present?
results.includes(:heading, administrator: :user, valuators: :user) results.includes(:heading, :group, :budget, administrator: :user, valuators: :user)
end end
def self.limit_results(results, budget, max_per_heading, max_for_no_heading) def self.limit_results(results, budget, max_per_heading, max_for_no_heading)
@@ -114,14 +114,6 @@ class Budget
where(heading_id: heading == 'all' ? nil : heading.presence) where(heading_id: heading == 'all' ? nil : heading.presence)
end end
def budget
heading.group.budget
end
def budget=(resource)
heading.group.budget = resource
end
def undecided? def undecided?
feasibility == "undecided" feasibility == "undecided"
end end

View File

@@ -223,7 +223,9 @@ FactoryGirl.define do
factory :budget_investment, class: 'Budget::Investment' do factory :budget_investment, class: 'Budget::Investment' do
sequence(:title) { |n| "Budget Investment #{n} title" } sequence(:title) { |n| "Budget Investment #{n} title" }
association :heading, factory: :budget_heading budget
group { create :budget_group, budget: budget }
heading { create :budget_heading, group: group }
association :author, factory: :user association :author, factory: :user
description 'Spend money on this' description 'Spend money on this'
price 10 price 10