Class: Mongoid::Relations::Referenced::One

Inherits:
One
  • Object
show all
Defined in:
build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb

Overview

This class defines the behaviour for all relations that are a one-to-one between documents in different collections.

Constant Summary collapse

VALID_OPTIONS =

The allowed options when defining this relation.

Returns:

  • (Array<Symbol>)

    The allowed options when defining this relation.

Since:

  • 6.0.0

[
  :as,
  :autobuild,
  :autosave,
  :dependent,
  :foreign_key,
  :primary_key
].freeze

Instance Attribute Summary

Attributes inherited from Proxy

#__metadata, #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, metadata) ⇒ One

Instantiate a new references_one relation. Will set the foreign key and the base on the inverse object.

Examples:

Create the new relation.

Referenced::One.new(base, target, )

Parameters:

  • base (Document)

    The document this relation hangs off of.

  • target (Document)

    The target (child) of the relation.

  • metadata (Metadata)

    The relation’s metadata.



33
34
35
36
37
38
39
40
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 33

def initialize(base, target, )
  init(base, target, ) do
    raise_mixed if klass.embedded? && !klass.cyclic?
    characterize_one(target)
    bind_one
    target.save if persistable?
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Proxy

Class Method Details

.builder(base, meta, object) ⇒ Builder

Return the builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the builder.

Referenced::One.builder(meta, object)

Parameters:

  • base (Document)

    The base document.

  • meta (Metadata)

    The metadata of the relation.

  • object (Document, Hash)

    A document or attributes to build with.

Returns:

  • (Builder)

    A new builder object.

Since:

  • 2.0.0.rc.1



119
120
121
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 119

def builder(base, meta, object)
  Builders::Referenced::One.new(base, meta, object)
end

.criteria(metadata, object, type = nil) ⇒ Criteria

Get the standard criteria used for querying this relation.

Examples:

Get the criteria.

Proxy.criteria(meta, id, Model)

Parameters:

  • metadata (Metadata)

    The metadata.

  • object (Object)

    The value of the foreign key.

  • type (Class) (defaults to: nil)

    The optional type.

Returns:

Since:

  • 2.1.0



135
136
137
138
139
140
141
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 135

def criteria(, object, type = nil)
  crit = .klass.where(.foreign_key => object)
  if .polymorphic?
    crit = crit.where(.type => type.name)
  end
  crit
end

.eager_load_klassObject



143
144
145
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 143

def eager_load_klass
  Relations::Eager::HasOne
end

.embedded?false

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

Examples:

Is this relation embedded?

Referenced::One.embedded?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0.rc.1



156
157
158
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 156

def embedded?
  false
end

.foreign_key(name) ⇒ String

Get the foreign key for the provided name.

Examples:

Get the foreign key.

Referenced::One.foreign_key(:person)

Parameters:

Returns:

  • (String)

    The foreign key.

Since:

  • 3.0.0



170
171
172
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 170

def foreign_key(name)
  "#{name}#{foreign_key_suffix}"
end

.foreign_key_defaultnil

Get the default value for the foreign key.

Examples:

Get the default.

Referenced::One.foreign_key_default

Returns:

  • (nil)

    Always nil.

Since:

  • 2.0.0.rc.1



182
183
184
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 182

def foreign_key_default
  nil
end

.foreign_key_suffixString

Returns the suffix of the foreign key field, either “_id” or “_ids”.

Examples:

Get the suffix for the foreign key.

Referenced::One.foreign_key_suffix

Returns:

  • (String)

    “_id”

Since:

  • 2.0.0.rc.1



194
195
196
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 194

def foreign_key_suffix
  "_id"
end

.macroSymbol

Returns the macro for this relation. Used mostly as a helper in reflection.

Examples:

Get the macro.

Referenced::One.macro

Returns:



205
206
207
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 205

def macro
  :has_one
end

.nested_builder(metadata, attributes, options) ⇒ NestedBuilder

Return the nested builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the nested builder.

Referenced::One.builder(attributes, options)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • attributes (Hash)

    The attributes to build with.

  • options (Hash)

    The options for the builder.

Options Hash (options):

  • :allow_destroy (true, false)

    Can documents be deleted?

  • :limit (Integer)

    Max number of documents to create at once.

  • :reject_if (Proc, Symbol)

    If documents match this option then they are ignored.

  • :update_only (true, false)

    Only existing documents can be modified.

Returns:

  • (NestedBuilder)

    A newly instantiated nested builder object.

Since:

  • 2.0.0.rc.1



231
232
233
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 231

def nested_builder(, attributes, options)
  Builders::NestedAttributes::One.new(, attributes, options)
end

.path(document) ⇒ Root

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:

  • (Root)

    The root atomic path calculator.

Since:

  • 2.1.0



245
246
247
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 245

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

.stores_foreign_key?false

Tells the caller if this relation is one that stores the foreign key on its own objects.

Examples:

Does this relation store a foreign key?

Referenced::One.stores_foreign_key?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0.rc.1



258
259
260
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 258

def stores_foreign_key?
  false
end

.valid_optionsArray<Symbol>

Get the valid options allowed with this relation.

Examples:

Get the valid options.

Relation.valid_options

Returns:

  • (Array<Symbol>)

    The valid options.

Since:

  • 2.1.0



270
271
272
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 270

def valid_options
  VALID_OPTIONS
end

.validation_defaulttrue, false

Get the default validation setting for the relation. Determines if by default a validates associated will occur.

Examples:

Get the validation default.

Proxy.validation_default

Returns:

  • (true, false)

    The validation default.

Since:

  • 2.1.9



283
284
285
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 283

def validation_default
  true
end

Instance Method Details

#nullifyObject

Removes the association between the base document and the target document by deleting the foreign key and the reference, orphaning the target document in the process.

Examples:

Nullify the relation.

person.game.nullify

Since:

  • 2.0.0.rc.1



50
51
52
53
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 50

def nullify
  unbind_one
  target.save
end

#substitute(replacement) ⇒ One

Substitutes the supplied target document for the existing document in the relation. If the new target is nil, perform the necessary deletion.

Examples:

Replace the relation.

person.game.substitute(new_game)

Parameters:

  • replacement (Array<Document>)

    The replacement target.

Returns:

  • (One)

    The relation.

Since:

  • 2.0.0.rc.1



67
68
69
70
71
72
73
74
75
76
77
# File 'build/mongoid-6.1/lib/mongoid/relations/referenced/one.rb', line 67

def substitute(replacement)
  unbind_one
  if persistable?
    if .destructive?
      send(.dependent)
    else
      save if persisted?
    end
  end
  One.new(base, replacement, ) if replacement
end