Class: ActiveRecord::Relation::WhereClause
- Inherits:
-
Object
- Object
- ActiveRecord::Relation::WhereClause
- Defined in:
- lib/active_record/relation/where_clause.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#binds ⇒ Object
readonly
Returns the value of attribute binds.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Object
- #ast ⇒ Object
- #except(*columns) ⇒ Object
-
#initialize(predicates, binds) ⇒ WhereClause
constructor
A new instance of WhereClause.
- #invert ⇒ Object
- #merge(other) ⇒ Object
- #or(other) ⇒ Object
- #to_h(table_name = nil) ⇒ Object
Constructor Details
#initialize(predicates, binds) ⇒ WhereClause
Returns a new instance of WhereClause.
8 9 10 11 |
# File 'lib/active_record/relation/where_clause.rb', line 8 def initialize(predicates, binds) @predicates = predicates @binds = binds end |
Instance Attribute Details
#binds ⇒ Object (readonly)
Returns the value of attribute binds.
4 5 6 |
# File 'lib/active_record/relation/where_clause.rb', line 4 def binds @binds end |
Class Method Details
.empty ⇒ Object
83 84 85 |
# File '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 '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 'lib/active_record/relation/where_clause.rb', line 73 def ==(other) other.is_a?(WhereClause) && predicates == other.predicates && binds == other.binds end |
#ast ⇒ Object
69 70 71 |
# File '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 'lib/active_record/relation/where_clause.rb', line 27 def except(*columns) WhereClause.new( predicates_except(columns), binds_except(columns), ) end |
#invert ⇒ Object
79 80 81 |
# File '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 '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 '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 '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 |