Sha256: 29d574a483956ff6263d1f46ecc2cbf92c30557c13009bd44291c7d947b35a68

Contents?: true

Size: 882 Bytes

Versions: 16

Compression:

Stored size: 882 Bytes

Contents

# frozen_string_literal: true

module ArLazyPreload
  # ActiveRecord::Relation::Merger patch implementing merge functionality
  # for lazy preloadable relations
  module Merger
    # Enhanced #merge implements merging lazy_preload_values
    def merge
      result = super

      if other.lazy_preload_values.any?
        if other.klass == relation.klass
          merge_lazy_preloads
        else
          reflect_and_merge_lazy_preloads
        end
      end

      result
    end

    private

    def merge_lazy_preloads
      relation.lazy_preload!(*other.lazy_preload_values)
    end

    def reflect_and_merge_lazy_preloads
      reflection = relation.klass.reflect_on_all_associations.find do |r|
        r.class_name == other.klass.name
      end
      return unless reflection

      relation.lazy_preload!(reflection.name => other.lazy_preload_values)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ar_lazy_preload-2.1.0 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-2.0.0 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-1.1.2 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-1.1.1 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-1.1.0 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-1.0.0 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.7.0 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.6.2 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.6.1 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.6.0 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.5.2 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.5.1 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.5.0 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.4.0 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.3.2 lib/ar_lazy_preload/active_record/merger.rb
ar_lazy_preload-0.3.1 lib/ar_lazy_preload/active_record/merger.rb