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:
@@ -7,7 +7,16 @@ module Budgets
|
|||||||
def show
|
def show
|
||||||
authorize! :read_executions, @budget
|
authorize! :read_executions, @budget
|
||||||
@statuses = ::Budget::Investment::Status.all
|
@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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@@ -16,5 +25,11 @@ module Budgets
|
|||||||
@budget = Budget.find_by(slug: params[:id]) || Budget.find_by(id: params[:id])
|
@budget = Budget.find_by(slug: params[:id]) || Budget.find_by(id: params[:id])
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
<% @headings.each do |heading| %>
|
<% @headings.each do |heading| %>
|
||||||
<h4 id="<%= heading.name.parameterize %>">
|
<h4 id="<%= heading.name.parameterize %>">
|
||||||
<%= heading.name %>
|
<%= heading.name %>
|
||||||
</h4>
|
</h4>
|
||||||
<% if winner_investments(heading).any? %>
|
|
||||||
<div class="row" data-equalizer-on="medium" data-equalizer>
|
<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="small-12 medium-6 large-4 column end margin-bottom">
|
||||||
<div class="budget-execution">
|
<div class="budget-execution">
|
||||||
<%= link_to budget_investment_path(@budget, investment, anchor: "tab-milestones"), data: { 'equalizer-watch': true } do %>
|
<%= link_to budget_investment_path(@budget, investment, anchor: "tab-milestones"), data: { 'equalizer-watch': true } do %>
|
||||||
@@ -31,9 +30,4 @@
|
|||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<% else %>
|
|
||||||
<div class="callout primary clear">
|
|
||||||
<%= t("budgets.executions.no_winner_investments") %>
|
|
||||||
</div>
|
|
||||||
<% end %>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -62,6 +62,12 @@
|
|||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<%= render 'budgets/executions/investments' %>
|
<% if @headings.any? %>
|
||||||
|
<%= render 'budgets/executions/investments' %>
|
||||||
|
<% else %>
|
||||||
|
<div class="callout primary clear">
|
||||||
|
<%= t("budgets.executions.no_winner_investments") %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ en:
|
|||||||
page_title: "%{budget} - Milestones"
|
page_title: "%{budget} - Milestones"
|
||||||
heading: "Participatory budget Milestones"
|
heading: "Participatory budget Milestones"
|
||||||
heading_selection_title: "By district"
|
heading_selection_title: "By district"
|
||||||
no_winner_investments: "No winner investments for this heading"
|
no_winner_investments: "No winner investments in this state"
|
||||||
filters:
|
filters:
|
||||||
label: "Project's current state"
|
label: "Project's current state"
|
||||||
all: "All"
|
all: "All"
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ es:
|
|||||||
page_title: "%{budget} - Seguimiento de proyectos"
|
page_title: "%{budget} - Seguimiento de proyectos"
|
||||||
heading: "Seguimiento de proyectos"
|
heading: "Seguimiento de proyectos"
|
||||||
heading_selection_title: "Ámbito de actuación"
|
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:
|
filters:
|
||||||
label: "Estado actual del proyecto"
|
label: "Estado actual del proyecto"
|
||||||
all: "Todos"
|
all: "Todos"
|
||||||
|
|||||||
@@ -27,7 +27,9 @@ feature 'Executions' do
|
|||||||
expect(page).not_to have_content(investment4.title)
|
expect(page).not_to have_content(investment4.title)
|
||||||
end
|
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_group = create(:budget_group, budget: budget)
|
||||||
empty_heading = create(:budget_heading, group: empty_group, price: 1000)
|
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)
|
expect(page).to have_content(empty_heading.name)
|
||||||
|
|
||||||
click_link 'Milestones'
|
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
|
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
|
context 'Images' do
|
||||||
scenario 'renders milestone image if available' do
|
scenario 'renders milestone image if available' do
|
||||||
@@ -157,7 +173,6 @@ feature 'Executions' do
|
|||||||
select 'Studying the project', from: 'status'
|
select 'Studying the project', from: 'status'
|
||||||
|
|
||||||
expect(page).not_to have_content(investment1.title)
|
expect(page).not_to have_content(investment1.title)
|
||||||
expect(page).to have_content('No winner investments for this heading')
|
|
||||||
|
|
||||||
select 'Bidding', from: 'status'
|
select 'Bidding', from: 'status'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user