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

Returns a new instance of HashMerger.



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

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

  @relation = relation
  @hash     = hash
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash



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

def hash
  @hash
end

#relationObject (readonly)

Returns the value of attribute relation



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

def relation
  @relation
end

Instance Method Details

#mergeObject



16
17
18
# File 'activerecord/lib/active_record/relation/merger.rb', line 16

def merge
  Merger.new(relation, other).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.



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

def other
  other = Relation.create(relation.klass, relation.table)
  hash.each { |k, v|
    if k == :joins
      if Hash === v
        other.joins!(v)
      else
        other.joins!(*v)
      end
    elsif k == :select
      other._select!(v)
    else
      other.send("#{k}!", v)
    end
  }
  other
end