Class: ForestLiana::OperatorValueParser
- Inherits:
-
Object
- Object
- ForestLiana::OperatorValueParser
- Defined in:
- app/services/forest_liana/operator_value_parser.rb
Class Method Summary collapse
- .format_value(resource, field, value) ⇒ Object
- .get_association_name_for_condition(resource, field) ⇒ Object
- .get_condition(field, operator, value, resource, timezone) ⇒ Object
- .get_condition_end(field, operator, value, resource, timezone) ⇒ Object
- .get_field_name(field, resource) ⇒ Object
- .get_has_one_condition(resource, field, value, timezone) ⇒ Object
- .is_belongs_to(field) ⇒ Object
- .parse(value) ⇒ Object
Class Method Details
.format_value(resource, field, value) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/services/forest_liana/operator_value_parser.rb', line 90 def self.format_value(resource, field, value) columns = resource.columns field_name = field column_found = columns.find { |column| column.name == field_name } if column_found.nil? raise ForestLiana::Errors::HTTP422Error.new("Field '#{field_name}' not found") end if column_found.type == :boolean ForestLiana::AdapterHelper.cast_boolean(value) else "'#{value}'" end end |
.get_association_name_for_condition(resource, field) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/services/forest_liana/operator_value_parser.rb', line 124 def self.get_association_name_for_condition(resource, field) field, subfield = field.split(':') association = resource.reflect_on_association(field.to_sym) return nil if association.blank? tables_associated_to_relations_name = ForestLiana::QueryHelper.get_tables_associated_to_relations_name(resource) association_name = association.name.to_s association_name_pluralized = association_name.pluralize if [association_name, association_name_pluralized].include? association.table_name # NOTICE: Default case. When the belongsTo association name and the referenced table name # are identical. association_name_for_condition = association.table_name else # NOTICE: When the the belongsTo association name and the referenced table name are not # identical. Format with the ActiveRecord query generator style. relations_on_this_table = tables_associated_to_relations_name[association.table_name] has_several_associations_to_the_table_and_is_not_first_one = !relations_on_this_table.nil? && relations_on_this_table.size > 1 && relations_on_this_table.find_index(association.name) > 0 if has_several_associations_to_the_table_and_is_not_first_one association_name_for_condition = "#{association_name_pluralized}_#{resource.table_name}" else association_name_for_condition = association.table_name end 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 |
# 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) if self.is_belongs_to(field) fieldSplit = field.split(':') association = fieldSplit.first.to_sym field = fieldSplit.last resource_association = resource.reflect_on_association(association) if resource_association.nil? raise ForestLiana::Errors::HTTP422Error.new("Association '#{association.to_s}' not found") end resource = resource.reflect_on_association(association).klass end "#{field_name} #{self.get_condition_end(field, operator, value, resource, timezone)}" end |
.get_condition_end(field, operator, value, resource, timezone) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/services/forest_liana/operator_value_parser.rb', line 57 def self.get_condition_end(field, operator, value, resource, timezone) 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() 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 if value "#{operator} #{self.format_value(resource, field, value)}" else operator end end end |
.get_field_name(field, resource) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'app/services/forest_liana/operator_value_parser.rb', line 79 def self.get_field_name(field, resource) if self.is_belongs_to(field) association = self.get_association_name_for_condition(resource, field) "#{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 |
.get_has_one_condition(resource, field, value, timezone) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/services/forest_liana/operator_value_parser.rb', line 110 def self.get_has_one_condition(resource, field, value, timezone) field, subfield = field.split(':') association = resource.reflect_on_association(field.to_sym) return nil if association.blank? operator, value = OperatorValueParser.parse(value) filter = OperatorValueParser .get_condition_end(subfield, operator, value, association.klass, timezone) association_name_for_condition = self.get_association_name_for_condition(resource, field) association_name_for_condition ? "#{association_name_for_condition}.#{subfield} #{filter}" : nil end |
.is_belongs_to(field) ⇒ Object
106 107 108 |
# File 'app/services/forest_liana/operator_value_parser.rb', line 106 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 |