Class: QueryReport::FilterModule::Comparator

Inherits:
Object
  • Object
show all
Defined in:
lib/query_report/comparator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter, type, name, default = nil) ⇒ Comparator

Returns a new instance of Comparator.



9
10
11
# File 'lib/query_report/comparator.rb', line 9

def initialize(filter, type, name, default=nil)
  @filter, @type, @name, @default = filter, type, name, default
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



7
8
9
# File 'lib/query_report/comparator.rb', line 7

def default
  @default
end

#filterObject (readonly)

Returns the value of attribute filter.



7
8
9
# File 'lib/query_report/comparator.rb', line 7

def filter
  @filter
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/query_report/comparator.rb', line 7

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/query_report/comparator.rb', line 7

def type
  @type
end

Instance Method Details

#has_default?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/query_report/comparator.rb', line 25

def has_default?
  !@default.nil?
end

#objectified_param_valueObject

convert param value which is a string to object like date and boolean



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/query_report/comparator.rb', line 41

def objectified_param_value
  @objectified_param_value ||= case @filter.type
                                 when :date
                                   format = I18n.t("date.formats.#{QueryReport.config.date_format}")
                                   param_value.kind_of?(String) ? (Date.strptime(param_value, format) rescue param_value) : param_value
                                 when :datetime
                                   param_value.kind_of?(String) ? Time.zone.parse(param_value) : param_value
                                 when :boolean
                                   param_value.to_boolean
                                 else
                                   param_value
                               end
end

#param_valueObject



21
22
23
# File 'lib/query_report/comparator.rb', line 21

def param_value
  @filter.params[@filter.params_key] ? @filter.params[@filter.params_key][search_key] : stringified_default
end

#search_keyObject



13
14
15
# File 'lib/query_report/comparator.rb', line 13

def search_key
  "#{@filter.column.to_s}_#{@type}".to_sym
end

#search_tag_nameObject



17
18
19
# File 'lib/query_report/comparator.rb', line 17

def search_tag_name
  "#{@filter.params_key}[#{search_key.to_s}]"
end

#stringified_defaultObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/query_report/comparator.rb', line 29

def stringified_default
  @stringified_default ||= case @filter.type
                             when :date
                               @default.kind_of?(String) ? @default : (I18n.l(@default, format: QueryReport.config.date_format) rescue @default)
                             when :datetime
                               @default.kind_of?(String) ? @default : (I18n.l(@default, format: QueryReport.config.datetime_format) rescue @default)
                             else
                               @default.to_s
                           end
end