22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/puffer/orm_adapter/active_record.rb', line 22
def filter scope, fields, options = {}
conditions, order = extract_conditions_and_order!(options)
order = order.map { |name, dir| field = fields[name]; "#{query_order(field)} #{dir}" if field && field.column }.compact.join(', ')
conditions_fields = fields.select {|f| f.column && conditions.keys.include?(f.field_name)}.to_fieldset
search_fields = fields.select {|f| f.column && !conditions_fields.include?(f) && search_types.include?(f.column_type)}
all_fields = conditions_fields + search_fields
scope = scope.includes(includes(all_fields)).includes(reflection_includes(fields)).where(searches(search_fields, options[:search])).order(order)
conditions.each do |name, value|
field = conditions_fields[name]
scope = if value.is_a?(Puffer::Filters::Diapason)
case
when value.from.blank? then scope.where(["#{query_column(field)} < ?", value.till])
when value.till.blank? then scope.where(["#{query_column(field)} > ?", value.from])
else scope.where(name => Range.new(value.from, value.till))
end
else
scope.where(name => value)
end if field
end
scope
end
|