Class: PgTaggable::PredicateHandler

Inherits:
ActiveRecord::PredicateBuilder::ArrayHandler
  • Object
show all
Defined in:
lib/pg_taggable/predicate_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate_builder, taggable_attributes) ⇒ PredicateHandler

Returns a new instance of PredicateHandler.



5
6
7
8
# File 'lib/pg_taggable/predicate_handler.rb', line 5

def initialize(predicate_builder, taggable_attributes)
  @taggable_attributes = taggable_attributes
  super(predicate_builder)
end

Instance Attribute Details

#taggable_attributesObject (readonly)

Returns the value of attribute taggable_attributes.



3
4
5
# File 'lib/pg_taggable/predicate_handler.rb', line 3

def taggable_attributes
  @taggable_attributes
end

Instance Method Details

#call(attribute, query) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pg_taggable/predicate_handler.rb', line 10

def call(attribute, query)
  taggable_attribute = taggable_attributes[attribute.name]
  if taggable_attribute
    attribute.name, type, operator = taggable_attribute
    query_attribute = ActiveRecord::Relation::QueryAttribute.new(attribute.name, query, type)
    bind_param = Arel::Nodes::BindParam.new(query_attribute)
    if operator == '='
      operator = '@>'
      Arel::Nodes::NamedFunction.new('ARRAY_LENGTH', [ attribute, 1 ]).eq(query.size).and(
        Arel::Nodes::InfixOperation.new(operator, attribute, bind_param)
      )
    else
      Arel::Nodes::InfixOperation.new(operator, attribute, bind_param)
    end
  else
    super(attribute, query)
  end
end