class SearchCopGrammar::Attributes::Collection

Constants

INCLUDED_OPERATORS

Attributes

key[R]
query_info[R]

Public Class Methods

new(query_info, key) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 10
def initialize(query_info, key)
  raise(SearchCop::UnknownColumn, "Unknown column #{key}") unless query_info.scope.reflection.attributes[key]

  @query_info = query_info
  @key = key
end

Public Instance Methods

==(other) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 21
def ==(other)
  other.is_a?(self.class) && [query_info.model, key] == [query_info.model, other.key]
end
alias_for(name) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 84
def alias_for(name)
  (query_info.scope.reflection.aliases[name] && name) || klass_for(name).table_name
end
attribute_for(attribute_definition) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 88
def attribute_for(attribute_definition)
  query_info.references.push attribute_definition

  table, column_with_fields = attribute_definition.split(".")
  column, *fields = column_with_fields.split("->")
  klass = klass_for(table)

  raise(SearchCop::UnknownAttribute, "Unknown attribute #{attribute_definition}") unless klass.columns_hash[column]

  Attributes.const_get(klass.columns_hash[column].type.to_s.classify).new(klass, alias_for(table), column, fields, options)
end
attributes() click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 61
def attributes
  @attributes ||= query_info.scope.reflection.attributes[key].collect { |attribute_definition| attribute_for attribute_definition }
end
compatible?(value) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 53
def compatible?(value)
  attributes.all? { |attribute| attribute.compatible? value }
end
eql?(other) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 17
def eql?(other)
  self == other
end
fulltext?() click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 49
def fulltext?
  (query_info.scope.reflection.options[key] || {})[:type] == :fulltext
end
generator(generator, value) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 35
def generator(generator, value)
  attributes.collect! do |attribute|
    SearchCopGrammar::Nodes::Generator.new(attribute, generator: generator, value: value)
  end.inject(:or)
end
generator_for(name) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 100
def generator_for(name)
  generators[name]
end
generators() click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 108
def generators
  query_info.scope.reflection.generators
end
hash() click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 25
def hash
  [query_info.model, key].hash
end
klass_for(name) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 74
def klass_for(name)
  alias_value = query_info.scope.reflection.aliases[name]

  return alias_value if alias_value.is_a?(Class)

  value = alias_value || name

  klass_for_association(value) || value.classify.constantize
end
klass_for_association(name) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 65
def klass_for_association(name)
  reflections = query_info.model.reflections

  return reflections[name].klass if reflections[name]
  return reflections[name.to_sym].klass if reflections[name.to_sym]

  nil
end
matches(value) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 41
def matches(value)
  if fulltext?
    SearchCopGrammar::Nodes::MatchesFulltext.new self, value.to_s
  else
    attributes.collect! { |attribute| attribute.matches value }.inject(:or)
  end
end
options() click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 57
def options
  query_info.scope.reflection.options[key]
end
valid_operator?(operator) click to toggle source
# File lib/search_cop_grammar/attributes.rb, line 104
def valid_operator?(operator)
  (INCLUDED_OPERATORS + generators.keys).include?(operator)
end