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:
- 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.
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 a relation 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.
-
#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 relation.
-
#inverse_class_name ⇒ String
The class name of the object owning this relation.
-
#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 relation.
-
#inverses(other = nil) ⇒ Array<Symbol>
Get the inverse names.
-
#key ⇒ Symbol, String
The foreign key field if this relation stores a foreign key.
-
#klass ⇒ String
(also: #relation_class)
The class of the relation object(s).
-
#path(document) ⇒ Mongoid::Atomic::Paths::Root
The atomic path for this relation.
-
#relation_class_name ⇒ String
(also: #class_name)
The class name of the relation 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.
38 39 40 |
# File 'lib/mongoid/association/relatable.rb', line 38 def name @name end |
#options ⇒ Hash (readonly)
The options on this association.
45 46 47 |
# File 'lib/mongoid/association/relatable.rb', line 45 def @options end |
Instance Method Details
#==(other) ⇒ Object
Compare this association to another.
73 74 75 76 77 78 |
# File 'lib/mongoid/association/relatable.rb', line 73 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.
110 |
# File 'lib/mongoid/association/relatable.rb', line 110 def bindable?(doc); false; end |
#counter_cache_column_name ⇒ String
Get the counter cache column name.
287 288 289 290 291 |
# File 'lib/mongoid/association/relatable.rb', line 287 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 a relation proxy object using the owner and target.
269 270 271 |
# File 'lib/mongoid/association/relatable.rb', line 269 def create_relation(owner, target) relation.new(owner, target, self) end |
#destructive? ⇒ true, false
Whether the dependent method is destructive.
278 279 280 |
# File 'lib/mongoid/association/relatable.rb', line 278 def destructive? @destructive ||= !!(dependent && (dependent == :delete_all || dependent == :destroy)) end |
#extension ⇒ Module
Get the extension.
298 299 300 |
# File 'lib/mongoid/association/relatable.rb', line 298 def extension @extension ||= @options[:extend] end |
#foreign_key_check ⇒ String
Get the name of the method to check if the foreign key has changed.
256 257 258 |
# File 'lib/mongoid/association/relatable.rb', line 256 def foreign_key_check @foreign_key_check ||= "#{foreign_key}_changed?" if (stores_foreign_key? && foreign_key) end |
#foreign_key_setter ⇒ String
The name of the foreign key setter method.
220 221 222 223 224 |
# File 'lib/mongoid/association/relatable.rb', line 220 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.
88 89 90 |
# File 'lib/mongoid/association/relatable.rb', line 88 def get_callbacks(callback_type) Array([callback_type]) end |
#initialize(_class, name, opts = {}, &block) ⇒ Object
Initialize the Association.
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongoid/association/relatable.rb', line 55 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.
307 308 309 310 |
# File 'lib/mongoid/association/relatable.rb', line 307 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.
137 138 139 |
# File 'lib/mongoid/association/relatable.rb', line 137 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 relation.
182 183 184 |
# File 'lib/mongoid/association/relatable.rb', line 182 def inverse_class @owner_class end |
#inverse_class_name ⇒ String
The class name of the object owning this relation.
173 174 175 |
# File 'lib/mongoid/association/relatable.rb', line 173 def inverse_class_name @inverse_class_name ||= @owner_class.name end |
#inverse_setter(other = nil) ⇒ String
The name of the inverse setter method.
211 212 213 |
# File 'lib/mongoid/association/relatable.rb', line 211 def inverse_setter(other = nil) @inverse_setter ||= "#{inverses(other).first}=" unless inverses(other).blank? end |
#inverse_type ⇒ nil
Get the inverse type.
146 |
# File 'lib/mongoid/association/relatable.rb', line 146 def inverse_type; end |
#inverse_type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic relation.
244 245 246 |
# File 'lib/mongoid/association/relatable.rb', line 244 def inverse_type_setter @inverse_type_setter ||= inverse_type.__setter__ end |
#inverses(other = nil) ⇒ Array<Symbol>
Get the inverse names.
120 121 122 123 124 125 126 127 |
# File 'lib/mongoid/association/relatable.rb', line 120 def inverses(other = nil) return [ inverse_of ] if inverse_of if polymorphic? polymorphic_inverses(other) else determine_inverses(other) end end |
#key ⇒ Symbol, String
The foreign key field if this relation stores a foreign key. Otherwise, the primary key.
193 194 195 |
# File 'lib/mongoid/association/relatable.rb', line 193 def key stores_foreign_key? ? foreign_key : primary_key end |
#klass ⇒ String Also known as: relation_class
The class of the relation object(s).
163 164 165 |
# File 'lib/mongoid/association/relatable.rb', line 163 def klass @klass ||= relation_class_name.constantize end |
#path(document) ⇒ Mongoid::Atomic::Paths::Root
The atomic path for this relation.
231 232 233 |
# File 'lib/mongoid/association/relatable.rb', line 231 def path(document) relation.path(document) end |
#relation_class_name ⇒ String Also known as: class_name
The class name of the relation object(s).
153 154 155 |
# File 'lib/mongoid/association/relatable.rb', line 153 def relation_class_name @class_name ||= @options[:class_name] || ActiveSupport::Inflector.classify(name_with_module) end |
#setter ⇒ String
The name of the setter on this object for assigning an associated object.
202 203 204 |
# File 'lib/mongoid/association/relatable.rb', line 202 def setter @setter ||= "#{name}=" end |
#type_setter ⇒ String
Only relevant for polymorphic relations that take the :as option.
Get the type setter.
98 99 100 |
# File 'lib/mongoid/association/relatable.rb', line 98 def type_setter @type_setter ||= type.__setter__ end |
#validate? ⇒ true, false
Whether the associated object(s) should be validated.
318 319 320 321 322 323 324 |
# File 'lib/mongoid/association/relatable.rb', line 318 def validate? @validate ||= if @options[:validate].nil? validation_default else !!@options[:validate] end end |