Class: ActiveRecord::Relation::WhereClause

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

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicates) ⇒ WhereClause

Returns a new instance of WhereClause.



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

def initialize(predicates)
  @predicates = predicates
end

Class Method Details

.emptyObject



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

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

Instance Method Details

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



18
19
20
21
22
# File 'lib/active_record/relation/where_clause.rb', line 18

def -(other)
  WhereClause.new(
    predicates - other.predicates,
  )
end

#==(other) ⇒ Object



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

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

#astObject



64
65
66
# File 'lib/active_record/relation/where_clause.rb', line 64

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

#except(*columns) ⇒ Object



30
31
32
# File 'lib/active_record/relation/where_clause.rb', line 30

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

#invertObject



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

def invert
  WhereClause.new(inverted_predicates)
end

#merge(other) ⇒ Object



24
25
26
27
28
# File 'lib/active_record/relation/where_clause.rb', line 24

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

#or(other) ⇒ Object



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

def or(other)
  left = self - other
  common = self - left
  right = other - common

  if left.empty? || right.empty?
    common
  else
    or_clause = WhereClause.new(
      [left.ast.or(right.ast)],
    )
    common + or_clause
  end
end

#to_h(table_name = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_record/relation/where_clause.rb', line 49

def to_h(table_name = nil)
  equalities = equalities(predicates)
  if table_name
    equalities = equalities.select do |node|
      node.left.relation.name == table_name
    end
  end

  equalities.map { |node|
    name = node.left.name.to_s
    value = extract_node_value(node.right)
    [name, value]
  }.to_h
end