Sha256: 377cfbcd2e0783b2490bbd17d73cfbf8fba192edc03d541abb8aa68c562cb565

Contents?: true

Size: 845 Bytes

Versions: 1

Compression:

Stored size: 845 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 }
    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

    private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ar_lazy_preload-1.1.1 lib/ar_lazy_preload/active_record/base.rb