Move validating params to model

This commit is contained in:
Anna Anks Nowak
2019-01-01 21:29:29 +01:00
committed by Javi Martín
parent 37b2226432
commit 3ab70ff0d8
2 changed files with 8 additions and 12 deletions

View File

@@ -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

View File

@@ -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)