Class: ActiveRecord::Relation::WhereClause

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/relation/where_clause.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicates, binds) ⇒ WhereClause

Returns a new instance of WhereClause.



8
9
10
11
# File 'lib/active_record/relation/where_clause.rb', line 8

def initialize(predicates, binds)
  @predicates = predicates
  @binds = binds
end

Instance Attribute Details

#bindsObject (readonly)

Returns the value of attribute binds.



4
5
6
# File 'lib/active_record/relation/where_clause.rb', line 4

def binds
  @binds
end

Class Method Details

.emptyObject



80
81
82
# File 'lib/active_record/relation/where_clause.rb', line 80

def self.empty
  @empty ||= new([], [])
end

Instance Method Details

#+(other) ⇒ Object



13
14
15
16
17
18
# File 'lib/active_record/relation/where_clause.rb', line 13

def +(other)
  WhereClause.new(
    predicates + other.predicates,
    binds + other.binds,
  )
end

#==(other) ⇒ Object



70
71
72
73
74
# File 'lib/active_record/relation/where_clause.rb', line 70

def ==(other)
  other.is_a?(WhereClause) &&
    predicates == other.predicates &&
    binds == other.binds
end

#astObject



66
67
68
# File 'lib/active_record/relation/where_clause.rb', line 66

def ast
  Arel::Nodes::And.new(predicates_with_wrapped_sql_literals)
end

#except(*columns) ⇒ Object



27
28
29
# File 'lib/active_record/relation/where_clause.rb', line 27

def except(*columns)
  WhereClause.new(*except_predicates_and_binds(columns))
end

#invertObject



76
77
78
# File 'lib/active_record/relation/where_clause.rb', line 76

def invert
  WhereClause.new(inverted_predicates, binds)
end

#merge(other) ⇒ Object



20
21
22
23
24
25
# File 'lib/active_record/relation/where_clause.rb', line 20

def merge(other)
  WhereClause.new(
    predicates_unreferenced_by(other) + other.predicates,
    non_conflicting_binds(other) + other.binds,
  )
end

#or(other) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_record/relation/where_clause.rb', line 31

def or(other)
  if empty?
    self
  elsif other.empty?
    other
  else
    WhereClause.new(
      [ast.or(other.ast)],
      binds + other.binds
    )
  end
end

#to_h(table_name = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_record/relation/where_clause.rb', line 44

def to_h(table_name = nil)
  equalities = predicates.grep(Arel::Nodes::Equality)
  if table_name
    equalities = equalities.select do |node|
      node.left.relation.name == table_name
    end
  end

  binds = self.binds.map { |attr| [attr.name, attr.value] }.to_h

  equalities.map { |node|
    name = node.left.name
    [name, binds.fetch(name.to_s) {
      case node.right
      when Array then node.right.map(&:val)
      when Arel::Nodes::Casted, Arel::Nodes::Quoted
        node.right.val
      end
    }]
  }.to_h
end