Sha256: 28d3579011c599bae614894e53c2b45b94236d59efc751422e2ff4d578962b88

Contents?: true

Size: 926 Bytes

Versions: 1

Compression:

Stored size: 926 Bytes

Contents

# frozen_string_literal: true
module Mongoload
  module RelationsEnumerableWithAutoInclude
    # methods which trigger Mongoload only when :fully_load is true
    # other methods delegated to #target will be processed by #unloaded_documents
    %w(first last size empty?).each do |method|
      define_method method do
        if !_loaded? && relation_metadata.fully_load? && Mongoload::RelationLoader.load(base, relation_metadata)
          base.ivar(relation_metadata.name).public_send(method)
        else
          super()
        end
      end
    end

    private

    # perform eager loading when loading unloaded documents
    def unloaded_documents
      if !_loaded? && Mongoload::RelationLoader.load(base, relation_metadata)
        base.ivar(relation_metadata.name)
      else
        super
      end
    end
  end
end

Mongoid::Relations::Targets::Enumerable.prepend Mongoload::RelationsEnumerableWithAutoInclude

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoload-0.0.3 lib/mongoload/patches/relations_targets_enumerable.rb