Module: Wice::MongoidField

Defined in:
lib/mongoid_field.rb

Overview

to be mixed in into Mongoid::Field

Instance Method Summary collapse

Instance Method Details

#wice_add_filter_criteria(all_filter_params, criteria, custom_filter_active) ⇒ Object

:nodoc:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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/mongoid_field.rb', line 6

def wice_add_filter_criteria(all_filter_params, criteria, custom_filter_active)  #:nodoc:
  request_params = all_filter_params ? all_filter_params[name] : nil
  return nil unless request_params

  # Preprocess incoming parameters for datetime, if what's coming in is
  # a datetime (with custom_filter it can be anything else, and not
  # the datetime hash {"fr" => ..., "to" => ...})
  if (self.type == Time) && request_params.is_a?(Hash)
    ["fr", "to"].each do |sym|
      if request_params[sym]
        if request_params[sym].is_a?(String)
          request_params[sym] = Time.parse(Wice::Defaults::DATETIME_PARSER.call(request_params[sym]).to_s)
        elsif request_params[sym].is_a?(Hash)
          request_params[sym] = Time.parse(::Wice::GridTools.params_2_datetime(request_params[sym]).to_s)
        end
      end
    end
  end

  # Preprocess incoming parameters for date, if what's coming in is
  # a date (with custom_filter it can be anything else, and not
  # the date hash {"fr" => ..., "to" => ...})
  if self.type == Date && request_params.is_a?(Hash)
    ["fr", "to"].each do |sym|
      if request_params[sym]
        if request_params[sym].is_a?(String)
          request_params[sym] = Wice::Defaults::DATE_PARSER.call(request_params[sym]).to_time
        elsif request_params[sym].is_a?(Hash)
          request_params[sym] = ::Wice::GridTools.params_2_date(request_params[sym]).to_time
        end
      end
    end
  end

  processor_klass =  ::Wice::FilterConditionsGeneratorCustomFilter if custom_filter_active
  processor_klass = ::Wice::FilterConditionsGenerator.handled_type[self.type] unless processor_klass
  unless processor_klass
    Wice.log("No processor for database type #{self.type}!!!")
    return nil
  end
  processor_klass.new(self, criteria).generate_conditions(request_params)
end