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



95
96
97
# File 'lib/active_record/relation/where_clause.rb', line 95

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 Also known as: eql?



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)


99
100
101
102
103
104
105
106
107
108
# File 'lib/active_record/relation/where_clause.rb', line 99

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



32
33
34
# File 'lib/active_record/relation/where_clause.rb', line 32

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

#extract_attributesObject



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

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

#hashObject



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

def hash
  [self.class, predicates].hash
end

#invertObject



85
86
87
88
89
90
91
92
93
# File 'lib/active_record/relation/where_clause.rb', line 85

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) ⇒ Object



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

def merge(other)
  predicates = except_predicates(other.extract_attributes)

  WhereClause.new(predicates | other.predicates)
end

#or(other) ⇒ Object



36
37
38
39
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 36

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 = if left.is_a?(Arel::Nodes::Or)
      Arel::Nodes::Or.new(left.children + [right])
    else
      Arel::Nodes::Or.new([left, right])
    end

    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