Sha256: 1408ffb83f21f1553612a040801971c8cd42e76f3e402951991b9a922b2c92ba

Contents?: true

Size: 981 Bytes

Versions: 3

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

module Torque
  module PostgreSQL
    module Associations
      module Preloader
        module LoaderQuery
          def foreign_column
            @foreign_column ||= scope.columns_hash[association_key_name.to_s]
          end

          def load_records_for_keys(keys, &block)
            condition = query_condition_for(keys)
            return super if condition.nil?

            scope.where(condition).load(&block)
          end

          def query_condition_for(keys)
            return unless connected_through_array?

            value = scope.cast_for_condition(foreign_column, keys.to_a)
            scope.table[association_key_name].overlaps(value)
          end

          def connected_through_array?
            !association_key_name.is_a?(Array) && foreign_column.array?
          end
        end

        ::ActiveRecord::Associations::Preloader::Association::LoaderQuery
          .prepend(LoaderQuery)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
torque-postgresql-3.3.3 lib/torque/postgresql/associations/preloader/loader_query.rb
torque-postgresql-3.3.2 lib/torque/postgresql/associations/preloader/loader_query.rb
torque-postgresql-3.3.1 lib/torque/postgresql/associations/preloader/loader_query.rb