Class: ActiveRecord::Relation::WhereClause

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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


8
9
10
11
# File 'activerecord/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 'activerecord/lib/active_record/relation/where_clause.rb', line 4

def binds
  @binds
end

Class Method Details

.emptyObject


83
84
85
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 83

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

Instance Method Details

#+(other) ⇒ Object


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

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

#==(other) ⇒ Object


73
74
75
76
77
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 73

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

#astObject


69
70
71
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 69

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

#except(*columns) ⇒ Object


27
28
29
30
31
32
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 27

def except(*columns)
  WhereClause.new(
    predicates_except(columns),
    binds_except(columns),
  )
end

#invertObject


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

def invert
  WhereClause.new(inverted_predicates, binds)
end

#merge(other) ⇒ Object


20
21
22
23
24
25
# File 'activerecord/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


34
35
36
37
38
39
40
41
42
43
44
45
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 34

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


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

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