diff --git a/app/controllers/budgets/executions_controller.rb b/app/controllers/budgets/executions_controller.rb index 52da2a7ed..25f27b1ca 100644 --- a/app/controllers/budgets/executions_controller.rb +++ b/app/controllers/budgets/executions_controller.rb @@ -14,8 +14,7 @@ module Budgets def investments_by_heading if params[:status].present? @budget.investments.winners - .joins(:milestones).includes(:milestones) - .select { |i| i.milestone_status_id == params[:status].to_i } + .with_milestone_status_id(params[:status]) .uniq .group_by(&:heading) else diff --git a/app/helpers/budget_executions_helper.rb b/app/helpers/budget_executions_helper.rb index 45175e806..90d0744b0 100644 --- a/app/helpers/budget_executions_helper.rb +++ b/app/helpers/budget_executions_helper.rb @@ -1,9 +1,7 @@ module BudgetExecutionsHelper def filters_select_counts(status) - @budget.investments.winners.with_milestones.select do |investment| - investment.milestone_status_id == status - end.count + @budget.investments.winners.with_milestone_status_id(status).count end def first_milestone_with_image(investment) diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 91a9f2219..110da8732 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -342,6 +342,12 @@ class Budget self.valuator_groups.collect(&:name).compact.join(', ').presence end + def self.with_milestone_status_id(status_id) + joins(:milestones).includes(:milestones).select do |investment| + investment.milestone_status_id == status_id.to_i + end + end + def milestone_status_id milestones.published.with_status.order_by_publication_date.last&.status_id end