Class: Mongoid::Relations::Metadata
- Inherits:
-
Hash
- Object
- Hash
- Mongoid::Relations::Metadata
- Defined in:
- lib/mongoid/relations/metadata.rb
Overview
The “Grand Poobah” of information about any relation is this class. It contains everything you could ever possible want to know.
Instance Method Summary collapse
-
#as ⇒ true, false
Returns the as option of the relation.
-
#as? ⇒ true, false
Tells whether an as option exists.
-
#autobuilding? ⇒ true, false
Is the relation autobuilding if accessed via the getter and the document is new.
-
#autosave ⇒ true, false
Returns the autosave option of the relation.
-
#autosave? ⇒ true, false
Does the metadata have a autosave option?.
-
#builder(base, object) ⇒ Builder
Gets a relation builder associated with the relation this metadata is for.
-
#cascade_strategy ⇒ Object
Returns the name of the strategy used for handling dependent relations.
-
#cascading_callbacks? ⇒ true, false
Is this an embedded relations that allows callbacks to cascade down to it?.
-
#class_name ⇒ String
Returns the name of the class that this relation contains.
-
#constraint ⇒ Constraint
Get the foreign key contraint for the metadata.
-
#counter_cache_column_name ⇒ String
Returns the counter cache column name.
-
#counter_cached? ⇒ true, false
Does the metadata have a counter cache?.
-
#criteria(object, type) ⇒ Criteria
Get the criteria that is used to query for this metadata’s relation.
-
#cyclic ⇒ true, false
Returns the cyclic option of the relation.
-
#cyclic? ⇒ true, false
Does the metadata have a cyclic option?.
-
#dependent ⇒ Symbol
Returns the dependent option of the relation.
-
#dependent? ⇒ true, false
Does the metadata have a dependent option?.
-
#destructive? ⇒ true, false
Does the relation have a destructive dependent option specified.
-
#eager_load(ids) ⇒ Criteria
Get the criteria needed to eager load this relation.
-
#embedded? ⇒ true, false
Will determine if the relation is an embedded one or not.
-
#extension ⇒ Module
Returns the extension of the relation.
-
#extension? ⇒ true, false
Tells whether an extension definition exist for this relation.
-
#forced_nil_inverse? ⇒ true, false
Does this metadata have a forced nil inverse_of defined.
-
#foreign_key ⇒ String
Handles all the logic for figuring out what the foreign_key is for each relations query.
-
#foreign_key_check ⇒ String
Get the name of the method to check if the foreign key has changed.
-
#foreign_key_setter ⇒ String
Returns the name of the method used to set the foreign key on a document.
-
#index ⇒ true, false
Returns the index option of the relation.
-
#indexed? ⇒ true, false
Tells whether a foreign key index exists on the relation.
-
#initialize(properties = {}) ⇒ Metadata
constructor
Instantiate new metadata for a relation.
-
#inspect ⇒ String
Since a lot of the information from the metadata is inferred and not explicitly stored in the hash, the inspection needs to be much more detailed.
-
#inverse(other = nil) ⇒ Symbol
Get the name of the inverse relation if it exists.
-
#inverse_class_name ⇒ true, false
Returns the inverse_class_name option of the relation.
-
#inverse_class_name? ⇒ true, false
Returns the if the inverse class name option exists.
-
#inverse_field_bindable? ⇒ true, false
Is the inverse field bindable? Ie, do we have more than one definition on the parent class with the same polymorphic name (as).
-
#inverse_foreign_key ⇒ String
Used for relational many to many only.
-
#inverse_klass ⇒ Class
Returns the inverse class of the proxied relation.
-
#inverse_metadata(object) ⇒ Metadata
Get the metadata for the inverse relation.
-
#inverse_of ⇒ true, false
Returns the inverse_of option of the relation.
-
#inverse_of? ⇒ true, false
Does the metadata have a inverse_of option?.
-
#inverse_of_field ⇒ String
Returns the name of the field in which to store the name of the inverse field for the polymorphic relation.
-
#inverse_of_field_setter ⇒ String
Gets the setter for the field that stores the name of the inverse field on a polymorphic relation.
-
#inverse_setter(other = nil) ⇒ String
Returns the setter for the inverse side of the relation.
-
#inverse_type ⇒ String
Returns the name of the field in which to store the name of the class for the polymorphic relation.
-
#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 name of the inverse relations if they exists.
-
#key ⇒ String
This returns the key that is to be used to grab the attributes for the relation or the foreign key or id that a referenced relation will use to query for the object.
-
#klass ⇒ Class
Returns the class of the proxied relation.
-
#macro ⇒ Symbol
Returns the macro for the relation of this metadata.
-
#many? ⇒ true, false
Is this metadata representing a one to many or many to many relation?.
-
#name ⇒ Symbol
Get the name associated with this metadata.
-
#name? ⇒ true, false
Is the name defined?.
-
#nested_builder(attributes, options) ⇒ NestedBuilder
Gets a relation nested builder associated with the relation this metadata is for.
-
#options ⇒ Metadata
Returns the metadata itself.
-
#order ⇒ Criterion::Complex?
Returns default order for this association.
-
#order? ⇒ true, false
Is a default order set?.
-
#path(document) ⇒ Object
Get the path calculator for the supplied document.
-
#polymorphic? ⇒ true, false
Returns true if the relation is polymorphic.
-
#primary_key ⇒ String
Get the primary key field for finding the related document.
-
#relation ⇒ Proxy
Get the relation associated with this metadata.
-
#setter ⇒ String
Gets the method name used to set this relation.
-
#store_as ⇒ String
Key where embedded document is save.
-
#touchable? ⇒ true, false
Is this relation touchable?.
-
#type ⇒ String
Returns the name of the field in which to store the name of the class for the polymorphic relation.
-
#type_relation ⇒ Hash
Returns the metadata class types.
-
#type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic relation.
-
#validate? ⇒ true, false
Are we validating this relation automatically?.
-
#versioned? ⇒ true, false
Is this relation using Mongoid’s internal versioning system?.
Constructor Details
Instance Method Details
#as ⇒ true, false
Returns the as option of the relation.
19 20 21 |
# File 'lib/mongoid/relations/metadata.rb', line 19 def as self[:as] end |
#as? ⇒ true, false
Tells whether an as option exists.
31 32 33 |
# File 'lib/mongoid/relations/metadata.rb', line 31 def as? !!as end |
#autobuilding? ⇒ true, false
Is the relation autobuilding if accessed via the getter and the document is new.
44 45 46 |
# File 'lib/mongoid/relations/metadata.rb', line 44 def autobuilding? !!self[:autobuild] end |
#autosave ⇒ true, false
Returns the autosave option of the relation.
56 57 58 |
# File 'lib/mongoid/relations/metadata.rb', line 56 def autosave self[:autosave] end |
#autosave? ⇒ true, false
Does the metadata have a autosave option?
68 69 70 |
# File 'lib/mongoid/relations/metadata.rb', line 68 def autosave? !!autosave end |
#builder(base, object) ⇒ Builder
Gets a relation builder associated with the relation this metadata is for.
84 85 86 |
# File 'lib/mongoid/relations/metadata.rb', line 84 def builder(base, object) relation.builder(base, self, object) end |
#cascade_strategy ⇒ Object
Returns the name of the strategy used for handling dependent relations.
96 97 98 99 100 |
# File 'lib/mongoid/relations/metadata.rb', line 96 def cascade_strategy if dependent? "Mongoid::Relations::Cascading::#{dependent.to_s.classify}".constantize end end |
#cascading_callbacks? ⇒ true, false
Is this an embedded relations that allows callbacks to cascade down to it?
111 112 113 |
# File 'lib/mongoid/relations/metadata.rb', line 111 def cascading_callbacks? !!self[:cascade_callbacks] end |
#class_name ⇒ String
Returns the name of the class that this relation contains. If the class_name was provided as an option this will return that, otherwise it will determine the name from the name property.
125 126 127 |
# File 'lib/mongoid/relations/metadata.rb', line 125 def class_name @class_name ||= (self[:class_name] || classify).sub(/\A::/,"") end |
#constraint ⇒ Constraint
Get the foreign key contraint for the metadata.
137 138 139 |
# File 'lib/mongoid/relations/metadata.rb', line 137 def constraint @constraint ||= Constraint.new(self) end |
#counter_cache_column_name ⇒ String
Returns the counter cache column name
161 162 163 164 165 166 167 |
# File 'lib/mongoid/relations/metadata.rb', line 161 def counter_cache_column_name if self[:counter_cache] == true "#{inverse || inverse_class_name.demodulize.underscore.pluralize}_count" else self[:counter_cache].to_s end end |
#counter_cached? ⇒ true, false
Does the metadata have a counter cache?
149 150 151 |
# File 'lib/mongoid/relations/metadata.rb', line 149 def counter_cached? !!self[:counter_cache] end |
#criteria(object, type) ⇒ Criteria
Get the criteria that is used to query for this metadata’s relation.
180 181 182 |
# File 'lib/mongoid/relations/metadata.rb', line 180 def criteria(object, type) relation.criteria(self, object, type) end |
#cyclic ⇒ true, false
Returns the cyclic option of the relation.
192 193 194 |
# File 'lib/mongoid/relations/metadata.rb', line 192 def cyclic self[:cyclic] end |
#cyclic? ⇒ true, false
Does the metadata have a cyclic option?
204 205 206 |
# File 'lib/mongoid/relations/metadata.rb', line 204 def cyclic? !!cyclic end |
#dependent ⇒ Symbol
Returns the dependent option of the relation.
216 217 218 |
# File 'lib/mongoid/relations/metadata.rb', line 216 def dependent self[:dependent] end |
#dependent? ⇒ true, false
Does the metadata have a dependent option?
228 229 230 |
# File 'lib/mongoid/relations/metadata.rb', line 228 def dependent? !!dependent end |
#destructive? ⇒ true, false
Does the relation have a destructive dependent option specified. This is true for :dependent => :delete and :dependent => :destroy.
690 691 692 |
# File 'lib/mongoid/relations/metadata.rb', line 690 def destructive? @destructive ||= (dependent == :delete || dependent == :destroy) end |
#eager_load(ids) ⇒ Criteria
Get the criteria needed to eager load this relation.
242 243 244 |
# File 'lib/mongoid/relations/metadata.rb', line 242 def eager_load(ids) relation.eager_load(self, ids) end |
#embedded? ⇒ true, false
Will determine if the relation is an embedded one or not. Currently only checks against embeds one and many.
255 256 257 |
# File 'lib/mongoid/relations/metadata.rb', line 255 def @embedded ||= (macro == :embeds_one || macro == :embeds_many) end |
#extension ⇒ Module
Returns the extension of the relation.
267 268 269 |
# File 'lib/mongoid/relations/metadata.rb', line 267 def extension self[:extend] end |
#extension? ⇒ true, false
Tells whether an extension definition exist for this relation.
279 280 281 |
# File 'lib/mongoid/relations/metadata.rb', line 279 def extension? !!extension end |
#forced_nil_inverse? ⇒ true, false
Does this metadata have a forced nil inverse_of defined. (Used in many to manies)
292 293 294 |
# File 'lib/mongoid/relations/metadata.rb', line 292 def forced_nil_inverse? @forced_nil_inverse ||= has_key?(:inverse_of) && inverse_of.nil? end |
#foreign_key ⇒ String
Handles all the logic for figuring out what the foreign_key is for each relations query. The logic is as follows:
-
If the developer defined a custom key, use that.
-
If the relation stores a foreign key, use the class_name_id strategy.
-
If the relation does not store the key, use the inverse_class_name_id strategy.
311 312 313 |
# File 'lib/mongoid/relations/metadata.rb', line 311 def foreign_key @foreign_key ||= determine_foreign_key end |
#foreign_key_check ⇒ String
Get the name of the method to check if the foreign key has changed.
323 324 325 |
# File 'lib/mongoid/relations/metadata.rb', line 323 def foreign_key_check @foreign_key_check ||= "#{foreign_key}_changed?" end |
#foreign_key_setter ⇒ String
Returns the name of the method used to set the foreign key on a document.
336 337 338 |
# File 'lib/mongoid/relations/metadata.rb', line 336 def foreign_key_setter @foreign_key_setter ||= "#{foreign_key}=" end |
#index ⇒ true, false
Returns the index option of the relation.
348 349 350 |
# File 'lib/mongoid/relations/metadata.rb', line 348 def index self[:index] end |
#indexed? ⇒ true, false
Tells whether a foreign key index exists on the relation.
360 361 362 |
# File 'lib/mongoid/relations/metadata.rb', line 360 def indexed? !!index end |
#inspect ⇒ String
Since a lot of the information from the metadata is inferred and not explicitly stored in the hash, the inspection needs to be much more detailed.
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/mongoid/relations/metadata.rb', line 387 def inspect %Q{#<Mongoid::Relations::Metadata autobuild: #{autobuilding?} class_name: #{class_name} cyclic: #{cyclic.inspect} counter_cache:#{counter_cached?} dependent: #{dependent.inspect} inverse_of: #{inverse_of.inspect} key: #{key} macro: #{macro} name: #{name} order: #{order.inspect} polymorphic: #{polymorphic?} relation: #{relation} setter: #{setter} versioned: #{versioned?}> } end |
#inverse(other = nil) ⇒ Symbol
Get the name of the inverse relation if it exists. If this is a polymorphic relation then just return the :as option that was defined.
434 435 436 437 |
# File 'lib/mongoid/relations/metadata.rb', line 434 def inverse(other = nil) invs = inverses(other) invs.first if invs.count == 1 end |
#inverse_class_name ⇒ true, false
Returns the inverse_class_name option of the relation.
447 448 449 |
# File 'lib/mongoid/relations/metadata.rb', line 447 def inverse_class_name self[:inverse_class_name] end |
#inverse_class_name? ⇒ true, false
Returns the if the inverse class name option exists.
459 460 461 |
# File 'lib/mongoid/relations/metadata.rb', line 459 def inverse_class_name? !!inverse_class_name end |
#inverse_field_bindable? ⇒ true, false
Is the inverse field bindable? Ie, do we have more than one definition on the parent class with the same polymorphic name (as).
472 473 474 475 476 |
# File 'lib/mongoid/relations/metadata.rb', line 472 def inverse_field_bindable? @inverse_field_bindable ||= (inverse_klass.relations.values.count do || .as == as end > 1) end |
#inverse_foreign_key ⇒ String
Used for relational many to many only. This determines the name of the foreign key field on the inverse side of the relation, since in this case there are keys on both sides.
488 489 490 |
# File 'lib/mongoid/relations/metadata.rb', line 488 def inverse_foreign_key @inverse_foreign_key ||= determine_inverse_foreign_key end |
#inverse_klass ⇒ Class
Returns the inverse class of the proxied relation.
500 501 502 |
# File 'lib/mongoid/relations/metadata.rb', line 500 def inverse_klass @inverse_klass ||= inverse_class_name.constantize end |
#inverse_metadata(object) ⇒ Metadata
Get the metadata for the inverse relation.
514 515 516 |
# File 'lib/mongoid/relations/metadata.rb', line 514 def (object) object.reflect_on_association(inverse(object)) end |
#inverse_of ⇒ true, false
Returns the inverse_of option of the relation.
526 527 528 |
# File 'lib/mongoid/relations/metadata.rb', line 526 def inverse_of self[:inverse_of] end |
#inverse_of? ⇒ true, false
Does the metadata have a inverse_of option?
538 539 540 |
# File 'lib/mongoid/relations/metadata.rb', line 538 def inverse_of? !!inverse_of end |
#inverse_of_field ⇒ String
Returns the name of the field in which to store the name of the inverse field for the polymorphic relation.
592 593 594 |
# File 'lib/mongoid/relations/metadata.rb', line 592 def inverse_of_field @inverse_of_field ||= determine_inverse_for(:field) end |
#inverse_of_field_setter ⇒ String
Gets the setter for the field that stores the name of the inverse field on a polymorphic relation.
603 604 605 |
# File 'lib/mongoid/relations/metadata.rb', line 603 def inverse_of_field_setter @inverse_of_field_setter ||= inverse_of_field.__setter__ end |
#inverse_setter(other = nil) ⇒ String
Returns the setter for the inverse side of the relation.
552 553 554 |
# File 'lib/mongoid/relations/metadata.rb', line 552 def inverse_setter(other = nil) inverse(other).__setter__ end |
#inverse_type ⇒ String
Returns the name of the field in which to store the name of the class for the polymorphic relation.
565 566 567 |
# File 'lib/mongoid/relations/metadata.rb', line 565 def inverse_type @inverse_type ||= determine_inverse_for(:type) end |
#inverse_type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic relation.
578 579 580 |
# File 'lib/mongoid/relations/metadata.rb', line 578 def inverse_type_setter @inverse_type_setter ||= inverse_type.__setter__ end |
#inverses(other = nil) ⇒ Array<Symbol>
Get the name of the inverse relations if they exists. If this is a polymorphic relation then just return the :as option that was defined.
415 416 417 418 419 420 421 |
# File 'lib/mongoid/relations/metadata.rb', line 415 def inverses(other = nil) if self[:polymorphic] lookup_inverses(other) else @inverses ||= determine_inverses end end |
#key ⇒ String
This returns the key that is to be used to grab the attributes for the relation or the foreign key or id that a referenced relation will use to query for the object.
617 618 619 |
# File 'lib/mongoid/relations/metadata.rb', line 617 def key @key ||= determine_key end |
#klass ⇒ Class
Returns the class of the proxied relation.
629 630 631 |
# File 'lib/mongoid/relations/metadata.rb', line 629 def klass @klass ||= class_name.constantize end |
#macro ⇒ Symbol
Returns the macro for the relation of this metadata.
653 654 655 |
# File 'lib/mongoid/relations/metadata.rb', line 653 def macro relation.macro end |
#many? ⇒ true, false
Is this metadata representing a one to many or many to many relation?
641 642 643 |
# File 'lib/mongoid/relations/metadata.rb', line 641 def many? @many ||= (relation.macro.to_s =~ /many/) end |
#name ⇒ Symbol
Get the name associated with this metadata.
665 666 667 |
# File 'lib/mongoid/relations/metadata.rb', line 665 def name self[:name] end |
#name? ⇒ true, false
Is the name defined?
677 678 679 |
# File 'lib/mongoid/relations/metadata.rb', line 677 def name? !!name end |
#nested_builder(attributes, options) ⇒ NestedBuilder
Gets a relation nested builder associated with the relation this metadata is for. Nested builders are used in conjunction with nested attributes.
706 707 708 |
# File 'lib/mongoid/relations/metadata.rb', line 706 def nested_builder(attributes, ) relation.nested_builder(self, attributes, ) end |
#options ⇒ Metadata
Returns the metadata itself. Here for compatibility with Rails association metadata.
847 848 849 |
# File 'lib/mongoid/relations/metadata.rb', line 847 def self end |
#order ⇒ Criterion::Complex?
Returns default order for this association.
859 860 861 |
# File 'lib/mongoid/relations/metadata.rb', line 859 def order self[:order] end |
#order? ⇒ true, false
Is a default order set?
871 872 873 |
# File 'lib/mongoid/relations/metadata.rb', line 871 def order? !!order end |
#path(document) ⇒ Object
Get the path calculator for the supplied document.
720 721 722 |
# File 'lib/mongoid/relations/metadata.rb', line 720 def path(document) relation.path(document) end |
#polymorphic? ⇒ true, false
Returns true if the relation is polymorphic.
732 733 734 |
# File 'lib/mongoid/relations/metadata.rb', line 732 def polymorphic? @polymorphic ||= (!!self[:as] || !!self[:polymorphic]) end |
#primary_key ⇒ String
Get the primary key field for finding the related document.
744 745 746 |
# File 'lib/mongoid/relations/metadata.rb', line 744 def primary_key @primary_key ||= (self[:primary_key] || "_id").to_s end |
#relation ⇒ Proxy
Get the relation associated with this metadata.
756 757 758 |
# File 'lib/mongoid/relations/metadata.rb', line 756 def relation self[:relation] end |
#setter ⇒ String
Gets the method name used to set this relation.
769 770 771 |
# File 'lib/mongoid/relations/metadata.rb', line 769 def setter @setter ||= "#{name}=" end |
#store_as ⇒ String
Key where embedded document is save. By default is the name of relation
806 807 808 |
# File 'lib/mongoid/relations/metadata.rb', line 806 def store_as @store_as ||= (self[:store_as].try(:to_s) || name.to_s) end |
#touchable? ⇒ true, false
Is this relation touchable?
883 884 885 |
# File 'lib/mongoid/relations/metadata.rb', line 883 def touchable? !!self[:touch] end |
#type ⇒ String
Returns the name of the field in which to store the name of the class for the polymorphic relation.
782 783 784 |
# File 'lib/mongoid/relations/metadata.rb', line 782 def type @type ||= polymorphic? ? "#{as}_type" : nil end |
#type_relation ⇒ Hash
Returns the metadata class types.
895 896 897 |
# File 'lib/mongoid/relations/metadata.rb', line 895 def type_relation { _type: { "$in" => klass._types }} end |
#type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic relation.
795 796 797 |
# File 'lib/mongoid/relations/metadata.rb', line 795 def type_setter @type_setter ||= type.__setter__ end |
#validate? ⇒ true, false
Are we validating this relation automatically?
818 819 820 821 822 823 824 |
# File 'lib/mongoid/relations/metadata.rb', line 818 def validate? unless self[:validate].nil? self[:validate] else self[:validate] = relation.validation_default end end |
#versioned? ⇒ true, false
Is this relation using Mongoid’s internal versioning system?
834 835 836 |
# File 'lib/mongoid/relations/metadata.rb', line 834 def versioned? !!self[:versioned] end |