From 650fe2553ec996d5cb2215542ea01baacab1f2e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 12 Apr 2018 13:40:33 +0200 Subject: [PATCH] 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. --- app/controllers/admin/budget_investments_controller.rb | 2 +- app/models/budget/investment.rb | 2 ++ spec/features/admin/budget_investments_spec.rb | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index 6797f63d0..60ab11d7c 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -77,7 +77,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController def load_investments @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? end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 110da8732..1b528a9cb 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -142,6 +142,8 @@ class Budget def self.order_filter(sorting_param) if sorting_param.present? && SORTING_OPTIONS.include?(sorting_param) send("sort_by_#{sorting_param}") + else + order(cached_votes_up: :desc).order(id: :desc) end end diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb index 276e4bf2b..e64c7ad66 100644 --- a/spec/features/admin/budget_investments_spec.rb +++ b/spec/features/admin/budget_investments_spec.rb @@ -603,6 +603,16 @@ feature 'Admin budget investments' do create(:budget_investment, title: 'C Third Investment', cached_votes_up: 10, budget: budget) 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 visit admin_budget_budget_investments_path(budget, sort_by: 'id')