Sha256: 3e091da596f6014ce1c2a2a8576ae7bdd5096fa7d825ede5ccb265b2e53730d0

Contents?: true

Size: 961 Bytes

Versions: 22

Compression:

Stored size: 961 Bytes

Contents

# encoding: utf-8
module Mongoid
  module Relations
    module Eager

      class HasAndBelongsToMany < Base

        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__(@metadata.name, element, @metadata) unless doc.blank?
        end

        def group_by_key
          @metadata.foreign_key
        end

        def key
          @metadata.primary_key
        end
      end
    end
  end
end

Version data entries

22 entries across 20 versions & 2 rubygems

Version Path
mongoid-6.4.8 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.4.7 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.4.5 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.4.4 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.4.2 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.4.1 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
tdiary-5.0.8 vendor/bundle/gems/mongoid-6.4.0/lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.4.0 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.3.0 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.2.1 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/relations/eager/has_and_belongs_to_many.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.2.0/lib/mongoid/relations/eager/has_and_belongs_to_many.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.2.0 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.1.1 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
tdiary-5.0.4 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.1.0 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.1.0.rc0 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.0.3 lib/mongoid/relations/eager/has_and_belongs_to_many.rb
mongoid-6.0.2 lib/mongoid/relations/eager/has_and_belongs_to_many.rb