diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb
index c2d5cbb75..9bf4d4610 100644
--- a/app/controllers/admin/budget_investments_controller.rb
+++ b/app/controllers/admin/budget_investments_controller.rb
@@ -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
diff --git a/app/helpers/budget_investments_helper.rb b/app/helpers/budget_investments_helper.rb
new file mode 100644
index 000000000..40db91c1f
--- /dev/null
+++ b/app/helpers/budget_investments_helper.rb
@@ -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
diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb
index 5534c7d00..58674be9b 100644
--- a/app/models/budget/investment.rb
+++ b/app/models/budget/investment.rb
@@ -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
diff --git a/app/views/admin/budget_investments/_investments.html.erb b/app/views/admin/budget_investments/_investments.html.erb
index 3456021db..06c4e3ae8 100644
--- a/app/views/admin/budget_investments/_investments.html.erb
+++ b/app/views/admin/budget_investments/_investments.html.erb
@@ -1,3 +1,12 @@
+<%= form_tag(admin_budget_budget_investments_path(budget: @budget), method: :get) do %>
+
+ <%= 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" } %>
+
+<% end %>
+
<%= link_to t("admin.budget_investments.index.download_current_selection"),
admin_budget_budget_investments_path(csv_params),
class: "float-right small" %>
diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml
index d979bcf17..a09d7a0ba 100644
--- a/config/locales/en/admin.yml
+++ b/config/locales/en/admin.yml
@@ -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
diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml
index d13c2cc42..308567ea7 100644
--- a/config/locales/es/admin.yml
+++ b/config/locales/es/admin.yml
@@ -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
diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb
index c9ad52a9b..8bf28e313 100644
--- a/spec/features/admin/budget_investments_spec.rb
+++ b/spec/features/admin/budget_investments_spec.rb
@@ -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'))