Files
nairobi/app/controllers/budgets/executions_controller.rb
Marko Lovic 8376efce3f Hide headings with no investments
The page should not show any headings which don't have any
winning investments. The "no content" message should only be
shown when there are no headings with investments to avoid an
otherwise blank page.

__Note:__ in the main @headings query, _both_ #includes and #joins
are needed to:
 1. eager load all necessary data (#includes)
and
 2. to perform an INNER JOIN on milestones to filter out investments
with no milestones (#joins).
2018-11-06 13:02:35 +01:00

36 lines
1.1 KiB
Ruby

module Budgets
class ExecutionsController < ApplicationController
before_action :load_budget
load_and_authorize_resource :budget
def show
authorize! :read_executions, @budget
@statuses = ::Budget::Investment::Status.all
@headings = @budget.headings
.includes(investments: :milestones)
.joins(investments: :milestones)
.where(budget_investments: {winner: true})
.distinct
.order(id: :asc)
if params[:status].present?
@headings = @headings.where(filter_investment_by_latest_milestone, params[:status])
end
end
private
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
end
end