Module: Mongoid::Association::Relatable
- Includes:
- Constrainable, Options
- Included in:
- Embedded::EmbeddedIn, Embedded::EmbedsMany, Embedded::EmbedsOne, Mongoid::Association::Referenced::BelongsTo, Mongoid::Association::Referenced::HasAndBelongsToMany, Mongoid::Association::Referenced::HasMany, Mongoid::Association::Referenced::HasOne
- Defined in:
- build/mongoid-8.1/lib/mongoid/association/relatable.rb
Overview
This module provides behaviors shared between Association types.
Constant Summary collapse
- SHARED_OPTIONS =
The options shared between all association types.
[ :class_name, :inverse_of, :validate, :extend ].freeze
- PRIMARY_KEY_DEFAULT =
The primary key default.
'_id'.freeze
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
The name of the association.
-
#options ⇒ Hash
readonly
The options on this association.
-
#parent_inclusions ⇒ Array<String>
The associations above this one in the inclusion tree.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare this association to another.
-
#bindable?(doc) ⇒ true | false
Whether trying to bind an object using this association should raise an error.
-
#counter_cache_column_name ⇒ String
Get the counter cache column name.
-
#create_relation(owner, target) ⇒ Proxy
Create an association proxy object using the owner and target.
-
#destructive? ⇒ true | false
Whether the dependent method is destructive.
-
#extension ⇒ Module
Get the extension.
-
#foreign_key_check ⇒ String
Get the name of the method to check if the foreign key has changed.
-
#foreign_key_setter ⇒ String
The name of the foreign key setter method.
-
#get_callbacks(callback_type) ⇒ Array<Proc | Symbol>
Get the callbacks for a given type.
-
#in_to? ⇒ true | false
Is this association an embedded_in or belongs_to association?.
-
#initialize(_class, name, opts = {}, &block) ⇒ Object
Initialize the Association.
-
#inverse(other = nil) ⇒ Symbol
Get the inverse name.
-
#inverse_association(other = nil) ⇒ Association
Get the inverse’s association metadata.
-
#inverse_class ⇒ String
(also: #inverse_klass)
The class of the object owning this association.
-
#inverse_class_name ⇒ String
The class name of the object owning this association.
-
#inverse_setter(other = nil) ⇒ String
The name of the inverse setter method.
-
#inverse_type ⇒ nil
Get the inverse type.
-
#inverse_type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic association.
-
#inverses(other = nil) ⇒ Array<Symbol>
Get the inverse names.
-
#key ⇒ Symbol | String
The foreign key field if this association stores a foreign key.
-
#many? ⇒ true | false
Is this association an embeds_many or has_many association?.
-
#one? ⇒ true | false
Is this association an embeds_one or has_one association?.
-
#path(document) ⇒ Mongoid::Atomic::Paths::Root
The atomic path for this association.
-
#relation_class ⇒ String
(also: #klass)
The class of the association object(s).
-
#relation_class_name ⇒ String
(also: #class_name)
- The class name, possibly unqualified or
-
prefixed, of the association object(s).
-
#setter ⇒ String
The name of the setter on this object for assigning an associated object.
-
#type_setter ⇒ String
Get the type setter.
-
#validate? ⇒ true | false
Whether the associated object(s) should be validated.
Methods included from Options
#as, #autobuilding?, #autosave, #cascading_callbacks?, #counter_cached?, #cyclic?, #dependent, #forced_nil_inverse?, #indexed?, #inverse_of, #order, #polymorphic?, #primary_key, #store_as, #touch_field, #type
Methods included from Constrainable
Instance Attribute Details
#name ⇒ Symbol (readonly)
The name of the association.
32 33 34 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 32 def name @name end |
#options ⇒ Hash (readonly)
The options on this association.
37 38 39 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 37 def @options end |
#parent_inclusions ⇒ Array<String>
Returns The associations above this one in the inclusion tree.
303 304 305 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 303 def parent_inclusions @parent_inclusions end |
Instance Method Details
#==(other) ⇒ Object
Compare this association to another.
61 62 63 64 65 66 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 61 def ==(other) relation_class_name == other.relation_class_name && inverse_class_name == other.inverse_class_name && name == other.name && == other. end |
#bindable?(doc) ⇒ true | false
Whether trying to bind an object using this association should raise an error.
92 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 92 def bindable?(doc); false; end |
#counter_cache_column_name ⇒ String
Get the counter cache column name.
269 270 271 272 273 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 269 def counter_cache_column_name @counter_cache_column_name ||= (@options[:counter_cache].is_a?(String) || @options[:counter_cache].is_a?(Symbol)) ? @options[:counter_cache] : "#{inverse || inverse_class_name.demodulize.underscore.pluralize}_count" end |
#create_relation(owner, target) ⇒ Proxy
Create an association proxy object using the owner and target.
255 256 257 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 255 def create_relation(owner, target) relation.new(owner, target, self) end |
#destructive? ⇒ true | false
Whether the dependent method is destructive.
262 263 264 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 262 def destructive? @destructive ||= !!(dependent && (dependent == :delete_all || dependent == :destroy)) end |
#extension ⇒ Module
Get the extension.
278 279 280 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 278 def extension @extension ||= @options[:extend] end |
#foreign_key_check ⇒ String
Get the name of the method to check if the foreign key has changed.
244 245 246 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 244 def foreign_key_check @foreign_key_check ||= "#{foreign_key}_previously_changed?" if (stores_foreign_key? && foreign_key) end |
#foreign_key_setter ⇒ String
The name of the foreign key setter method.
214 215 216 217 218 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 214 def foreign_key_setter # note: You can't check if this association stores foreign key # See HasOne and HasMany binding, they referenced foreign_key_setter @foreign_key_setter ||= "#{foreign_key}=" if foreign_key end |
#get_callbacks(callback_type) ⇒ Array<Proc | Symbol>
Get the callbacks for a given type.
74 75 76 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 74 def get_callbacks(callback_type) Array([callback_type]) end |
#in_to? ⇒ true | false
Is this association an embedded_in or belongs_to association?
327 328 329 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 327 def in_to? [Referenced::BelongsTo, Embedded::EmbeddedIn].any? { |a| self.is_a?(a) } end |
#initialize(_class, name, opts = {}, &block) ⇒ Object
Initialize the Association.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 45 def initialize(_class, name, opts = {}, &block) @owner_class = _class @name = name @options = opts @extension = nil @module_path = _class.name ? _class.name.split('::')[0..-2].join('::') : '' @module_path << '::' unless @module_path.empty? create_extension!(&block) validate! end |
#inverse(other = nil) ⇒ Symbol
Get the inverse name.
285 286 287 288 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 285 def inverse(other = nil) candidates = inverses(other) candidates.detect { |c| c } if candidates end |
#inverse_association(other = nil) ⇒ Association
Get the inverse’s association metadata.
117 118 119 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 117 def inverse_association(other = nil) (other || relation_class).relations[inverse(other)] end |
#inverse_class ⇒ String Also known as: inverse_klass
The class of the object owning this association.
184 185 186 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 184 def inverse_class @owner_class end |
#inverse_class_name ⇒ String
The class name of the object owning this association.
177 178 179 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 177 def inverse_class_name @inverse_class_name ||= @owner_class.name end |
#inverse_setter(other = nil) ⇒ String
The name of the inverse setter method.
207 208 209 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 207 def inverse_setter(other = nil) @inverse_setter ||= "#{inverses(other).first}=" unless inverses(other).blank? end |
#inverse_type ⇒ nil
Get the inverse type.
124 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 124 def inverse_type; end |
#inverse_type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic association.
234 235 236 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 234 def inverse_type_setter @inverse_type_setter ||= inverse_type.__setter__ end |
#inverses(other = nil) ⇒ Array<Symbol>
Get the inverse names.
100 101 102 103 104 105 106 107 108 109 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 100 def inverses(other = nil) return [ inverse_of ] if inverse_of return [] if @options.key?(:inverse_of) && !inverse_of if polymorphic? polymorphic_inverses(other) else determine_inverses(other) end end |
#key ⇒ Symbol | String
The foreign key field if this association stores a foreign key. Otherwise, the primary key.
193 194 195 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 193 def key stores_foreign_key? ? foreign_key : primary_key end |
#many? ⇒ true | false
Is this association an embeds_many or has_many association?
312 313 314 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 312 def many? [Referenced::HasMany, Embedded::EmbedsMany].any? { |a| self.is_a?(a) } end |
#one? ⇒ true | false
Is this association an embeds_one or has_one association?
319 320 321 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 319 def one? [Referenced::HasOne, Embedded::EmbedsOne].any? { |a| self.is_a?(a) } end |
#path(document) ⇒ Mongoid::Atomic::Paths::Root
The atomic path for this association.
223 224 225 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 223 def path(document) relation.path(document) end |
#relation_class ⇒ String Also known as: klass
The class of the association object(s).
This method returns the class instance corresponding to relation_class_name
, resolved relative to the host document class.
If the class does not exist, this method raises NameError. This can happen because the target class has not yet been defined. Note that polymorphic associations generally do not have a well defined target class because the target class can change from one object to another, and calling this method on a polymorphic association will generally fail with a NameError or produce misleading results (if a class does happen to be defined with the same name as the association name).
166 167 168 169 170 171 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 166 def relation_class @klass ||= begin cls_name = @options[:class_name] || ActiveSupport::Inflector.classify(name) resolve_name(inverse_class, cls_name) end end |
#relation_class_name ⇒ String Also known as: class_name
The return value of this method should not be used to determine whether two associations have the same target class, because the return value is not always a fully qualified class name. To compare classes, retrieve the class instance of the association target using the relation_class
method.
- The class name, possibly unqualified or
-
prefixed, of the association
object(s).
This method returns the class name as it is used in the association definition. If :class_name option is given in the association, the exact value of that option is returned here. If :class_name option is not given, the name of the class is calculated from association name but is not resolved to the actual class.
The class name returned by this method may not correspond to a defined class, either because the corresponding class has not been loaded yet, or because the association references a non-existent class altogether. To obtain the association class, use relation_class
method.
147 148 149 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 147 def relation_class_name @class_name ||= @options[:class_name] || ActiveSupport::Inflector.classify(name) end |
#setter ⇒ String
The name of the setter on this object for assigning an associated object.
200 201 202 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 200 def setter @setter ||= "#{name}=" end |
#type_setter ⇒ String
Only relevant for polymorphic associations that take the :as option.
Get the type setter.
82 83 84 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 82 def type_setter @type_setter ||= type.__setter__ end |
#validate? ⇒ true | false
Whether the associated object(s) should be validated.
294 295 296 297 298 299 300 |
# File 'build/mongoid-8.1/lib/mongoid/association/relatable.rb', line 294 def validate? @validate ||= if @options[:validate].nil? validation_default else !!@options[:validate] end end |