Class: Mongoid::Relations::Embedded::Many

Inherits:
Relations::Many
  • Object
show all
Defined in:
lib/locomotive/mongoid/patches.rb

Instance Method Summary collapse

Instance Method Details

#with_same_class!(document, klass) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/locomotive/mongoid/patches.rb', line 48

def with_same_class!(document, klass)
  return document if document.is_a?(klass)

  Factory.build(klass, {}).tap do |_document|
    # make it part of the relationship
    _document.apply_post_processed_defaults
    integrate(_document)

    # make it look like the old document
    attributes = document.attributes
    attributes['_type'] = _document._type

    _document.instance_variable_set(:@attributes, attributes)
    _document.new_record  = document.new_record?
    _document._index      = document._index

    # finally, make the change in the lists
    target[_document._index]    = _document
    _unscoped[_document._index] = _document
  end
end