Module: BlindIndex::Extensions::PredicateBuilder

Defined in:
lib/blind_index/extensions.rb

Instance Method Summary collapse

Instance Method Details

#build(attribute, value, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blind_index/extensions.rb', line 15

def build(attribute, value, *args)
  if table.has_blind_indexes? && (bi = table.send(:klass).blind_indexes[attribute.name.to_sym]) && !value.is_a?(ActiveRecord::StatementCache::Substitute)
    model = table.send(:klass)
    attribute_name = attribute.name.to_sym
    cast =
      if model.respond_to?(:normalized_attributes) && model.normalized_attributes.include?(attribute_name)
        ->(v) { model.normalize_value_for(attribute_name, v) }
      else
        ->(v) { v }
      end
    attribute = attribute.relation[bi[:bidx_attribute]]
    value =
      if value.is_a?(Array) || (defined?(Set) && value.is_a?(Set))
        value.map { |v| BlindIndex.generate_bidx(cast.call(v), **bi) }
      else
        BlindIndex.generate_bidx(cast.call(value), **bi)
      end
  end

  super(attribute, value, *args)
end