Sha256: 0ecfd6760c7751b097906d27bf6e74e1c99ec956bbd2ee4864c747797b3bccbf

Contents?: true

Size: 1.72 KB

Versions: 20

Compression:

Stored size: 1.72 KB

Contents

module ActiveRecord
  class PredicateBuilder
    class PolymorphicArrayHandler # :nodoc:
      def initialize(predicate_builder)
        @predicate_builder = predicate_builder
      end

      def call(attribute, value)
        table = value.associated_table
        queries = value.type_to_ids_mapping.map do |type, ids|
          { table.association_foreign_type.to_s => type, table.association_foreign_key.to_s => ids }
        end

        predicates = queries.map { |query| predicate_builder.build_from_hash(query) }

        if predicates.size > 1
          type_and_ids_predicates = predicates.map { |type_predicate, id_predicate| Arel::Nodes::Grouping.new(type_predicate.and(id_predicate)) }
          type_and_ids_predicates.inject(&:or)
        else
          predicates.first
        end
      end

      # TODO Change this to private once we've dropped Ruby 2.2 support.
      # Workaround for Ruby 2.2 "private attribute?" warning.
      protected

        attr_reader :predicate_builder
    end

    class PolymorphicArrayValue # :nodoc:
      attr_reader :associated_table, :values

      def initialize(associated_table, values)
        @associated_table = associated_table
        @values = values
      end

      def type_to_ids_mapping
        default_hash = Hash.new { |hsh, key| hsh[key] = [] }
        values.each_with_object(default_hash) { |value, hash| hash[base_class(value).name] << convert_to_id(value) }
      end

      private

        def primary_key(value)
          associated_table.association_primary_key(base_class(value))
        end

        def base_class(value)
          value.class.base_class
        end

        def convert_to_id(value)
          value._read_attribute(primary_key(value))
        end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
activerecord-5.1.7 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.7.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.6.2 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.6.1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.6 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.5 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.5.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.4 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.4.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.3 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.3.rc3 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.3.rc2 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.3.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.2 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.2.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.0 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.0.rc2 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.0.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.1.0.beta1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb