Sha256: 9eef07eb5044545a3757a34d99adae8dc7f1ac6deaeb31fdc68eb767f1f13008

Contents?: true

Size: 1.28 KB

Versions: 16

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module IdentityCache
  module Cached
    module EmbeddedFetching
      private

      def fetch_embedded(records)
        fetch_embedded_async(LoadStrategy::Eager, records) {}
      end

      def fetch_embedded_async(load_strategy, records)
        return yield if embedded_fetched?(records)

        klass = reflection.active_record
        cached_associations = klass.send(:embedded_associations)

        return yield if cached_associations.empty?

        return yield unless klass.primary_cache_index_enabled

        load_strategy.load_multi(klass.cached_primary_index, records.map(&:id)) do |cached_records_by_id|
          cached_associations.each_value do |cached_association|
            records.each do |record|
              next unless (cached_record = cached_records_by_id[record.id])

              cached_value = cached_association.read(cached_record)
              cached_association.write(record, cached_value)
            end
          end

          yield
        end
      end

      def embedded_fetched?(records)
        # NOTE: Assume all records are the same, so just check the first one.
        record = records.first
        record.association(name).loaded? || record.instance_variable_defined?(records_variable_name)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
identity_cache-1.6.3 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.6.2 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.6.1 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.6.0 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.5.6 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.5.5 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.5.4 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.5.3 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.5.2 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.5.1 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.5.0 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.4.1 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.4.0 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.3.1 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.3.0 lib/identity_cache/cached/embedded_fetching.rb
identity_cache-1.2.0 lib/identity_cache/cached/embedded_fetching.rb