bouquins-ror/app/helpers/preferences_helper.rb

40 lines
1.1 KiB
Ruby
Raw Normal View History

module PreferencesHelper
2015-08-02 09:12:04 +00:00
# update preferences (per_page, initial, sort)
def preferences
2015-08-04 11:47:26 +00:00
@preference = Preference.new
puts session[:preference]
@preference.from_json(session[:preference].to_s) if session[:preference]
p @preference
if params[:initial]
2015-08-04 11:47:26 +00:00
@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
2015-08-04 17:32:38 +00:00
def query_filter(prefix = nil)
sort_col = prefix ? prefix + ".sort" : "sort"
2015-08-04 11:47:26 +00:00
conditions = Array.new
params = Array.new
if @preference.initial
2015-08-04 17:32:38 +00:00
conditions.push "UPPER(#{sort_col}) LIKE ?"
2015-08-04 11:47:26 +00:00
params.push "#{@preference.initial}%"
end
if @preference.term
2015-08-04 17:32:38 +00:00
conditions.push "#{sort_col} LIKE ?"
2015-08-04 11:47:26 +00:00
params.push "%#{@preference.term}%"
end
2015-08-04 11:47:26 +00:00
[ conditions.join(" AND ") ] + params
end
2015-08-04 17:32:38 +00:00
def pagination
{ page: params[:page], per_page: @preference.per_page }
end
end