Sha256: 2ef95b56b20394a7928b55bd1e8ee68926bfb37f75abc59010b337988868b87a

Contents?: true

Size: 1.03 KB

Versions: 22

Compression:

Stored size: 1.03 KB

Contents

require 'active_record/associations/preloader/association'

module ActiveRecord::Turntable
  module ActiveRecordExt
    module AssociationPreloader
      extend ActiveSupport::Concern

      included do
        alias_method_chain :records_for, :turntable
      end

      # @note Override to add sharding condition on preload
      def records_for_with_turntable(ids)
        returning_scope = records_for_without_turntable(ids)
        if should_use_shard_key?
          returning_scope = returning_scope.where(klass.turntable_shard_key => owners.map(&foreign_shard_key.to_sym).uniq)
        end
        returning_scope
      end

      private

      def foreign_shard_key
        options[:foreign_shard_key] || model.turntable_shard_key
      end

      def should_use_shard_key?
        sharded_by_same_key? || !!options[:foreign_shard_key]
      end

      def sharded_by_same_key?
        model.turntable_enabled? &&
          klass.turntable_enabled? &&
          model.turntable_shard_key == klass.turntable_shard_key
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
activerecord-turntable-2.5.0 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.4.0 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.3.3 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.3.2 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.3.1 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.3.0 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.2.2 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.2.1 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.2.0 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.1.1 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.1.0 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.1.0.rc2 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.1.0.rc1 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.1.0.beta2 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.1.0.beta1 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.0.6 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.0.5 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.0.4 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.0.3 lib/active_record/turntable/active_record_ext/association_preloader.rb
activerecord-turntable-2.0.2 lib/active_record/turntable/active_record_ext/association_preloader.rb