Sha256: ed0cdab007c609ee0d1400d0bd86c0ab391bd5667652d2b983f04a6825d1e860

Contents?: true

Size: 1011 Bytes

Versions: 3

Compression:

Stored size: 1011 Bytes

Contents

# frozen_string_literal: true

module ArLazyPreload
  # ActiveRecord::Base patch with lazy preloading support
  module Base
    def self.included(base)
      base.class.delegate :lazy_preload, to: :all
      base.class.delegate :preload_associations_lazily, to: :all

      base.after_create { try_setup_auto_preload_context }

      base.extend(ClassMethods)
    end

    attr_accessor :lazy_preload_context

    delegate :try_preload_lazily, to: :lazy_preload_context, allow_nil: true

    def reload(options = nil)
      super(options).tap { try_setup_auto_preload_context }
    end

    def skip_preload
      lazy_preload_context&.records&.delete(self)
      self.lazy_preload_context = nil
      self
    end

    def try_setup_auto_preload_context
      ArLazyPreload::Context.register(records: [self]) if ArLazyPreload.config.auto_preload?
    end

    module ClassMethods
      def find_by(*args)
        super(*args).tap { |object| object&.try_setup_auto_preload_context }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ar_lazy_preload-2.1.0 lib/ar_lazy_preload/active_record/base.rb
ar_lazy_preload-2.0.0 lib/ar_lazy_preload/active_record/base.rb
ar_lazy_preload-1.1.2 lib/ar_lazy_preload/active_record/base.rb