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
48
|
# File 'lib/puffer/orm_adapter/mongoid.rb', line 22
def filter scope, fields, options = {}
conditions, order = extract_conditions_and_order!(options)
order = order.map { |o| f = fields[o.first]; [query_order(f), o.last] if f && f.column }.compact
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.any_of(searches(search_fields, options[:search])) if options[:search].present?
scope = scope.order_by(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(name.to_sym.lt => value.till)
when value.till.blank? then scope.where(name.to_sym.gt => value.from)
else scope.where(name => Range.new(value.from, value.till))
end
else
scope.where(name => value)
end if field
end
scope
end
|