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


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/locomotive/mongoid/patches.rb', line 70

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