Sha256: 9e1ede58758ff0c08213165c87f9b243486b2bff04adac2b866541c7463bf91e

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module IdentityCache
  module Cached
    module Prefetcher
      ASSOCIATION_FETCH_EVENT = "association_fetch.identity_cache"

      class << self
        def prefetch(klass, associations, records, load_strategy: LoadStrategy::Eager)
          return if (records = records.to_a).empty?

          case associations
          when Symbol
            fetch_association(load_strategy, klass, associations, records) {}
          when Array
            load_strategy.lazy_load do |lazy_loader|
              associations.each do |association|
                prefetch(klass, association, records, load_strategy: lazy_loader)
              end
            end
          when Hash
            load_strategy.lazy_load do |lazy_loader|
              associations.each do |association, sub_associations|
                fetch_association(lazy_loader, klass, association, records) do |next_level_records|
                  if sub_associations.present?
                    association_class = klass.reflect_on_association(association).klass
                    prefetch(association_class, sub_associations, next_level_records, load_strategy: lazy_loader)
                  end
                end
              end
            end
          else
            raise TypeError, "Invalid associations class #{associations.class}"
          end
        end

        private

        def fetch_association(load_strategy, klass, association, records, &block)
          unless records.first.class.should_use_cache?
            ActiveRecord::Associations::Preloader.new.preload(records, association)
            return yield
          end

          cached_association = klass.cached_association(association)
          cached_association.fetch_async(load_strategy, records, &block)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
identity_cache-1.0.1 lib/identity_cache/cached/prefetcher.rb
identity_cache-1.0.0 lib/identity_cache/cached/prefetcher.rb