Sha256: f1c61e49ae8889618cdd0b91a4d54f101f3cd7a356850b91875eac174fb53ad3

Contents?: true

Size: 1.08 KB

Versions: 12

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module N1Loader
  module ArLazyPreload
    # Context adapter for injected N1Loader loaders.
    class ContextAdapter
      attr_reader :context

      delegate_missing_to :context

      def initialize(context)
        @context = context
      end

      # Assign initialized preloader to +association_name+ in case it wasn't yet preloaded within the given context.
      def try_preload_lazily(association_name)
        return unless context&.send(:association_needs_preload?, association_name)

        perform_preloading(association_name)
      end

      # Initialize preloader for +association_name+ with context builder callback.
      # The callback will be executed when on records load.
      def perform_preloading(association_name)
        context_setup = lambda { |records|
          AssociatedContextBuilder.prepare(
            parent_context: self,
            association_name: association_name,
            records: records
          )
        }

        N1Loader::Preloader.new(records, context_setup).preload(association_name)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
n1_loader-1.7.4 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.7.3 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.7.2 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.7.1 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.7.0 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.6.6 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.6.5 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.6.4 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.6.3 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.6.2 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.6.1 lib/n1_loader/ar_lazy_preload/context_adapter.rb
n1_loader-1.6.0 lib/n1_loader/ar_lazy_preload/context_adapter.rb