Sha256: d27ff668a0f280c1e041d0b6ec7f64431f919f3b4fd1a50cd37fe0a2ad40bb15

Contents?: true

Size: 1.17 KB

Versions: 25

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

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

25 entries across 25 versions & 1 rubygems

Version Path
mongoid-8.1.8 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.1.7 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.1.6 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.0.8 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.1.5 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.1.4 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.0.7 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.1.3 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.1.2 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.0.6 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.5.4 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.1.1 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.0.5 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.1.0 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.5.3 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.0.4 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.0.3 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.5.2 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-8.0.2 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb
mongoid-7.5.1 lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb