Sha256: 85e19523fc7f1952d8fd6100177e8620fba6cb85b46a84a2ef0b6f4808893d1b

Contents?: true

Size: 966 Bytes

Versions: 2

Compression:

Stored size: 966 Bytes

Contents

# frozen_string_literal: true
module Mongoload
  module MongoRelationsAccessorsWithAutoInclude
    private

    def get_relation(name, metadata, object, reload=false)
      # has_one and belongs_to will return the value immediately
      # while other relation are lazy
      if [Mongoid::Relations::Referenced::In, Mongoid::Relations::Referenced::One].include?(metadata.relation)
        Mongoload::RelationLoader.load(self, metadata) if !object.is_a?(metadata.klass) && ivar(name) == false
      end

      result = super
      result.tap do |relation|
        if relation.class == Mongoid::Relations::Targets::Enumerable
          relation.target.define_singleton_method :relation_metadata do
            relation.relation_metadata
          end

          relation.target.define_singleton_method :base do
            relation.base
          end
        end
      end
    end
  end
end

Mongoid::Document.prepend Mongoload::MongoRelationsAccessorsWithAutoInclude

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoload-0.0.2 lib/mongoload/patches/mongoid_relations_accessors_patch.rb
mongoload-0.0.1 lib/mongoload/patches/mongoid_relations_accessors_patch.rb