Merge pull request #2378 from wairbut-m2c/aperez-investments-search
Add search & sorting options to Admin::BudgetInvestments
This commit is contained in:
@@ -52,9 +52,21 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
|
||||
|
||||
private
|
||||
|
||||
def sort_by(params)
|
||||
if params.present? && Budget::Investment::SORTING_OPTIONS.include?(params)
|
||||
"#{params == 'supports' ? 'cached_votes_up' : params} ASC"
|
||||
else
|
||||
"cached_votes_up DESC, created_at DESC"
|
||||
end
|
||||
end
|
||||
|
||||
def load_investments
|
||||
@investments = Budget::Investment.scoped_filter(params, @current_filter)
|
||||
.order(cached_votes_up: :desc, created_at: :desc)
|
||||
if params[:project_title].present?
|
||||
@investments = Budget::Investment.where("title ILIKE ?", "%#{params[:project_title].strip}%")
|
||||
else
|
||||
@investments = Budget::Investment.scoped_filter(params, @current_filter)
|
||||
.order(sort_by(params[:sort_by]))
|
||||
end
|
||||
@investments = @investments.page(params[:page]) unless request.format.csv?
|
||||
end
|
||||
|
||||
|
||||
5
app/helpers/budget_investments_helper.rb
Normal file
5
app/helpers/budget_investments_helper.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
module BudgetInvestmentsHelper
|
||||
def budget_investments_sorting_options
|
||||
Budget::Investment::SORTING_OPTIONS.map { |so| [t("admin.budget_investments.index.sort_by.#{so}"), so] }
|
||||
end
|
||||
end
|
||||
@@ -2,6 +2,8 @@ require 'csv'
|
||||
|
||||
class Budget
|
||||
class Investment < ActiveRecord::Base
|
||||
SORTING_OPTIONS = %w(id title supports).freeze
|
||||
|
||||
include Rails.application.routes.url_helpers
|
||||
include Measurable
|
||||
include Sanitizable
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
<div id="advanced_filters" class="row expanded advanced-filters-content hide" data-toggler=".hide">
|
||||
<%= 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],
|
||||
page: 1), method: :get, remote: true, enforce_utf8: false) do %>
|
||||
@@ -38,7 +39,7 @@
|
||||
</div>
|
||||
|
||||
<div class="small-12 medium-4 large-2 column">
|
||||
<%= submit_tag t("#{i18n_namespace}.filters.button"), class: "button expanded" %>
|
||||
<%= submit_tag t("admin.budget_investments.index.buttons.filter"), class: "button expanded" %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
@@ -1,3 +1,12 @@
|
||||
<%= form_tag(admin_budget_budget_investments_path(budget: @budget), method: :get) do %>
|
||||
<div class="small-12 medium-3 column">
|
||||
<%= select_tag :sort_by, options_for_select(budget_investments_sorting_options, params[:sort_by]),
|
||||
{ prompt: t("admin.budget_investments.index.sort_by.placeholder"),
|
||||
label: false,
|
||||
class: "js-submit-on-change" } %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= link_to t("admin.budget_investments.index.download_current_selection"),
|
||||
admin_budget_budget_investments_path(csv_params),
|
||||
class: "float-right small" %>
|
||||
@@ -74,6 +83,7 @@
|
||||
toggle_selection_admin_budget_budget_investment_path(@budget,
|
||||
investment,
|
||||
filter: params[:filter],
|
||||
sort_by: params[:sort_by],
|
||||
second_filter: params[:second_filter],
|
||||
max_per_heading: params[:max_per_heading],
|
||||
page: params[:page]),
|
||||
@@ -86,6 +96,7 @@
|
||||
toggle_selection_admin_budget_budget_investment_path(@budget,
|
||||
investment,
|
||||
filter: params[:filter],
|
||||
sort_by: params[:sort_by],
|
||||
second_filter: params[:second_filter],
|
||||
max_per_heading: params[:max_per_heading],
|
||||
page: params[:page]),
|
||||
|
||||
12
app/views/admin/budget_investments/_search_form.html.erb
Normal file
12
app/views/admin/budget_investments/_search_form.html.erb
Normal file
@@ -0,0 +1,12 @@
|
||||
<div class="small-12 medium-6">
|
||||
<%= form_for(Budget::Investment.new, url: admin_budget_budget_investments_path(budget: @budget),
|
||||
method: :get,
|
||||
remote: true) do |f| %>
|
||||
<div class="input-group">
|
||||
<%= text_field_tag :project_title, "", placeholder: t("admin.budget_investments.index.placeholder") %>
|
||||
<div class="input-group-button">
|
||||
<%= f.submit t("admin.budget_investments.index.buttons.search"), class: "button" %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
@@ -1,5 +1,7 @@
|
||||
<h2 class="inline-block"><%= @budget.name %> - <%= t("admin.budget_investments.index.title") %></h2>
|
||||
|
||||
<%= render "search_form" %>
|
||||
|
||||
<%= form_tag(admin_budget_budget_investments_path(budget: @budget), method: :get, enforce_utf8: false) do %>
|
||||
<div class="small-12 medium-3 column">
|
||||
<%= select_tag :administrator_id,
|
||||
|
||||
@@ -138,6 +138,12 @@ en:
|
||||
valuator_filter_all: All valuators
|
||||
tags_filter_all: All tags
|
||||
advanced_filters: Advanced filters
|
||||
placeholder: Search projects
|
||||
sort_by:
|
||||
placeholder: Sort by
|
||||
id: ID
|
||||
title: Title
|
||||
supports: Supports
|
||||
filters:
|
||||
all: All
|
||||
without_admin: Without assigned admin
|
||||
@@ -150,9 +156,11 @@ en:
|
||||
unfeasible: Unfeasible
|
||||
max_per_heading: Max. supports per heading
|
||||
winners: Winners
|
||||
button: Filter
|
||||
one_filter_html: "Current applied filter: <b><em>%{filter}</em></b>"
|
||||
two_filters_html: "Current applied filters: <b><em>%{filter}, %{second_filter}</em></b>"
|
||||
buttons:
|
||||
search: Search
|
||||
filter: Filter
|
||||
download_current_selection: "Download current selection"
|
||||
no_budget_investments: "There are no investment projects."
|
||||
title: Investment projects
|
||||
|
||||
@@ -138,6 +138,12 @@ es:
|
||||
valuator_filter_all: Todos los evaluadores
|
||||
tags_filter_all: Todas las etiquetas
|
||||
advanced_filters: Filtros avanzados
|
||||
placeholder: Buscar proyectos
|
||||
sort_by:
|
||||
placeholder: Ordenar por
|
||||
id: ID
|
||||
title: Título
|
||||
supports: Apoyos
|
||||
filters:
|
||||
all: Todos
|
||||
without_admin: Sin administrador
|
||||
@@ -150,9 +156,11 @@ es:
|
||||
unfeasible: Inviables
|
||||
max_per_heading: Corte por partida
|
||||
winners: Ganadores
|
||||
button: Filtrar
|
||||
one_filter_html: "Filtro en uso: <b><em>%{filter}</em></b>"
|
||||
two_filters_html: "Filtros en uso: <b><em>%{filter}, %{second_filter}</em></b>"
|
||||
buttons:
|
||||
search: Buscar
|
||||
filter: Filtrar
|
||||
download_current_selection: "Descargar selección actual"
|
||||
no_budget_investments: "No hay proyectos de gasto."
|
||||
title: Proyectos de gasto
|
||||
|
||||
@@ -338,6 +338,57 @@ feature 'Admin budget investments' do
|
||||
|
||||
end
|
||||
|
||||
context 'Search' do
|
||||
background do
|
||||
@budget = create(:budget)
|
||||
@investment_1 = create(:budget_investment, title: "Some investment", budget: @budget)
|
||||
@investment_2 = create(:budget_investment, title: "Some other investment", budget: @budget)
|
||||
end
|
||||
|
||||
scenario "Search investments by title" do
|
||||
visit admin_budget_budget_investments_path(@budget)
|
||||
|
||||
expect(page).to have_content(@investment_1.title)
|
||||
expect(page).to have_content(@investment_2.title)
|
||||
|
||||
fill_in 'project_title', with: 'Some investment'
|
||||
click_button 'Search'
|
||||
|
||||
expect(page).to have_content(@investment_1.title)
|
||||
expect(page).to_not have_content(@investment_2.title)
|
||||
end
|
||||
end
|
||||
|
||||
context 'Sorting' do
|
||||
background do
|
||||
@budget = create(:budget)
|
||||
@investment_1 = create(:budget_investment, title: "BBBB", cached_votes_up: 50, budget: @budget)
|
||||
@investment_2 = create(:budget_investment, title: "AAAA", cached_votes_up: 25, budget: @budget)
|
||||
@investment_3 = create(:budget_investment, title: "CCCC", cached_votes_up: 10, budget: @budget)
|
||||
end
|
||||
|
||||
scenario 'Sort by ID' do
|
||||
visit admin_budget_budget_investments_path(@budget, sort_by: 'id')
|
||||
|
||||
expect(@investment_1.title).to appear_before(@investment_2.title)
|
||||
expect(@investment_2.title).to appear_before(@investment_3.title)
|
||||
end
|
||||
|
||||
scenario 'Sort by title' do
|
||||
visit admin_budget_budget_investments_path(@budget, sort_by: 'title')
|
||||
|
||||
expect(@investment_2.title).to appear_before(@investment_1.title)
|
||||
expect(@investment_1.title).to appear_before(@investment_3.title)
|
||||
end
|
||||
|
||||
scenario 'Sort by supports' do
|
||||
visit admin_budget_budget_investments_path(@budget, sort_by: 'supports')
|
||||
|
||||
expect(@investment_3.title).to appear_before(@investment_2.title)
|
||||
expect(@investment_2.title).to appear_before(@investment_1.title)
|
||||
end
|
||||
end
|
||||
|
||||
context 'Show' do
|
||||
background do
|
||||
@administrator = create(:administrator, user: create(:user, username: 'Ana', email: 'ana@admins.org'))
|
||||
|
||||
Reference in New Issue
Block a user