diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index 0258c23a6..043c06394 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -75,17 +75,9 @@ class Admin::BudgetInvestmentsController < Admin::BaseController resource_model.parameterize('_') end - 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 @investments = Budget::Investment.scoped_filter(params, @current_filter) - .order(sort_by(params[:sort_by])) + @investments = @investments.order_filter(params[:sort_by]) if params[:sort_by].present? @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 ee3292fcd..82c17e517 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -57,6 +57,10 @@ class Budget scope :sort_by_price, -> { reorder(price: :desc, confidence_score: :desc, id: :desc) } scope :sort_by_random, ->(seed) { reorder("budget_investments.id % #{seed.to_f.nonzero? ? seed.to_f : 1}, budget_investments.id") } + scope :sort_by_id, -> { order("id DESC") } + scope :sort_by_title, -> { order("title ASC") } + scope :sort_by_supports, -> { order("cached_votes_up DESC") } + scope :valuation_open, -> { where(valuation_finished: false) } scope :without_admin, -> { valuation_open.where(administrator_id: nil) } scope :without_valuator, -> { valuation_open.where(valuator_assignments_count: 0) } @@ -132,6 +136,12 @@ class Budget results.where("budget_investments.id IN (?)", ids) end + def self.order_filter(sorting_param) + if sorting_param.present? && SORTING_OPTIONS.include?(sorting_param) + send("sort_by_#{sorting_param}") + end + end + def self.limit_results(budget, params, results) max_per_heading = params[:max_per_heading].to_i return results if max_per_heading <= 0 diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb index fac876f17..60034f297 100644 --- a/spec/features/admin/budget_investments_spec.rb +++ b/spec/features/admin/budget_investments_spec.rb @@ -608,8 +608,8 @@ feature 'Admin budget investments' do scenario 'Sort by ID' do visit admin_budget_budget_investments_path(budget, sort_by: 'id') - expect('B First Investment').to appear_before('A Second Investment') - expect('A Second Investment').to appear_before('C Third Investment') + expect('C Third Investment').to appear_before('A Second Investment') + expect('A Second Investment').to appear_before('B First Investment') end scenario 'Sort by title' do @@ -622,8 +622,8 @@ feature 'Admin budget investments' do scenario 'Sort by supports' do visit admin_budget_budget_investments_path(budget, sort_by: 'supports') - expect('C Third Investment').to appear_before('A Second Investment') - expect('A Second Investment').to appear_before('B First Investment') + expect('B First Investment').to appear_before('A Second Investment') + expect('A Second Investment').to appear_before('C Third Investment') end end