Class: ActiveRecord::Relation::HashMerger

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, hash, rewhere = nil) ⇒ HashMerger

Returns a new instance of HashMerger.


10
11
12
13
14
15
16
# File 'activerecord/lib/active_record/relation/merger.rb', line 10

def initialize(relation, hash, rewhere = nil)
  hash.assert_valid_keys(*Relation::VALUE_METHODS)

  @relation = relation
  @hash     = hash
  @rewhere  = rewhere
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash


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

def hash
  @hash
end

#relationObject (readonly)

Returns the value of attribute relation


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

def relation
  @relation
end

Instance Method Details

#mergeObject


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

def merge
  Merger.new(relation, other, @rewhere).merge
end

#otherObject

Applying values to a relation has some side effects. E.g. interpolation might take place for where values. So we should build a relation to merge in rather than directly merging the values.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'activerecord/lib/active_record/relation/merger.rb', line 26

def other
  other = Relation.create(
    relation.klass,
    table: relation.table,
    predicate_builder: relation.predicate_builder
  )
  hash.each do |k, v|
    k = :_select if k == :select
    if Array === v
      other.public_send("#{k}!", *v)
    else
      other.public_send("#{k}!", v)
    end
  end
  other
end