class Mongoid::Association::Embedded::EmbedsOne

The EmbedsOne type association.

@since 7.0

Constants

ASSOCIATION_OPTIONS

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

@return [ Array<Symbol> ] The extra valid options.

@since 7.0

VALID_OPTIONS

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

@return [ Array<Symbol> ] The valid options.

@since 7.0

Public Instance Methods

embedded?() click to toggle source

Is this association type embedded?

@return [ true ] Always true.

@since 7.0

# File lib/mongoid/association/embedded/embeds_one.rb, line 76
def embedded?; true; end
key() click to toggle source

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

@return [ String ] The name of the field used to store the association.

@since 7.0

# File lib/mongoid/association/embedded/embeds_one.rb, line 67
def key
  store_as.to_s
end
nested_builder(attributes, options) click to toggle source

The nested builder object.

@param [ Hash ] attributes The attributes to use to build the association object. @param [ Hash ] options The options for the association.

@return [ Association::Nested::One ] The Nested Builder object.

@since 7.0

# File lib/mongoid/association/embedded/embeds_one.rb, line 138
def nested_builder(attributes, options)
  Nested::One.new(self, attributes, options)
end
polymorphic?() click to toggle source

Is this association polymorphic?

@return [ true, false ] Whether this association is polymorphic.

@since 7.0

# File lib/mongoid/association/embedded/embeds_one.rb, line 115
def polymorphic?
  @polymorphic ||= !!@options[:as]
end
primary_key() click to toggle source

The primary key

@return [ nil ] Not relevant for this association

# File lib/mongoid/association/embedded/embeds_one.rb, line 99
def primary_key; end
relation() click to toggle source

Get the association proxy class for this association type.

@return [ Association::Embedded::EmbedsMany::Proxy ] The proxy class.

@since 7.0

# File lib/mongoid/association/embedded/embeds_one.rb, line 106
def relation
  Proxy
end
setup!() click to toggle source

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

@return [ self ]

@since 7.0

# File lib/mongoid/association/embedded/embeds_one.rb, line 46
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_as() click to toggle source

The field key used to store the association object.

@return [ String ] The field name.

@since 7.0

# File lib/mongoid/association/embedded/embeds_one.rb, line 58
def store_as
  @store_as ||= (@options[:store_as].try(:to_s) || name.to_s)
end
stores_foreign_key?() click to toggle source

Does this association type store the foreign key?

@return [ false ] Always false.

@since 7.0

# File lib/mongoid/association/embedded/embeds_one.rb, line 94
def stores_foreign_key?; false; end
type() click to toggle source

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

@note Only relevant if the association is polymorphic.

@return [ String, nil ] The field for storing the associated object’s type.

@since 7.0

# File lib/mongoid/association/embedded/embeds_one.rb, line 126
def type
  @type ||= "#{as}_type" if polymorphic?
end
validation_default() click to toggle source

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

@example Get the validation default.

Proxy.validation_default

@return [ true, false ] The validation default.

@since 2.1.9

# File lib/mongoid/association/embedded/embeds_one.rb, line 87
def validation_default; true; end

Private Instance Methods

determine_inverses(other) click to toggle source
# File lib/mongoid/association/embedded/embeds_one.rb, line 162
def determine_inverses(other)
  matches = relation_class.relations.values.select do |rel|
    relation_complements.include?(rel.class) &&
      # https://jira.mongodb.org/browse/MONGOID-4882
      rel.relation_class_name.sub(/\A::/, '') == inverse_class_name

  end
  if matches.size > 1
    raise Errors::AmbiguousRelationship.new(relation_class, @owner_class, name, matches)
  end
  matches.collect { |m| m.name } unless matches.blank?
end
polymorphic_inverses(other = nil) click to toggle source
# File lib/mongoid/association/embedded/embeds_one.rb, line 158
def polymorphic_inverses(other = nil)
  [ as ]
end
relation_complements() click to toggle source
# File lib/mongoid/association/embedded/embeds_one.rb, line 154
def relation_complements
  @relation_complements ||= [ Embedded::EmbeddedIn ].freeze
end
setup_instance_methods!() click to toggle source
# File lib/mongoid/association/embedded/embeds_one.rb, line 144
def setup_instance_methods!
  define_getter!
  define_setter!
  define_existence_check!
  define_builder!
  define_creator!
  @owner_class.cyclic = true if cyclic?
  @owner_class.validates_associated(name) if validate?
end