|
1 | 1 | module PolymorphicIntegerType |
2 | 2 | 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 | + |
3 | 12 | 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) |
8 | 25 |
|
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) |
13 | 29 | end |
14 | | - result |
15 | 30 | end |
16 | 31 | end |
17 | 32 | end |
|
0 commit comments