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 1/3] 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') From be864ee92f06f5af1f8c3e66bc99a730f7c8829c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 9 Jan 2019 12:55:22 +0100 Subject: [PATCH 2/3] Make sure selected investment is visibile in spec After changing the order for budget investments, the selected investment didn't appear on the first page anymore, and so it couldn't be clicked on during the test. --- .../features/admin/budget_investments_spec.rb | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb index e64c7ad66..8242dc891 100644 --- a/spec/features/admin/budget_investments_spec.rb +++ b/spec/features/admin/budget_investments_spec.rb @@ -1126,18 +1126,22 @@ feature 'Admin budget investments' do end end - scenario "Pagination after unselecting an investment", :js do - create_list(:budget_investment, 30, budget: budget) + feature "Pagination" do + background { selected_bi.update(cached_votes_up: 50) } - visit admin_budget_budget_investments_path(budget) + scenario "After unselecting an investment", :js do + create_list(:budget_investment, 30, budget: budget) - within("#budget_investment_#{selected_bi.id}") do - click_link('Selected') + visit admin_budget_budget_investments_path(budget) + + within("#budget_investment_#{selected_bi.id}") do + click_link('Selected') + end + + click_link('Next') + + expect(page).to have_link('Previous') end - - click_link('Next') - - expect(page).to have_link('Previous') end end From 7c06320f392a01ecdab9ed567d30712813f91b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 9 Jan 2019 12:55:27 +0100 Subject: [PATCH 3/3] Fix typo --- spec/features/admin/budget_investments_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb index 8242dc891..7e5a6be03 100644 --- a/spec/features/admin/budget_investments_spec.rb +++ b/spec/features/admin/budget_investments_spec.rb @@ -609,7 +609,7 @@ feature 'Admin budget investments' do 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('B First Investment').to appear_before('A Second Investment') expect('A Second Investment').to appear_before('C Third Investment') end