bouquins-ror/app/helpers/preferences_helper.rb

35 lines
1003 B
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
def query_filter
conditions = Array.new
params = Array.new
if @preference.initial
conditions.push "UPPER(sort) LIKE ?"
params.push "#{@preference.initial}%"
end
if @preference.term
conditions.push "sort LIKE ?"
params.push "%#{@preference.term}%"
end
2015-08-04 11:47:26 +00:00
[ conditions.join(" AND ") ] + params
end
end