Merge pull request #2407 from wairbut-m2c/aperez-investment-filters-aggregation

Investments can be applied several filters at once
This commit is contained in:
Raimond Garcia
2018-01-31 14:39:02 +01:00
committed by GitHub
9 changed files with 65 additions and 26 deletions

View File

@@ -15,7 +15,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
def index
respond_to do |format|
format.html
format.js { render layout: false }
format.js
format.csv do
send_data Budget::Investment.to_csv(@investments, headers: true),
filename: 'budget_investments.csv'
@@ -48,6 +48,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
def toggle_selection
@investment.toggle :selected
@investment.save
load_investments
end
private

View File

@@ -2,4 +2,8 @@ module BudgetInvestmentsHelper
def budget_investments_sorting_options
Budget::Investment::SORTING_OPTIONS.map { |so| [t("admin.budget_investments.index.sort_by.#{so}"), so] }
end
def budget_investments_advanced_filters(params)
params.map { |af| t("admin.budget_investments.index.filters.#{af}") }.join(', ')
end
end

View File

@@ -111,17 +111,21 @@ class Budget
results = results.by_heading(params[:heading_id]) if params[:heading_id].present?
results = results.by_valuator(params[:valuator_id]) if params[:valuator_id].present?
results = results.by_admin(params[:administrator_id]) if params[:administrator_id].present?
# Advanced filters
results = results.valuation_finished_feasible if params[:second_filter] == 'feasible'
results = results.where(selected: true) if params[:second_filter] == 'selected'
results = results.undecided if params[:second_filter] == 'undecided'
results = results.unfeasible if params[:second_filter] == 'unfeasible'
results = advanced_filters(params, results) if params[:advanced_filters].present?
results = results.send(current_filter) if current_filter.present?
results.includes(:heading, :group, :budget, administrator: :user, valuators: :user)
end
def self.advanced_filters(params, results)
ids = []
ids += results.valuation_finished_feasible.pluck(:id) if params[:advanced_filters].include?('feasible')
ids += results.where(selected: true).pluck(:id) if params[:advanced_filters].include?('selected')
ids += results.undecided.pluck(:id) if params[:advanced_filters].include?('undecided')
ids += results.unfeasible.pluck(:id) if params[:advanced_filters].include?('unfeasible')
results.where("budget_investments.id IN (?)", ids)
end
def self.limit_results(budget, params, results)
max_per_heading = params[:max_per_heading].to_i
return results if max_per_heading <= 0

View File

@@ -8,27 +8,27 @@
<%= form_tag(admin_budget_budget_investments_path(budget: @budget,
filter: params[:filter],
sort_by: params[:sort_by],
second_filter: params[:second_filter],
max_per_heading: params[:max_per_heading],
advanced_filters: params[:advanced_filters],
page: 1), method: :get, remote: true, enforce_utf8: false) do %>
<div class="small-12 medium-8 large-10 column">
<span class="filter">
<%= check_box_tag :second_filter, "feasible" %>
<%= check_box_tag "advanced_filters[]", "feasible" %>
<%= t("#{i18n_namespace}.filters.feasible") %>
</span>
<span class="filter">
<%= check_box_tag :second_filter, "selected" %>
<%= check_box_tag "advanced_filters[]", "selected" %>
<%= t("#{i18n_namespace}.filters.selected") %>
</span>
<span class="filter">
<%= check_box_tag :second_filter, "undecided" %>
<%= check_box_tag "advanced_filters[]", "undecided" %>
<%= t("#{i18n_namespace}.filters.undecided") %>
</span>
<span class="filter">
<%= check_box_tag :second_filter, "unfeasible" %>
<%= check_box_tag "advanced_filters[]", "unfeasible" %>
<%= t("#{i18n_namespace}.filters.unfeasible") %>
</span>

View File

@@ -1,13 +1,16 @@
<% if params[:filter].present? && params[:second_filter].present? %>
<% if params[:filter].present? && params[:advanced_filters].present? %>
<p class="inline-block"><%= t("#{i18n_namespace}.filters.two_filters_html",
filter: t("#{i18n_namespace}.filters.#{params[:filter]}"),
second_filter: t("#{i18n_namespace}.filters.#{params[:second_filter]}")) %></p>
advanced_filters: budget_investments_advanced_filters(params[:advanced_filters])) %></p>
<% elsif params[:filter].present? %>
<p class="inline-block"><%= t("#{i18n_namespace}.filters.one_filter_html",
filter: t("#{i18n_namespace}.filters.#{params[:filter]}")) %></p>
<% elsif params[:second_filter].present? %>
<% elsif params[:advanced_filters].present? %>
<p class="inline-block"><%= t("#{i18n_namespace}.filters.one_filter_html",
filter: t("#{i18n_namespace}.filters.#{params[:second_filter]}")) %></p>
filter: budget_investments_advanced_filters(params[:advanced_filters])) %></p>
<% end %>

View File

@@ -84,8 +84,8 @@
investment,
filter: params[:filter],
sort_by: params[:sort_by],
second_filter: params[:second_filter],
max_per_heading: params[:max_per_heading],
advanced_filters: params[:advanced_filters],
page: params[:page]),
method: :patch,
remote: true,
@@ -97,8 +97,8 @@
investment,
filter: params[:filter],
sort_by: params[:sort_by],
second_filter: params[:second_filter],
max_per_heading: params[:max_per_heading],
advanced_filters: params[:advanced_filters],
page: params[:page]),
method: :patch,
remote: true,

View File

@@ -156,8 +156,8 @@ en:
unfeasible: Unfeasible
max_per_heading: Max. supports per heading
winners: Winners
one_filter_html: "Current applied filter: <b><em>%{filter}</em></b>"
two_filters_html: "Current applied filters: <b><em>%{filter}, %{second_filter}</em></b>"
one_filter_html: "Current applied filters: <b><em>%{filter}</em></b>"
two_filters_html: "Current applied filters: <b><em>%{filter}, %{advanced_filters}</em></b>"
buttons:
search: Search
filter: Filter

View File

@@ -156,8 +156,8 @@ es:
unfeasible: Inviables
max_per_heading: Corte por partida
winners: Ganadores
one_filter_html: "Filtro en uso: <b><em>%{filter}</em></b>"
two_filters_html: "Filtros en uso: <b><em>%{filter}, %{second_filter}</em></b>"
one_filter_html: "Filtros en uso: <b><em>%{filter}</em></b>"
two_filters_html: "Filtros en uso: <b><em>%{filter}, %{advanced_filters}</em></b>"
buttons:
search: Buscar
filter: Filtrar

View File

@@ -686,6 +686,7 @@ feature 'Admin budget investments' do
let!(:feasible_vf_bi) { create(:budget_investment, :feasible, :finished, budget: @budget, title: "Feasible, VF project") }
let!(:selected_bi) { create(:budget_investment, :selected, budget: @budget, title: "Selected project") }
let!(:winner_bi) { create(:budget_investment, :winner, budget: @budget, title: "Winner project") }
let!(:undecided_bi) { create(:budget_investment, :undecided, budget: @budget, title: "Undecided project") }
scenario "Filtering by valuation and selection", :js do
visit admin_budget_budget_investments_path(@budget)
@@ -698,7 +699,7 @@ feature 'Admin budget investments' do
expect(page).to have_content(winner_bi.title)
click_link 'Advanced filters'
within('#advanced_filters') { find(:css, "#second_filter[value='feasible']").set(true) }
within('#advanced_filters') { find(:css, "#advanced_filters_[value='feasible']").set(true) }
click_button 'Filter'
expect(page).not_to have_content(unfeasible_bi.title)
@@ -707,7 +708,8 @@ feature 'Admin budget investments' do
expect(page).to have_content(selected_bi.title)
expect(page).to have_content(winner_bi.title)
within('#advanced_filters') { find(:css, "#second_filter[value='selected']").set(true) }
within('#advanced_filters') { find(:css, "#advanced_filters_[value='selected']").set(true) }
within('#advanced_filters') { find(:css, "#advanced_filters_[value='feasible']").set(false) }
click_button 'Filter'
expect(page).not_to have_content(unfeasible_bi.title)
@@ -724,6 +726,31 @@ feature 'Admin budget investments' do
expect(page).to have_content(winner_bi.title)
end
scenario "Aggregating results", :js do
visit admin_budget_budget_investments_path(@budget)
click_link 'Advanced filters'
within('#advanced_filters') { find(:css, "#advanced_filters_[value='undecided']").set(true) }
click_button 'Filter'
expect(page).to have_content(undecided_bi.title)
expect(page).not_to have_content(winner_bi.title)
expect(page).not_to have_content(selected_bi.title)
expect(page).not_to have_content(feasible_bi.title)
expect(page).not_to have_content(unfeasible_bi.title)
expect(page).not_to have_content(feasible_vf_bi.title)
within('#advanced_filters') { find(:css, "#advanced_filters_[value='unfeasible']").set(true) }
click_button 'Filter'
expect(page).to have_content(undecided_bi.title)
expect(page).to have_content(unfeasible_bi.title)
expect(page).not_to have_content(winner_bi.title)
expect(page).not_to have_content(selected_bi.title)
expect(page).not_to have_content(feasible_bi.title)
expect(page).not_to have_content(feasible_vf_bi.title)
end
scenario "Showing the selection buttons", :js do
visit admin_budget_budget_investments_path(@budget)
@@ -757,7 +784,7 @@ feature 'Admin budget investments' do
end
click_link 'Advanced filters'
within('#advanced_filters') { find(:css, "#second_filter[value='selected']").set(true) }
within('#advanced_filters') { find(:css, "#advanced_filters_[value='selected']").set(true) }
click_button 'Filter'
within("#budget_investment_#{feasible_vf_bi.id}") do
@@ -769,7 +796,7 @@ feature 'Admin budget investments' do
scenario "Unselecting an investment", :js do
visit admin_budget_budget_investments_path(@budget)
click_link 'Advanced filters'
within('#advanced_filters') { find(:css, "#second_filter[value='selected']").set(true) }
within('#advanced_filters') { find(:css, "#advanced_filters_[value='selected']").set(true) }
click_button 'Filter'
expect(page).to have_content('There are 2 investments')