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).
This commit is contained in:
Marko Lovic
2018-07-20 12:43:36 +02:00
committed by Javi Martín
parent a3ef662509
commit 8376efce3f
7 changed files with 48 additions and 43 deletions

View File

@@ -7,7 +7,16 @@ module Budgets
def show
authorize! :read_executions, @budget
@statuses = ::Budget::Investment::Status.all
@headings = @budget.headings.order(id: :asc)
@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
@@ -16,5 +25,11 @@ module Budgets
@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

View File

@@ -1,25 +0,0 @@
module BudgetExecutionsHelper
def winner_investments(heading)
if params[:status].present?
heading.investments
.winners
.joins(:milestones)
.distinct
.where(filter_investment_by_latest_milestone, params[:status])
else
heading.investments
.winners
.joins(:milestones)
.distinct
end
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

View File

@@ -2,9 +2,8 @@
<h4 id="<%= heading.name.parameterize %>">
<%= heading.name %>
</h4>
<% if winner_investments(heading).any? %>
<div class="row" data-equalizer-on="medium" data-equalizer>
<% winner_investments(heading).each do |investment| %>
<% heading.investments.each do |investment| %>
<div class="small-12 medium-6 large-4 column end margin-bottom">
<div class="budget-execution">
<%= link_to budget_investment_path(@budget, investment, anchor: "tab-milestones"), data: { 'equalizer-watch': true } do %>
@@ -31,9 +30,4 @@
</div>
<% end %>
</div>
<% else %>
<div class="callout primary clear">
<%= t("budgets.executions.no_winner_investments") %>
</div>
<% end %>
<% end %>

View File

@@ -62,6 +62,12 @@
</div>
<% end %>
<% if @headings.any? %>
<%= render 'budgets/executions/investments' %>
<% else %>
<div class="callout primary clear">
<%= t("budgets.executions.no_winner_investments") %>
</div>
<% end %>
</div>
</div>

View File

@@ -179,7 +179,7 @@ en:
page_title: "%{budget} - Milestones"
heading: "Participatory budget Milestones"
heading_selection_title: "By district"
no_winner_investments: "No winner investments for this heading"
no_winner_investments: "No winner investments in this state"
filters:
label: "Project's current state"
all: "All"

View File

@@ -179,7 +179,7 @@ es:
page_title: "%{budget} - Seguimiento de proyectos"
heading: "Seguimiento de proyectos"
heading_selection_title: "Ámbito de actuación"
no_winner_investments: "No hay proyectos de gasto ganadores para esta partida"
no_winner_investments: "No hay proyectos de gasto ganadores en este estado"
filters:
label: "Estado actual del proyecto"
all: "Todos"

View File

@@ -27,7 +27,9 @@ feature 'Executions' do
expect(page).not_to have_content(investment4.title)
end
scenario 'render a message for headings without winner investments' do
scenario "Do not display headings with no winning investments for selected status" do
create(:budget_investment_milestone, investment: investment1)
empty_group = create(:budget_group, budget: budget)
empty_heading = create(:budget_heading, group: empty_group, price: 1000)
@@ -38,11 +40,25 @@ feature 'Executions' do
expect(page).to have_content(empty_heading.name)
click_link 'Milestones'
click_link "#{empty_heading.name}"
expect(page).to have_content('No winner investments for this heading')
expect(page).to have_content(heading.name)
expect(page).not_to have_content(empty_heading.name)
end
scenario "Show message when there are no winning investments with the selected status", :js do
create(:budget_investment_status, name: I18n.t('seeds.budgets.statuses.executed'))
visit budget_path(budget)
click_link 'See results'
click_link 'Milestones'
expect(page).not_to have_content('No winner investments in this state')
select 'Executed', from: 'status'
expect(page).to have_content('No winner investments in this state')
end
context 'Images' do
scenario 'renders milestone image if available' do
@@ -157,7 +173,6 @@ feature 'Executions' do
select 'Studying the project', from: 'status'
expect(page).not_to have_content(investment1.title)
expect(page).to have_content('No winner investments for this heading')
select 'Bidding', from: 'status'