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
-
#eql?(other) ⇒ Boolean
Public: Checks if the collection already has an inclusion with the specified metadata.
-
#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
#eql?(other) ⇒ Boolean
Public: Checks if the collection already has an inclusion with the specified metadata.
22 23 24 |
# File 'lib/mongoid/includes/inclusion.rb', line 22 def eql?(other) == other && (!other.respond_to?(:from) || from == other.from) end |
#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.
61 62 63 64 65 66 67 |
# File 'lib/mongoid/includes/inclusion.rb', line 61 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.
32 33 34 |
# File 'lib/mongoid/includes/inclusion.rb', line 32 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.
48 49 50 51 52 53 54 55 |
# File 'lib/mongoid/includes/inclusion.rb', line 48 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.
37 38 39 |
# File 'lib/mongoid/includes/inclusion.rb', line 37 def loader @loader ||= @options[:loader] end |
#modifier ⇒ Object
Internal: Proc that will modify the documents to include in the relation.
42 43 44 |
# File 'lib/mongoid/includes/inclusion.rb', line 42 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.
27 28 29 |
# File 'lib/mongoid/includes/inclusion.rb', line 27 def polymorphic_belongs_to? .polymorphic? && .relation == Mongoid::Relations::Referenced::In end |