Sha256: e97331d5385a1debf0ddffd6071d8f5c639bce127d4d46cbfaeb22fad8f1a408

Contents?: true

Size: 1.16 KB

Versions: 14

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8
module Mongoid
  module Association
    module Referenced
      class HasAndBelongsToMany

        # Eager class for has_and_belongs_to_many associations.
        class Eager < Association::Referenced::Eager::Base

          private

          def preload
            @docs.each do |d|
              set_relation(d, [])
            end

            entries = {}
            each_loaded_document do |doc|
              entries[doc.send(key)] = doc
            end

            @docs.each do |d|
              keys = d.send(group_by_key)
              docs = entries.values_at(*keys).compact
              set_relation(d, docs)
            end
          end

          def keys_from_docs
            keys = Set.new
            @docs.each do |d|
              keys += d.send(group_by_key)
            end
            keys.to_a
          end

          def set_relation(doc, element)
            doc.__build__(@association.name, element, @association) unless doc.blank?
          end

          def group_by_key
            @association.foreign_key
          end

          def key
            @association.primary_key
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mongoid-7.0.13 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.12 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.11 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.10 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.8 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.7 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.6 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.5 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.4 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.3 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.2 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.1 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.0 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.0.0.beta lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb