Reuse code to set and order by a random seed

This commit is contained in:
Javi Martín
2018-12-19 19:18:10 +01:00
parent e3ca700e17
commit 6682121069
7 changed files with 10 additions and 21 deletions

View File

@@ -4,6 +4,7 @@ module Budgets
include FeatureFlags
include CommentableActions
include FlagActions
include RandomSeed
include ImageAttributes
PER_PAGE = 10
@@ -119,16 +120,6 @@ module Budgets
@investment_votes = current_user ? current_user.budget_investment_votes(investments) : {}
end
def set_random_seed
if params[:order] == 'random' || params[:order].blank?
seed = params[:random_seed] || session[:random_seed] || rand
params[:random_seed] = seed
session[:random_seed] = params[:random_seed]
else
params[:random_seed] = nil
end
end
def investment_params
params.require(:budget_investment)
.permit(:title, :description, :heading_id, :tag_list,
@@ -170,7 +161,7 @@ module Budgets
def investments
if @current_order == 'random'
@budget.investments.apply_filters_and_search(@budget, params, @current_filter)
.send("sort_by_#{@current_order}", params[:random_seed])
.sort_by_random(session[:random_seed])
else
@budget.investments.apply_filters_and_search(@budget, params, @current_filter)
.send("sort_by_#{@current_order}")

View File

@@ -107,7 +107,7 @@ class Legislation::ProcessesController < Legislation::BaseController
@current_filter = "winners" if params[:filter].blank? && @proposals.winners.any?
if @current_filter == "random"
@proposals = @proposals.send(@current_filter, session[:random_seed]).page(params[:page])
@proposals = @proposals.sort_by_random(session[:random_seed]).page(params[:page])
else
@proposals = @proposals.send(@current_filter).page(params[:page])
end