Skip to content

Commit bd110dc

Browse files
committed
With the introduction of #polymorphic_name we need additional checks to make sure our type mappings
include namespaces. If they don't we can fall back to other methods like #sti_name, base_class.to_s, or base_class.sti_name until we get a hit.
1 parent fa3a809 commit bd110dc

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
module PolymorphicIntegerType
22
module PolymorphicArrayValueExtension
3+
4+
# original method:
5+
# def type_to_ids_mapping
6+
# default_hash = Hash.new { |hsh, key| hsh[key] = [] }
7+
# result = values.each_with_object(default_hash) do |value, hash|
8+
# hash[klass(value).polymorphic_name] << convert_to_id(value)
9+
# end
10+
# end
11+
312
def type_to_ids_mapping
4-
super.tap do |result|
5-
association = @associated_table.send(:association)
6-
klass = association.active_record
7-
name = association.name
13+
association = @associated_table.send(:association)
14+
name = association.name
15+
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
16+
17+
values.each_with_object(default_hash) do |value, hash|
18+
if association.active_record.respond_to?("#{name}_type_mapping")
19+
mapping = association.active_record.send("#{name}_type_mapping")
20+
klass = klass(value)
21+
key ||= mapping.key(klass.polymorphic_name)
22+
key ||= mapping.key(klass.sti_name)
23+
key ||= mapping.key(klass.base_class.to_s)
24+
key ||= mapping.key(klass.base_class.sti_name)
825

9-
if klass.respond_to?("#{name}_type_mapping")
10-
result.transform_keys! do |key|
11-
klass.send("#{name}_type_mapping").key(key)
12-
end
26+
hash[key] << convert_to_id(value)
27+
else
28+
hash[klass.polymorphic_name] << convert_to_id(value)
1329
end
14-
result
1530
end
1631
end
1732
end

0 commit comments

Comments
 (0)