|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Mongoid |
| 4 | + module Association |
| 5 | + |
| 6 | + # Base class for eager load preload functions. |
| 7 | + class Eager |
| 8 | + # Instantiate the eager load class. |
| 9 | + # |
| 10 | + # @example Create the new belongs to eager load preloader. |
| 11 | + # BelongsTo.new(association, parent_docs) |
| 12 | + # |
| 13 | + # @param [ Array<Mongoid::Association::Relatable> ] associations |
| 14 | + # Associations to eager load |
| 15 | + # @param [ Array<Document> ] docs Documents to preload the associations |
| 16 | + # |
| 17 | + # @return [ Base ] The eager load preloader |
| 18 | + def initialize(associations, docs) |
| 19 | + @associations = associations |
| 20 | + @docs = docs |
| 21 | + @grouped_docs = {} |
| 22 | + end |
| 23 | + |
| 24 | + # Run the preloader. |
| 25 | + # |
| 26 | + # @example Preload the associations into the documents. |
| 27 | + # loader.run |
| 28 | + # |
| 29 | + # @return [ Array ] The list of documents given. |
| 30 | + def run |
| 31 | + @loaded = [] |
| 32 | + while shift_association |
| 33 | + preload |
| 34 | + @loaded << @docs.collect { |d| d.send(@association.name) if d.respond_to?(@association.name) } |
| 35 | + end |
| 36 | + @loaded.flatten |
| 37 | + end |
| 38 | + |
| 39 | + protected |
| 40 | + |
| 41 | + # Preload the current association. |
| 42 | + # |
| 43 | + # This method should be implemented in the subclass |
| 44 | + # |
| 45 | + # @example Preload the current association into the documents. |
| 46 | + # loader.preload |
| 47 | + def preload |
| 48 | + raise NotImplementedError |
| 49 | + end |
| 50 | + |
| 51 | + # Retrieves the documents referenced by the association, and |
| 52 | + # yields each one sequentially to the provided block. If the |
| 53 | + # association is not polymorphic, all documents are retrieved in |
| 54 | + # a single query. If the association is polymorphic, one query is |
| 55 | + # issued per association target class. |
| 56 | + def each_loaded_document(&block) |
| 57 | + each_loaded_document_of_class(@association.klass, keys_from_docs, &block) |
| 58 | + end |
| 59 | + |
| 60 | + # Retrieves the documents of the specified class, that have the |
| 61 | + # foreign key included in the specified list of keys. |
| 62 | + # |
| 63 | + # When the documents are retrieved, the set of inclusions applied |
| 64 | + # is the set of inclusions applied to the host document minus the |
| 65 | + # association that is being eagerly loaded. |
| 66 | + private def each_loaded_document_of_class(cls, keys) |
| 67 | + # Note: keys should not include nil elements. |
| 68 | + # Upstream code is responsible for eliminating nils from keys. |
| 69 | + return cls.none if keys.empty? |
| 70 | + |
| 71 | + criteria = cls.criteria |
| 72 | + criteria = criteria.apply_scope(@association.scope) |
| 73 | + criteria = criteria.any_in(key => keys) |
| 74 | + criteria.inclusions = criteria.inclusions - [@association] |
| 75 | + criteria.each do |doc| |
| 76 | + yield doc |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # Set the pre-loaded document into its parent. |
| 81 | + # |
| 82 | + # @example Set docs into parent with pk = "foo" |
| 83 | + # loader.set_on_parent("foo", docs) |
| 84 | + # |
| 85 | + # @param [ ObjectId ] id parent`s id |
| 86 | + # @param [ Document | Array ] element to push into the parent |
| 87 | + def set_on_parent(id, element) |
| 88 | + grouped_docs[id].each do |d| |
| 89 | + set_relation(d, element) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + # Return a hash with the current documents grouped by key. |
| 94 | + # |
| 95 | + # Documents that do not have a value for the association being loaded |
| 96 | + # are not returned. |
| 97 | + # |
| 98 | + # @example Return a hash with the current documents grouped by key. |
| 99 | + # loader.grouped_docs |
| 100 | + # |
| 101 | + # @return [ Hash ] hash with grouped documents. |
| 102 | + def grouped_docs |
| 103 | + @grouped_docs[@association.name] ||= @docs.group_by do |doc| |
| 104 | + doc.send(group_by_key) if doc.respond_to?(group_by_key) |
| 105 | + end.reject do |k, v| |
| 106 | + k.nil? |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + # Group the documents and return the keys. |
| 111 | + # |
| 112 | + # This method omits nil keys (i.e. keys from documents that do not |
| 113 | + # have a value for the association being loaded). |
| 114 | + # |
| 115 | + # @example |
| 116 | + # loader.keys_from_docs |
| 117 | + # |
| 118 | + # @return [ Array ] keys, ids |
| 119 | + def keys_from_docs |
| 120 | + grouped_docs.keys |
| 121 | + end |
| 122 | + |
| 123 | + # Return the key to group the current documents. |
| 124 | + # |
| 125 | + # This method should be implemented in the subclass |
| 126 | + # |
| 127 | + # @example Return the key for group |
| 128 | + # loader.group_by_key |
| 129 | + # |
| 130 | + # @return [ Symbol ] Key to group by the current documents. |
| 131 | + def group_by_key |
| 132 | + raise NotImplementedError |
| 133 | + end |
| 134 | + |
| 135 | + # Set the pre-loaded document into its parent. |
| 136 | + # |
| 137 | + # @example Set docs into parent using the current association name. |
| 138 | + # loader.set_relation(doc, docs) |
| 139 | + # |
| 140 | + # @param [ Document ] doc The object to set the association on |
| 141 | + # @param [ Document | Array ] element to set into the parent |
| 142 | + def set_relation(doc, element) |
| 143 | + doc.set_relation(@association.name, element) unless doc.blank? |
| 144 | + end |
| 145 | + |
| 146 | + private |
| 147 | + |
| 148 | + # Shift the current association metadata |
| 149 | + # |
| 150 | + # @example Shift the current association. |
| 151 | + # loader.shift_association |
| 152 | + # |
| 153 | + # @return [ Mongoid::Association::Relatable ] The association object. |
| 154 | + def shift_association |
| 155 | + @association = @associations.shift |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | +end |
0 commit comments