Class: Mongoid::Association::Embedded::EmbedsMany

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

Overview

The EmbedsMany 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.

%i[
  as
  cascade_callbacks
  cyclic
  order
  store_as
  before_add
  after_add
  before_remove
  after_remove
]
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, #owner_class, #parent_inclusions

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, #in_to?, #initialize, #inverse, #inverse_association, #inverse_class, #inverse_class_name, #inverse_setter, #inverse_type, #inverse_type_setter, #inverses, #many?, #one?, #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, #touchable?

Methods included from Constrainable

#convert_to_foreign_key

Instance Method Details

#criteria(base, target) ⇒ Object

Get a criteria object for searching given a parent and children documents.

Parameters:

  • base (Document)

    The base document.

  • target (Document)

    The children documents.



141
142
143
144
145
146
147
148
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 141

def criteria(base, target)
  criterion = klass.scoped
  criterion.embedded = true
  criterion.documents = target
  criterion.parent_document = base
  criterion.association = self
  apply_ordering(criterion)
end

#embedded?true

Is this association type embedded?

Returns:

  • (true)

    Always true.



64
65
66
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 64

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.



57
58
59
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 57

def key
  store_as.to_s
end

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

The nested builder object.

Parameters:

  • attributes (Hash)

    The attributes to use to build the association object.

  • options (Hash)

    The options for the association.

Returns:



120
121
122
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 120

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

#path(document) ⇒ Mongoid::Atomic::Paths::Embedded::Many

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:



133
134
135
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 133

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

#polymorphic?true | false

Is this association polymorphic?

Returns:

  • (true | false)

    Whether this association is polymorphic.



101
102
103
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 101

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

#primary_keynil

The primary key

Returns:

  • (nil)

    Not relevant for this association



89
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 89

def primary_key; end

#relationAssociation::Embedded::EmbedsMany::Proxy

Get the association proxy class for this association type.

Returns:



94
95
96
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 94

def relation
  Proxy
end

#setup!self

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

Returns:

  • (self)


40
41
42
43
44
45
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 40

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 list of association objects.

Returns:

  • (String)

    The field name.



50
51
52
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 50

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.



82
83
84
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 82

def stores_foreign_key?
  false
end

#typeString | nil

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.



110
111
112
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 110

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

#validation_defaulttrue

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)

    Always true.



75
76
77
# File 'lib/mongoid/association/embedded/embeds_many.rb', line 75

def validation_default
  true
end