1 # Mixin module for reading out query parameters from request params.
6 # @where, @filters, @limit, @offset, @orders
9 # Default number of rows to return in a single query.
12 # Maximum number of rows to return in a single query, even if the client asks for more.
15 # Load params[:where] into @where
17 if params[:where].nil? or params[:where] == ""
19 elsif params[:where].is_a? Hash
20 @where = params[:where]
21 elsif params[:where].is_a? String
23 @where = Oj.load(params[:where])
24 raise unless @where.is_a? Hash
26 raise ArgumentError.new("Could not parse \"where\" param as an object")
29 @where = @where.with_indifferent_access
32 # Load params[:filters] into @filters
33 def load_filters_param
35 if params[:filters].is_a? Array
36 @filters += params[:filters]
37 elsif params[:filters].is_a? String and !params[:filters].empty?
39 f = Oj.load params[:filters]
41 raise unless f.is_a? Array
45 raise ArgumentError.new("Could not parse \"filters\" param as an array")
51 ["#{table_name}.modified_at desc"]
54 # Load params[:limit], params[:offset] and params[:order]
55 # into @limit, @offset, @orders
56 def load_limit_offset_order_params
58 unless params[:limit].to_s.match(/^\d+$/)
59 raise ArgumentError.new("Invalid value for limit parameter")
61 @limit = [params[:limit].to_i, MAX_LIMIT].min
63 @limit = DEFAULT_LIMIT
67 unless params[:offset].to_s.match(/^\d+$/)
68 raise ArgumentError.new("Invalid value for offset parameter")
70 @offset = params[:offset].to_i
76 if (params[:order].is_a?(Array) && !params[:order].empty?) || !params[:order].blank?
80 if params[:order].starts_with? '['
81 od = Oj.load(params[:order])
82 raise unless od.is_a? Array
85 params[:order].split(',')
93 attr, direction = order.strip.split " "
95 # The attr can have its table unspecified if it happens to be for the current "model_class" (the first case)
96 # or it can be fully specified with the database tablename (the second case) (e.g. "collections.name").
97 # NB that the security check for the second case table_name will not work if the model
98 # has used set_table_name to use an alternate table name from the Rails standard.
99 # I could not find a perfect way to handle this well, but ActiveRecord::Base.send(:descendants)
100 # would be a place to start if this ever becomes necessary.
101 if attr.match /^[a-z][_a-z0-9]+$/ and
102 model_class.columns.collect(&:name).index(attr) and
103 ['asc','desc'].index direction.downcase
104 @orders << "#{table_name}.#{attr} #{direction.downcase}"
105 elsif attr.match /^([a-z][_a-z0-9]+)\.([a-z][_a-z0-9]+)$/ and
106 ['asc','desc'].index(direction.downcase) and
107 ActiveRecord::Base.connection.tables.include?($1) and
108 $1.classify.constantize.columns.collect(&:name).index($2)
109 # $1 in the above checks references the first match from the regular expression, which is expected to be the database table name
110 # $2 is of course the actual database column name
111 @orders << "#{attr} #{direction.downcase}"
117 @orders = default_orders
122 @select = params[:select]
125 @select = Oj.load params[:select]
126 raise unless @select.is_a? Array or @select.nil?
128 raise ArgumentError.new("Could not parse \"select\" param as an array")
133 # Any ordering columns must be selected when doing select,
134 # otherwise it is an SQL error, so filter out invaliding orderings.
135 @orders.select! { |o|
136 # match select column against order array entry
137 @select.select { |s| /^#{table_name}.#{s}( (asc|desc))?$/.match o }.any?
141 @distinct = true if (params[:distinct] == true || params[:distinct] == "true")
142 @distinct = false if (params[:distinct] == false || params[:distinct] == "false")