Class: ActiveRecord::Relation::WhereClause

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


10
11
12
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 10

def initialize(predicates)
  @predicates = predicates
end

Class Method Details

.emptyObject


92
93
94
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 92

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

Instance Method Details

#+(other) ⇒ Object


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

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

#-(other) ⇒ Object


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

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

#==(other) ⇒ Object


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

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

#astObject


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

def ast
  predicates = predicates_with_wrapped_sql_literals
  predicates.one? ? predicates.first : Arel::Nodes::And.new(predicates)
end

#contradiction?Boolean

Returns:

  • (Boolean)

96
97
98
99
100
101
102
103
104
105
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 96

def contradiction?
  predicates.any? do |x|
    case x
    when Arel::Nodes::In
      Array === x.right && x.right.empty?
    when Arel::Nodes::Equality
      x.right.respond_to?(:unboundable?) && x.right.unboundable?
    end
  end
end

#except(*columns) ⇒ Object


36
37
38
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 36

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

#extract_attributesObject


107
108
109
110
111
112
113
114
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 107

def extract_attributes
  predicates.each_with_object([]) do |node, attrs|
    attr = extract_attribute(node) || begin
      node.left if node.equality? && node.left.is_a?(Arel::Predications)
    end
    attrs << attr if attr
  end
end

#invert(as = :nand) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 80

def invert(as = :nand)
  if predicates.size == 1
    inverted_predicates = [ invert_predicate(predicates.first) ]
  elsif as == :nor
    inverted_predicates = predicates.map { |node| invert_predicate(node) }
  else
    inverted_predicates = [ Arel::Nodes::Not.new(ast) ]
  end

  WhereClause.new(inverted_predicates)
end

#merge(other, rewhere = nil) ⇒ Object


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

def merge(other, rewhere = nil)
  predicates = if rewhere
    except_predicates(other.extract_attributes)
  else
    predicates_unreferenced_by(other)
  end

  WhereClause.new(predicates | other.predicates)
end

#or(other) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 40

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

  if left.empty? || right.empty?
    common
  else
    left = left.ast
    left = left.expr if left.is_a?(Arel::Nodes::Grouping)

    right = right.ast
    right = right.expr if right.is_a?(Arel::Nodes::Grouping)

    or_clause = Arel::Nodes::Or.new(left, right)

    common.predicates << Arel::Nodes::Grouping.new(or_clause)
    common
  end
end

#to_h(table_name = nil) ⇒ Object


61
62
63
64
65
66
67
68
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 61

def to_h(table_name = nil)
  equalities(predicates).each_with_object({}) do |node, hash|
    next if table_name&.!= node.left.relation.name
    name = node.left.name.to_s
    value = extract_node_value(node.right)
    hash[name] = value
  end
end

#|(other) ⇒ Object


22
23
24
# File 'activerecord/lib/active_record/relation/where_clause.rb', line 22

def |(other)
  WhereClause.new(predicates | other.predicates)
end