Change admin budget investments list order by supports from less to more

Sorting by id is in descending order and by title ascending

Backported from AyuntamientoMadrid/consul
This commit is contained in:
María Checa
2018-04-10 18:58:58 +02:00
committed by Marko Lovic
parent 7053711269
commit 5dfefbbe2f
3 changed files with 15 additions and 13 deletions

View File

@@ -75,17 +75,9 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
resource_model.parameterize('_') resource_model.parameterize('_')
end 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 def load_investments
@investments = Budget::Investment.scoped_filter(params, @current_filter) @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? @investments = @investments.page(params[:page]) unless request.format.csv?
end end

View File

@@ -57,6 +57,10 @@ class Budget
scope :sort_by_price, -> { reorder(price: :desc, confidence_score: :desc, id: :desc) } 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_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 :valuation_open, -> { where(valuation_finished: false) }
scope :without_admin, -> { valuation_open.where(administrator_id: nil) } scope :without_admin, -> { valuation_open.where(administrator_id: nil) }
scope :without_valuator, -> { valuation_open.where(valuator_assignments_count: 0) } scope :without_valuator, -> { valuation_open.where(valuator_assignments_count: 0) }
@@ -132,6 +136,12 @@ class Budget
results.where("budget_investments.id IN (?)", ids) results.where("budget_investments.id IN (?)", ids)
end 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) def self.limit_results(budget, params, results)
max_per_heading = params[:max_per_heading].to_i max_per_heading = params[:max_per_heading].to_i
return results if max_per_heading <= 0 return results if max_per_heading <= 0

View File

@@ -608,8 +608,8 @@ feature 'Admin budget investments' do
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')
expect('B First Investment').to appear_before('A Second Investment') expect('C Third Investment').to appear_before('A Second Investment')
expect('A Second Investment').to appear_before('C Third Investment') expect('A Second Investment').to appear_before('B First Investment')
end end
scenario 'Sort by title' do scenario 'Sort by title' do
@@ -622,8 +622,8 @@ feature 'Admin budget investments' do
scenario 'Sort by supports' do scenario 'Sort by supports' do
visit admin_budget_budget_investments_path(budget, sort_by: 'supports') visit admin_budget_budget_investments_path(budget, sort_by: 'supports')
expect('C Third Investment').to appear_before('A Second Investment') expect('B First Investment').to appear_before('A Second Investment')
expect('A Second Investment').to appear_before('B First Investment') expect('A Second Investment').to appear_before('C Third Investment')
end end
end end