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.
This commit is contained in:
Javi Martín
2023-02-06 14:30:00 +01:00
parent ed4b03c3ed
commit 1649b9125e
3 changed files with 7 additions and 6 deletions

View File

@@ -11,7 +11,7 @@ class Valuation::Budgets::RowComponent < ApplicationComponent
def investments def investments
return Budget::Investment.none unless budget.valuating_or_later? 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 end
def valuation_open_investments_count def valuation_open_investments_count

View File

@@ -17,7 +17,8 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController
def index def index
@heading_filters = heading_filters @heading_filters = heading_filters
@investments = if current_user.valuator? && @budget.present? @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) .order(cached_votes_up: :desc)
.page(params[:page]) .page(params[:page])
else else
@@ -72,7 +73,7 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController
end end
def heading_filters 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) investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id)).sort_by(&:name)
all_headings_filter = [ all_headings_filter = [
@@ -92,9 +93,8 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController
end end
end end
def params_for_current_valuator def filtered_params
Budget::Investment.filter_params(params).to_h.merge({ valuator_id: current_user.valuator.id, Budget::Investment.filter_params(params).to_h.except(:valuator_id).merge(budget_id: @budget.id)
budget_id: @budget.id })
end end
def valuation_params def valuation_params

View File

@@ -100,6 +100,7 @@ class Budget
scope :by_heading, ->(heading_id) { where(heading_id: heading_id) } scope :by_heading, ->(heading_id) { where(heading_id: heading_id) }
scope :by_admin, ->(admin_id) { where(administrator_id: admin_id) } scope :by_admin, ->(admin_id) { where(administrator_id: admin_id) }
scope :by_tag, ->(tag_name) { tagged_with(tag_name).distinct } 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) } scope :for_render, -> { includes(:heading) }