Class: ForestLiana::OperatorValueParser

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/operator_value_parser.rb

Class Method Summary collapse

Class Method Details

.format_value(resource, field, value) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/forest_liana/operator_value_parser.rb', line 72

def self.format_value(resource, field, value)
  if self.is_belongs_to(field)
    fields = field.split(':')
    columns = resource.reflect_on_association(fields[0]).klass.columns
    field_name = fields[1]
  else
    columns = resource.columns
    field_name = field
  end

  column = columns.find { |column| column.name == field_name }

  if column.type == :boolean
    ForestLiana::AdapterHelper.cast_boolean(value)
  else
    "'#{value}'"
  end
end

.get_condition(field, operator, value, resource, timezone) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/forest_liana/operator_value_parser.rb', line 38

def self.get_condition(field, operator, value, resource, timezone)
  field_name = self.get_field_name(field, resource)

  operator_date_interval_parser = OperatorDateIntervalParser
    .new(value, timezone)

  if operator_date_interval_parser.is_interval_date_value()
    filter = operator_date_interval_parser.get_interval_date_filter()
    "#{field_name} #{filter}"
  else
    # NOTICE: Set the integer value instead of a string if "enum" type
    # NOTICE: Rails 3 do not have a defined_enums method
    if resource.respond_to?(:defined_enums) &&
      resource.defined_enums.has_key?(field)
      value = resource.defined_enums[field][value]
    end

    where = "#{field_name} #{operator}"
    where += " #{self.format_value(resource, field, value)}" if value
    where
  end
end

.get_field_name(field, resource) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'app/services/forest_liana/operator_value_parser.rb', line 61

def self.get_field_name(field, resource)
  if self.is_belongs_to(field)
    association = field.split(':')[0].pluralize
    "#{ActiveRecord::Base.connection.quote_column_name(association)}." +
    "#{ActiveRecord::Base.connection.quote_column_name(field.split(':')[1])}"
  else
    "#{resource.quoted_table_name}." +
    "#{ActiveRecord::Base.connection.quote_column_name(field)}"
  end
end

.is_belongs_to(field) ⇒ Object



91
92
93
# File 'app/services/forest_liana/operator_value_parser.rb', line 91

def self.is_belongs_to(field)
  field.split(':').size >= 2
end

.parse(value) ⇒ Object



4
5
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
# File 'app/services/forest_liana/operator_value_parser.rb', line 4

def self.parse(value)
  operator = nil
  value_comparison = nil

  if value.first == '!' && value[1] != '*'
    operator = '!='
    value_comparison = value[1..-1]
  elsif value.first == '>'
    operator = '>'
    value_comparison = value[1..-1]
  elsif value.first == '<'
    operator = '<'
    value_comparison = value[1..-1]
  elsif value[0] == '!' && value[1] == '*' && value[-1] == '*'
    operator = 'NOT LIKE'
    value = value.delete('!')
    value_comparison = value.gsub('*', '%')
  elsif value[0] == '*' || value[-1] == '*'
    operator = 'LIKE'
    value_comparison = value.gsub('*', '%')
  elsif value === '$present'
    operator = 'IS NOT NULL'
    value_comparison = nil
  elsif value === '$blank'
    operator = 'IS NULL'
    value_comparison = nil
  else
    operator = '='
    value_comparison = value
  end

  [operator, value_comparison]
end