1 # Mixin module for reading out query parameters from request params.
6 # @where, @filters, @limit, @offset, @orders
9 # Default limit on number of rows to return in a single query.
12 # Load params[:where] into @where
14 if params[:where].nil? or params[:where] == ""
16 elsif params[:where].is_a? Hash
17 @where = params[:where]
18 elsif params[:where].is_a? String
20 @where = Oj.load(params[:where])
21 raise unless @where.is_a? Hash
23 raise ArgumentError.new("Could not parse \"where\" param as an object")
26 @where = @where.with_indifferent_access
29 # Load params[:filters] into @filters
30 def load_filters_param
32 if params[:filters].is_a? Array
33 @filters += params[:filters]
34 elsif params[:filters].is_a? String and !params[:filters].empty?
36 f = Oj.load params[:filters]
37 raise unless f.is_a? Array
40 raise ArgumentError.new("Could not parse \"filters\" param as an array")
45 # Load params[:limit], params[:offset] and params[:order]
46 # into @limit, @offset, @orders
47 def load_limit_offset_order_params
49 unless params[:limit].to_s.match(/^\d+$/)
50 raise ArgumentError.new("Invalid value for limit parameter")
52 @limit = params[:limit].to_i
54 @limit = DEFAULT_LIMIT
58 unless params[:offset].to_s.match(/^\d+$/)
59 raise ArgumentError.new("Invalid value for offset parameter")
61 @offset = params[:offset].to_i
68 params[:order].split(',').each do |order|
69 attr, direction = order.strip.split " "
71 if attr.match /^[a-z][_a-z0-9]+$/ and
72 model_class.columns.collect(&:name).index(attr) and
73 ['asc','desc'].index direction.downcase
74 @orders << "#{table_name}.#{attr} #{direction.downcase}"
79 @orders << "#{table_name}.modified_at desc"