diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index 259945c8e..71c92aee9 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -76,11 +76,9 @@ class Admin::BudgetInvestmentsController < Admin::BaseController end def load_investments - params[:direction].present? ? params[:direction] : params[:direction] = "asc" @investments = Budget::Investment.scoped_filter(params, @current_filter) - .order_filter(params[:sort_by], params[:direction]) + .order_filter(params) - @investments = Budget::Investment.by_budget(@budget).all @investments = @investments.page(params[:page]) unless request.format.csv? end @@ -136,5 +134,4 @@ class Admin::BudgetInvestmentsController < Admin::BaseController end end end - end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 0c90369be..e10070592 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -139,16 +139,15 @@ class Budget results.where("budget_investments.id IN (?)", ids) end - def self.order_filter(sorting_param, direction) - sorting_key = sorting_param.to_sym if sorting_param - allowed_sort_option = SORTING_OPTIONS.select { |sp| sp[sorting_key]}.reduce + def self.order_filter(params) + sorting_key = params[:sort_by].to_sym if params[:sort_by] + allowed_sort_option = SORTING_OPTIONS.select { |so| so[sorting_key]}.reduce - if allowed_sort_option.present? then - direction = %w[asc desc].include?(direction) ? direction : "asc" - order("#{allowed_sort_option[sorting_key]} #{direction}") - else - order(cached_votes_up: :desc).order(id: :desc) + if allowed_sort_option.present? + direction = %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc" + return order("#{allowed_sort_option[sorting_key]} #{direction}") end + order(cached_votes_up: :desc).order(id: :desc) end def self.limit_results(budget, params, results)