Sha256: c389cc48bac1704ca338f7d087e75aaa81dbfe00e5f5ed61ec7c5fc0b63d2fc3

Contents?: true

Size: 1.57 KB

Versions: 32

Compression:

Stored size: 1.57 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

      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

32 entries across 32 versions & 4 rubygems

Version Path
activerecord-5.0.7.2 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.7.1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.7 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.6 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.6.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.5 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.5.rc2 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.5.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.4 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.4.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.3 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.2 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
activerecord-5.0.2.rc1 lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb