bouquins-ror/app/helpers/preferences_helper.rb

41 lines
1.2 KiB
Ruby

module PreferencesHelper
# update preferences (per_page, initial, sort)
def preferences
@preference = Preference.new
puts session[:preference]
@preference.from_json(session[:preference].to_s) if session[:preference]
p @preference
if params[:initial]
@preference.initial = (params[:initial] == "reset" ? nil : params[:initial])
end
@preference.per_page = params[:per_page] ? params[:per_page].to_i : (@preference.per_page || WillPaginate.per_page)
@preference.sort = params[:sort] if params[:sort]
@preference.term = params[:term] if params[:term]
puts @preference.as_json
puts @preference.to_json
session[:preference] = @preference.to_json
end
def query_filter(prefix = nil)
sort_col = prefix ? prefix + ".sort" : "sort"
conditions = Array.new
params = Array.new
if @preference.initial
conditions.push "UPPER(#{sort_col}) LIKE ?"
params.push "#{@preference.initial}%"
end
if @preference.term
conditions.push "#{sort_col} LIKE ?"
search_term = ActiveSupport::Inflector.transliterate(@preference.term)
params.push "%#{search_term}%"
end
[ conditions.join(" AND ") ] + params
end
def pagination
{ page: params[:page], per_page: @preference.per_page }
end
end