Class: ActiveRecord::PredicateBuilder

Inherits:
Object
  • Object
show all
Extended by:
Everywhere::Util
Includes:
Everywhere::Util
Defined in:
lib/everywhere/hash_key.rb,
lib/everywhere/hash_value.rb

Instance Method Summary collapse

Instance Method Details

#build_from_hash_with_not_and_like_and_not_like(attributes, default_table) ⇒ Object

< 3.0.13



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/everywhere/hash_key.rb', line 40

def build_from_hash_with_not_and_like_and_not_like(engine, attributes, default_table, allow_table_name = true)
  attributes_with_not_and_like_and_not_like = {}
  attributes.each do |column, value|
    # {not: {key: value}}
    if column.in?([:not, :like, :not_like])
      value.each do |k, v|
        attributes_with_not_and_like_and_not_like["#{k}__#{column}__"] = v
      end
    else
      attributes_with_not_and_like_and_not_like[column] = value
    end
  end
  build_from_hash_without_not_and_like_and_not_like(engine, attributes_with_not_and_like_and_not_like, default_table, allow_table_name).map do |rel|
    if rel.left.name.to_s.ends_with? '__not__'
      rel.left.name = rel.left.name.to_s.sub(/__not__$/, '').to_sym
      negate rel
    elsif rel.left.name.to_s.ends_with? '__like__'
      rel.left.name = rel.left.name.to_s.sub(/__like__$/, '').to_sym
      Arel::Nodes::Matches.new rel.left, rel.right
    elsif rel.left.name.to_s.ends_with? '__not_like__'
      rel.left.name = rel.left.name.to_s.sub(/__not_like__$/, '').to_sym
      Arel::Nodes::DoesNotMatch.new rel.left, rel.right
    else
      rel
    end
  end
end