Add sorting options for Admin::BudgetInvestments (#2336)

This commit is contained in:
Angel Perez
2018-01-24 12:54:08 -04:00
parent d30d2b0dd8
commit e04dc5b8f2
7 changed files with 65 additions and 1 deletions

View File

@@ -52,12 +52,20 @@ 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
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(cached_votes_up: :desc, created_at: :desc)
.order(sort_by(params[:sort_by]))
end
@investments = @investments.page(params[:page]) unless request.format.csv?
end

View 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

View File

@@ -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

View File

@@ -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" %>

View File

@@ -139,6 +139,11 @@ en:
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

View File

@@ -139,6 +139,11 @@ es:
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

View File

@@ -359,6 +359,36 @@ feature 'Admin budget investments' do
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'))