Class: Wice::ActiveRecordColumnWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/wice/active_record_column_wrapper.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(column, all_filter_params, main_table, table_alias, custom_filter_active, filter_type) ⇒ ActiveRecordColumnWrapper

:nodoc:



12
13
14
15
16
17
# File 'lib/wice/active_record_column_wrapper.rb', line 12

def initialize(column, all_filter_params, main_table, table_alias, custom_filter_active, filter_type) #:nodoc:
  @column = column
  @filter_type = filter_type
  @all_filter_params, @main_table, @table_alias, @custom_filter_active =
    all_filter_params, main_table, table_alias, custom_filter_active
end

Instance Method Details

#alias_or_table_name(table_alias) ⇒ Object

:nodoc:



97
98
99
# File 'lib/wice/active_record_column_wrapper.rb', line 97

def alias_or_table_name(table_alias) #:nodoc:
  table_alias || @column.model.table_name
end

#modelObject

:nodoc:



92
93
94
# File 'lib/wice/active_record_column_wrapper.rb', line 92

def model #:nodoc:
  @column.model
end

#nameObject

:nodoc:



88
89
90
# File 'lib/wice/active_record_column_wrapper.rb', line 88

def name #:nodoc:
  @column.name
end

#wg_generate_conditionsObject

:nodoc:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wice/active_record_column_wrapper.rb', line 67

def wg_generate_conditions  #:nodoc:
  return nil if @request_params.nil?

  if @custom_filter_active
    custom_processor_klass = ::Wice::Columns.get_conditions_generator_column_processor(:custom)
    custom_processor = custom_processor_klass.new(self)
    return custom_processor.generate_conditions(@table_alias, @request_params)
  end

  column_type = @filter_type || @column.type.to_s

  processor_class = ::Wice::Columns.get_conditions_generator_column_processor(column_type)

  if processor_class
    return processor_class.new(self).generate_conditions(@table_alias, @request_params)
  else
    Wice.log("No processor for database type #{column_type}!!!")
    nil
  end
end

#wg_initialize_request_parametersObject

:nodoc:



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wice/active_record_column_wrapper.rb', line 20

def wg_initialize_request_parameters  #:nodoc:
  @request_params = nil
  return if @all_filter_params.nil?

  # if the parameter does not specify the table name we only allow columns in the default table to use these parameters
  if @main_table && @request_params  = @all_filter_params[@column.name]
    current_parameter_name = @column.name
  elsif @request_params = @all_filter_params[alias_or_table_name(@table_alias) + '.' + @column.name]
    current_parameter_name = alias_or_table_name(@table_alias) + '.' + @column.name
  end

  # 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 @request_params
    if (@column.type == :datetime || @column.type == :timestamp) && @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::ConfigurationProvider.value_for(:DATETIME_PARSER).call(@request_params[sym])
          elsif @request_params[sym].is_a?(Hash)
            @request_params[sym] = Wice::GridTools.params_2_datetime(@request_params[sym])
          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 @column.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::ConfigurationProvider.value_for(:DATE_PARSER).call(@request_params[sym])
          elsif @request_params[sym].is_a?(Hash)
            @request_params[sym] = ::Wice::GridTools.params_2_date(@request_params[sym])
          end
        end
      end
    end
  end

  return wg_generate_conditions, current_parameter_name
end