Sha256: d39c331fafd2458cd23a44c73e5a63b5e34a00eee0b78d348ffa409a3265e007

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module PolymorphicIntegerType
  module PolymorphicArrayValueExtension

    # original method:
    # def type_to_ids_mapping
    #   default_hash = Hash.new { |hsh, key| hsh[key] = [] }
    #   result = values.each_with_object(default_hash) do |value, hash|
    #     hash[klass(value).polymorphic_name] << convert_to_id(value)
    #   end
    # end

    def type_to_ids_mapping
      association = @associated_table.send(:association)
      name = association.name
      default_hash = Hash.new { |hsh, key| hsh[key] = [] }
      values.each_with_object(default_hash) do |value, hash|
        klass = respond_to?(:klass, true) ? klass(value) : value.class
        if association.active_record.respond_to?("#{name}_type_mapping")
          mapping = association.active_record.send("#{name}_type_mapping")
          key ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)
          key ||= mapping.key(klass.sti_name)
          key ||= mapping.key(klass.base_class.to_s)
          key ||= mapping.key(klass.base_class.sti_name)

          hash[key] << convert_to_id(value)
        else
          hash[klass.polymorphic_name] << convert_to_id(value)
        end
      end
    end
  end
end

ActiveRecord::PredicateBuilder::PolymorphicArrayValue.prepend(PolymorphicIntegerType::PolymorphicArrayValueExtension)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
polymorphic_integer_type-3.0.0 lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb
polymorphic_integer_type-2.3.1 lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb