From 1649b9125e25049f1da59a9ddc6ab94dbd2046bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 6 Feb 2023 14:30:00 +0100 Subject: [PATCH] Add scope to get investments visible to a valuator Using this method makes it more obvious that we're loading the same investments in the budgets index as in the investments index. --- app/components/valuation/budgets/row_component.rb | 2 +- .../valuation/budget_investments_controller.rb | 10 +++++----- app/models/budget/investment.rb | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/components/valuation/budgets/row_component.rb b/app/components/valuation/budgets/row_component.rb index fd9417bd2..6795f90d9 100644 --- a/app/components/valuation/budgets/row_component.rb +++ b/app/components/valuation/budgets/row_component.rb @@ -11,7 +11,7 @@ class Valuation::Budgets::RowComponent < ApplicationComponent def investments return Budget::Investment.none unless budget.valuating_or_later? - budget.investments.visible_to_valuators.by_valuator(current_user.valuator) + budget.investments.visible_to_valuator(current_user.valuator) end def valuation_open_investments_count diff --git a/app/controllers/valuation/budget_investments_controller.rb b/app/controllers/valuation/budget_investments_controller.rb index f1ff5d834..37f891617 100644 --- a/app/controllers/valuation/budget_investments_controller.rb +++ b/app/controllers/valuation/budget_investments_controller.rb @@ -17,7 +17,8 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController def index @heading_filters = heading_filters @investments = if current_user.valuator? && @budget.present? - @budget.investments.visible_to_valuators.scoped_filter(params_for_current_valuator, @current_filter) + @budget.investments.visible_to_valuator(current_user.valuator) + .scoped_filter(filtered_params, @current_filter) .order(cached_votes_up: :desc) .page(params[:page]) else @@ -72,7 +73,7 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController end def heading_filters - investments = @budget.investments.by_valuator(current_user.valuator&.id).visible_to_valuators.distinct + investments = @budget.investments.visible_to_valuator(current_user.valuator).distinct investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id)).sort_by(&:name) all_headings_filter = [ @@ -92,9 +93,8 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController end end - def params_for_current_valuator - Budget::Investment.filter_params(params).to_h.merge({ valuator_id: current_user.valuator.id, - budget_id: @budget.id }) + def filtered_params + Budget::Investment.filter_params(params).to_h.except(:valuator_id).merge(budget_id: @budget.id) end def valuation_params diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 76fe714f4..306f89793 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -100,6 +100,7 @@ class Budget scope :by_heading, ->(heading_id) { where(heading_id: heading_id) } scope :by_admin, ->(admin_id) { where(administrator_id: admin_id) } scope :by_tag, ->(tag_name) { tagged_with(tag_name).distinct } + scope :visible_to_valuator, ->(valuator) { visible_to_valuators.by_valuator(valuator) } scope :for_render, -> { includes(:heading) }