Add default order for admin budget investments list

When there's no sorting option selected, by default it orders the investment list by supports and, for those with the same number of supports, by ID.
This commit is contained in:
María Checa
2018-04-12 13:40:33 +02:00
committed by Javi Martín
parent 6098fb1188
commit 650fe2553e
3 changed files with 13 additions and 1 deletions

View File

@@ -77,7 +77,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
def load_investments def load_investments
@investments = Budget::Investment.scoped_filter(params, @current_filter) @investments = Budget::Investment.scoped_filter(params, @current_filter)
@investments = @investments.order_filter(params[:sort_by]) if params[:sort_by].present? .order_filter(params[:sort_by])
@investments = @investments.page(params[:page]) unless request.format.csv? @investments = @investments.page(params[:page]) unless request.format.csv?
end end

View File

@@ -142,6 +142,8 @@ class Budget
def self.order_filter(sorting_param) def self.order_filter(sorting_param)
if sorting_param.present? && SORTING_OPTIONS.include?(sorting_param) if sorting_param.present? && SORTING_OPTIONS.include?(sorting_param)
send("sort_by_#{sorting_param}") send("sort_by_#{sorting_param}")
else
order(cached_votes_up: :desc).order(id: :desc)
end end
end end

View File

@@ -603,6 +603,16 @@ feature 'Admin budget investments' do
create(:budget_investment, title: 'C Third Investment', cached_votes_up: 10, budget: budget) create(:budget_investment, title: 'C Third Investment', cached_votes_up: 10, budget: budget)
end end
scenario "Default sorting" do
create(:budget_investment, title: 'D Fourth Investment', cached_votes_up: 50, budget: budget)
visit admin_budget_budget_investments_path(budget)
expect('D Fourth Investment').to appear_before('B First Investment')
expect('D Fourth Investment').to appear_before('A Second Investment')
expect('A Second Investment').to appear_before('C Third Investment')
end
scenario 'Sort by ID' do scenario 'Sort by ID' do
visit admin_budget_budget_investments_path(budget, sort_by: 'id') visit admin_budget_budget_investments_path(budget, sort_by: 'id')