Class: Mongoid::Association::Embedded::EmbedsOne::Proxy

Inherits:
One
  • Object
show all
Defined in:
lib/mongoid/association/embedded/embeds_one/proxy.rb

Constant Summary collapse

VALID_OPTIONS =

The valid options when defining this association.

Returns:

  • (Array<Symbol>)

    The allowed options when defining this association.

[
    :autobuild,
    :as,
    :cascade_callbacks,
    :cyclic,
    :store_as
].freeze

Instance Attribute Summary

Attributes inherited from Proxy

#_association, #_base, #_target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from One

#__evolve_object_id__, #clear, #in_memory, #respond_to?

Methods inherited from Proxy

apply_ordering, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable

Methods included from Marshalable

#marshal_dump, #marshal_load

Constructor Details

#initialize(base, target, association) ⇒ Proxy

Instantiate a new embeds_one association.

Examples:

Create the new proxy.

One.new(person, name, association)

Parameters:



29
30
31
32
33
34
35
36
37
38
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 29

def initialize(base, target, association)
  init(base, target, association) do
    characterize_one(_target)
    bind_one
    characterize_one(_target)
    update_attributes_hash(_target)
    _base._reset_memoized_descendants!
    _target.save if persistable?
  end
end

Class Method Details

.eager_loader(associations, docs) ⇒ Mongoid::Association::Embedded::Eager

Returns the eager loader for this association.

Parameters:

  • associations (Array<Mongoid::Association>)

    The associations to be eager loaded

  • docs (Array<Mongoid::Document>)

    The parent documents that possess the given associations, which ought to be populated by the eager-loaded documents.

Returns:



151
152
153
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 151

def eager_loader(associations, docs)
  Eager.new(associations, docs)
end

.embedded?true

Returns true if the association is an embedded one. In this case always true.

Examples:

Is this association embedded?

Association::Embedded::EmbedsOne.embedded?

Returns:

  • (true)

    true.



162
163
164
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 162

def embedded?
  true
end

.path(document) ⇒ Mongoid::Atomic::Paths::Embedded::One

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

Proxy.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:



175
176
177
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 175

def path(document)
  Mongoid::Atomic::Paths::Embedded::One.new(document)
end

Instance Method Details

#substitute(replacement) ⇒ Document | nil

Substitutes the supplied target documents for the existing document in the association.

Examples:

Substitute the new document.

person.name.substitute(new_name)

Parameters:

  • replacement (Document | Hash)

    A document to replace the target.

Returns:

  • (Document | nil)

    The association or nil.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 49

def substitute(replacement)
  if replacement != self
    if _assigning?
      _base.add_atomic_unset(_target) unless replacement
    else
      # The associated object will be replaced by the below update if non-nil, so only
      # run the callbacks and state-changing code by passing persist: false in that case.
      _target.destroy(persist: !replacement) if persistable?

      # A little explanation on why this is needed... Say we have three assignments:
      #
      # canvas.palette = palette
      # canvas.palette = nil
      # canvas.palette = palette
      # Where canvas embeds_one palette.
      #
      # Previously, what was happening was, on the first assignment,
      # palette was considered a "new record" (new_record?=true) and
      # thus palette was being inserted into the database. However,
      # on the third assignment, we're trying to reassign the palette,
      # palette is no longer considered a new record, because it had
      # been inserted previously. This is not exactly accurate,
      # because the second assignment ultimately removed the palette
      # from the database, so it needs to be reinserted. Since the
      # palette's new_record is false, Mongoid ends up "updating" the
      # document, which doesn't reinsert it into the database.
      #
      # The change I introduce here, respecifies palette as a "new
      # record" when it gets removed from the database, so if it is
      # reassigned, it will be reinserted into the database.
      _target.new_record = true
    end
    unbind_one
    unless replacement
      update_attributes_hash(replacement)

      # when `touch: true` is the default (see MONGOID-5016), creating
      # an embedded document will touch the parent, and will cause the
      # _descendants list to be initialized and memoized. If the object
      # is then deleted, we need to make sure and un-memoize that list,
      # otherwise when the update happens, the memoized _descendants list
      # gets used and the "deleted" subdocument gets added again.
      _reset_memoized_descendants!

      return nil
    end
    replacement = Factory.build(klass, replacement) if replacement.is_a?(::Hash)
    self._target = replacement
    characterize_one(_target)
    bind_one
    update_attributes_hash(_target)
    _target.save if persistable?
  end
  self
end