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

Inherits:
Object
  • Object
show all
Includes:
Buildable, Relatable
Defined in:
build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb,
build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one/proxy.rb,
build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one/binding.rb,
build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one/buildable.rb

Overview

The EmbedsOne type association.

Defined Under Namespace

Modules: Buildable Classes: Binding, Proxy

Constant Summary collapse

ASSOCIATION_OPTIONS =

The options available for this type of association, in addition to the common ones.

Returns:

  • (Array<Symbol>)

    The extra valid options.

[
    :autobuild,
    :as,
    :cascade_callbacks,
    :cyclic,
    :store_as
]
VALID_OPTIONS =

The complete list of valid options for this association, including the shared ones.

Returns:

  • (Array<Symbol>)

    The valid options.

(ASSOCIATION_OPTIONS + SHARED_OPTIONS).freeze

Constants included from Relatable

Relatable::PRIMARY_KEY_DEFAULT, Relatable::SHARED_OPTIONS

Instance Attribute Summary

Attributes included from Relatable

#name, #options

Instance Method Summary collapse

Methods included from Buildable

#build

Methods included from Relatable

#==, #bindable?, #counter_cache_column_name, #create_relation, #destructive?, #extension, #foreign_key_check, #foreign_key_setter, #get_callbacks, #initialize, #inverse, #inverse_association, #inverse_class, #inverse_class_name, #inverse_setter, #inverse_type, #inverse_type_setter, #inverses, #path, #relation_class, #relation_class_name, #setter, #type_setter, #validate?

Methods included from Options

#as, #autobuilding?, #autosave, #cascading_callbacks?, #counter_cached?, #cyclic?, #dependent, #forced_nil_inverse?, #indexed?, #inverse_of, #order, #touch_field

Methods included from Constrainable

#convert_to_foreign_key

Instance Method Details

#embedded?true

Is this association type embedded?

Returns:

  • (true)

    Always true.



61
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 61

def embedded?; true; end

#keyString

The key that is used to get the attributes for the associated object.

Returns:

  • (String)

    The name of the field used to store the association.



54
55
56
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 54

def key
  store_as.to_s
end

#nested_builder(attributes, options) ⇒ Association::Nested::One

The nested builder object.

Parameters:

  • attributes (Hash)

    The attributes to use to build the association object.

  • options (Hash)

    The options for the association.

Returns:



111
112
113
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 111

def nested_builder(attributes, options)
  Nested::One.new(self, attributes, options)
end

#polymorphic?true, false

Is this association polymorphic?

Returns:

  • (true, false)

    Whether this association is polymorphic.



92
93
94
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 92

def polymorphic?
  @polymorphic ||= !!@options[:as]
end

#primary_keynil

The primary key

Returns:

  • (nil)

    Not relevant for this association



80
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 80

def primary_key; end

#relationAssociation::Embedded::EmbedsMany::Proxy

Get the association proxy class for this association type.

Returns:



85
86
87
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 85

def relation
  Proxy
end

#setup!self

Setup the instance methods, fields, etc. on the association owning class.

Returns:

  • (self)


37
38
39
40
41
42
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 37

def setup!
  setup_instance_methods!
  @owner_class.embedded_relations = @owner_class.embedded_relations.merge(name => self)
  @owner_class.aliased_fields[name.to_s] = store_as if store_as
  self
end

#store_asString

The field key used to store the association object.

Returns:

  • (String)

    The field name.



47
48
49
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 47

def store_as
  @store_as ||= (@options[:store_as].try(:to_s) || name.to_s)
end

#stores_foreign_key?false

Does this association type store the foreign key?

Returns:

  • (false)

    Always false.



75
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 75

def stores_foreign_key?; false; end

#typeString?

Note:

Only relevant if the association is polymorphic.

The field used to store the type of the related object.

Returns:

  • (String, nil)

    The field for storing the associated object's type.



101
102
103
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 101

def type
  @type ||= "#{as}_type" if polymorphic?
end

#validation_defaulttrue, false

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

Examples:

Get the validation default.

Proxy.validation_default

Returns:

  • (true, false)

    The validation default.



70
# File 'build/mongoid-7.4/lib/mongoid/association/embedded/embeds_one.rb', line 70

def validation_default; true; end