From 6d06a59403aecc18e15b539c0cf2f093df9aa35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Mon, 5 Sep 2016 13:01:46 +0200 Subject: [PATCH] uses direct associations when managing investments --- app/models/budget/investment.rb | 18 +++++------------- spec/factories.rb | 4 +++- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 890175035..f76d1f68e 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -12,6 +12,8 @@ class Budget belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' belongs_to :heading + belongs_to :group + belongs_to :budget belongs_to :administrator has_many :valuator_assignments, dependent: :destroy @@ -58,16 +60,14 @@ class Budget end def self.scoped_filter(params, current_filter) - 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 + results = Investment.where(budget_id: params[:budget_id]) + results = results.where(group_id: params[:group_id]) if params[:group_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_tag(params[:tag_name]) if params[:tag_name].present? results = results.by_valuator(params[:valuator_id]) if params[:valuator_id].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 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) end - def budget - heading.group.budget - end - - def budget=(resource) - heading.group.budget = resource - end - def undecided? feasibility == "undecided" end diff --git a/spec/factories.rb b/spec/factories.rb index db694b4ce..243e3a928 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -223,7 +223,9 @@ FactoryGirl.define do factory :budget_investment, class: 'Budget::Investment' do 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 description 'Spend money on this' price 10