Sha256: 4958c83694ffa4e00486a57502549ec552491105b404d61c6d1d1f690eee25e4

Contents?: true

Size: 862 Bytes

Versions: 2

Compression:

Stored size: 862 Bytes

Contents

# frozen_string_literal: true
module Mongoload
  module RelationLoader
    module_function

    def load(doc, metadata)
      docs = doc.auto_include_context.docs.select do |d|
        auto_include?(d, metadata)
      end
      eager_load(metadata, docs)
      docs.include?(doc)
    end

    def eager_load(metadata, docs)
      metadata.relation.eager_load_klass.new([metadata], docs).run
    end

    def auto_include?(doc, metadata)
      relation?(doc, metadata) && !loaded?(doc, metadata)
    end

    def relation?(doc, metadata)
      doc.relations.values.include?(metadata)
    end

    def loaded?(doc, metadata)
      if [Mongoid::Relations::Referenced::In, Mongoid::Relations::Referenced::One].include?(metadata.relation)
        doc.ivar(metadata.name) != false
      else
        doc.public_send(metadata.name)._loaded?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoload-0.0.2 lib/mongoload/relation_loader.rb
mongoload-0.0.1 lib/mongoload/relation_loader.rb