<commit_before>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|
        if association.active_record.respond_to?("#{name}_type_mapping")
          mapping = association.active_record.send("#{name}_type_mapping")
          klass = klass(value)
          key ||= mapping.key(klass.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)<commit_msg>make sure klass is defined for both paths<commit_after>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 = klass(value)
        if association.active_record.respond_to?("#{name}_type_mapping")
          mapping = association.active_record.send("#{name}_type_mapping")
          key ||= mapping.key(klass.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)
{'fraction_non_alphanumeric': 0.10819165378670788, 'fraction_numerical': 0.0, 'mean_word_length': 3.2442622950819673, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1120185', 'n_tokens_mistral': 840, 'n_tokens_neox': 827, 'n_words': 130}