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.



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

def initialize(predicates)
  @predicates = predicates
end

Class Method Details

.emptyObject



90
91
92
# File 'lib/active_record/relation/where_clause.rb', line 90

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

Instance Method Details

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



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

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

#==(other) ⇒ Object



75
76
77
78
# File '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 '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)


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

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 'lib/active_record/relation/where_clause.rb', line 36

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

#extract_attributesObject



105
106
107
108
109
# File 'lib/active_record/relation/where_clause.rb', line 105

def extract_attributes
  attrs = []
  each_attributes { |attr, _| attrs << attr }
  attrs
end

#invertObject



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

def invert
  if predicates.size == 1
    inverted_predicates = [ invert_predicate(predicates.first) ]
  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 '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 '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, equality_only: false) ⇒ Object



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

def to_h(table_name = nil, equality_only: false)
  equalities(predicates, equality_only).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 'lib/active_record/relation/where_clause.rb', line 22

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