diff --git a/app/controllers/budgets/executions_controller.rb b/app/controllers/budgets/executions_controller.rb index 5bc7705e0..9db722de4 100644 --- a/app/controllers/budgets/executions_controller.rb +++ b/app/controllers/budgets/executions_controller.rb @@ -7,14 +7,19 @@ module Budgets def show authorize! :read_executions, @budget @statuses = ::Budget::Investment::Status.all - @headings = @budget.headings - .includes(investments: :milestones) - .joins(investments: :milestones) - .distinct - .order(name: :asc) if params[:status].present? - @headings = @headings.where(filter_investment_by_latest_milestone, params[:status]) + @investments_by_heading = @budget.investments.winners + .joins(:milestones).includes(:milestones) + .select { |i| i.milestones.published.with_status + .order_by_publication_date.last + .status_id == params[:status].to_i } + .uniq + .group_by(&:heading) + else + @investments_by_heading = @budget.investments.winners + .joins(:milestones).includes(:milestones) + .distinct.group_by(&:heading) end @headings = reorder_alphabetically_with_city_heading_first(@headings) @@ -25,24 +30,5 @@ module Budgets def load_budget @budget = Budget.find_by(slug: params[:id]) || Budget.find_by(id: params[:id]) end - - def filter_investment_by_latest_milestone - <<-SQL - (SELECT status_id FROM budget_investment_milestones - WHERE investment_id = budget_investments.id ORDER BY publication_date DESC LIMIT 1) = ? - SQL - end - - def reorder_alphabetically_with_city_heading_first(headings) - headings.sort do |a, b| - if a.name == 'Toda la ciudad' - -1 - elsif b.name == 'Toda la ciudad' - 1 - else - a.name <=> b.name - end - end - end end end diff --git a/app/models/budget/investment/milestone.rb b/app/models/budget/investment/milestone.rb index c71516446..2421974da 100644 --- a/app/models/budget/investment/milestone.rb +++ b/app/models/budget/investment/milestone.rb @@ -18,6 +18,8 @@ class Budget validate :description_or_status_present? scope :order_by_publication_date, -> { order(publication_date: :asc) } + scope :published, -> { where("publication_date <= ?", Date.today) } + scope :with_status, -> { where("status_id IS NOT NULL") } def self.title_max_length 80 diff --git a/app/views/budgets/executions/_investments.html.erb b/app/views/budgets/executions/_investments.html.erb index 0808b354a..9a03dacb8 100644 --- a/app/views/budgets/executions/_investments.html.erb +++ b/app/views/budgets/executions/_investments.html.erb @@ -1,9 +1,9 @@ -<% @headings.each do |heading| %> +<% @investments_by_heading.each do |heading, investments| %>

- <%= heading.name %> + <%= heading.name %> (<%= investments.count %>)

- <% heading.investments.each do |investment| %> + <% investments.each do |investment| %>
<%= link_to budget_investment_path(@budget, investment, anchor: "tab-milestones"), data: { 'equalizer-watch': true } do %> diff --git a/app/views/budgets/executions/show.html.erb b/app/views/budgets/executions/show.html.erb index dc18d2d08..941e0d2d2 100644 --- a/app/views/budgets/executions/show.html.erb +++ b/app/views/budgets/executions/show.html.erb @@ -44,7 +44,7 @@ <%= t("budgets.executions.heading_selection_title") %>