Class: Mongoid::Includes::Inclusion
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Mongoid::Includes::Inclusion
- Defined in:
- lib/mongoid/includes/inclusion.rb
Overview
Public: Represents a relation that needs to be eager loaded.
Instance Method Summary collapse
-
#for_class_name(class_name) ⇒ Object
Public: Clones the inclusion and changes the Mongoid::Metadata::Relation that it wraps to make it non polymorphic and target a particular class.
-
#from ⇒ Object
Public: Name of the relation from which a nested inclusion is performed.
-
#initialize(metadata, options = {}) ⇒ Inclusion
constructor
A new instance of Inclusion.
-
#load_documents_for(foreign_key, foreign_key_values) ⇒ Object
Public: Preloads the documents for the relation.
-
#loader ⇒ Object
Internal: Proc that will return the included documents from a set of foreign keys.
-
#modifier ⇒ Object
Internal: Proc that will modify the documents to include in the relation.
-
#nested? ⇒ Boolean
Public: Returns true if the relation is not direct.
-
#polymorphic_belongs_to? ⇒ Boolean
Public: Returns true if the relation is a polymorphic belongs_to.
Constructor Details
#initialize(metadata, options = {}) ⇒ Inclusion
Returns a new instance of Inclusion.
10 11 12 13 |
# File 'lib/mongoid/includes/inclusion.rb', line 10 def initialize(, = {}) super() @options = end |
Instance Method Details
#for_class_name(class_name) ⇒ Object
Public: Clones the inclusion and changes the Mongoid::Metadata::Relation that it wraps to make it non polymorphic and target a particular class.
Returns an Inclusion that can be eager loaded as usual.
55 56 57 58 59 60 61 |
# File 'lib/mongoid/includes/inclusion.rb', line 55 def for_class_name(class_name) Inclusion.new .clone.instance_eval { || self[:class_name] = @class_name = class_name self[:polymorphic], self[:as], @polymorphic, @klass = nil self } end |
#from ⇒ Object
Public: Name of the relation from which a nested inclusion is performed.
26 27 28 |
# File 'lib/mongoid/includes/inclusion.rb', line 26 def from @from ||= @options[:from] end |
#load_documents_for(foreign_key, foreign_key_values) ⇒ Object
Public: Preloads the documents for the relation. Users a custom block if one was provided, or fetches them using the class and the foreign key.
42 43 44 45 46 47 48 49 |
# File 'lib/mongoid/includes/inclusion.rb', line 42 def load_documents_for(foreign_key, foreign_key_values) if loader loader.call(foreign_key, foreign_key_values) else docs = klass.any_in(foreign_key => foreign_key_values) modifier ? modifier.call(docs) : docs end end |
#loader ⇒ Object
Internal: Proc that will return the included documents from a set of foreign keys.
31 32 33 |
# File 'lib/mongoid/includes/inclusion.rb', line 31 def loader @loader ||= @options[:loader] end |
#modifier ⇒ Object
Internal: Proc that will modify the documents to include in the relation.
36 37 38 |
# File 'lib/mongoid/includes/inclusion.rb', line 36 def modifier @modifier ||= @options[:with] end |
#nested? ⇒ Boolean
Public: Returns true if the relation is not direct.
16 17 18 |
# File 'lib/mongoid/includes/inclusion.rb', line 16 def nested? !!from end |
#polymorphic_belongs_to? ⇒ Boolean
Public: Returns true if the relation is a polymorphic belongs_to.
21 22 23 |
# File 'lib/mongoid/includes/inclusion.rb', line 21 def polymorphic_belongs_to? .polymorphic? && .relation == Mongoid::Relations::Referenced::In end |