Skip to content

Commit 531e39a

Browse files
authored
Merge pull request #37 from Drew-Goddyn/fix-type-to-ids-mapping-bug
fix bug with selecting correct class name from type mapping values
2 parents 2ca2095 + 4c1ff5f commit 531e39a

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def type_to_ids_mapping
1414
name = association.name
1515
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
1616
values.each_with_object(default_hash) do |value, hash|
17-
klass = respond_to?(:klass) ? klass(value) : klass.class
17+
klass = respond_to?(:klass, true) ? klass(value) : value.class
1818
if association.active_record.respond_to?("#{name}_type_mapping")
1919
mapping = association.active_record.send("#{name}_type_mapping")
2020
key ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)

lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ module Associations
33
class BelongsToPolymorphicAssociation < BelongsToAssociation
44
private def replace_keys(record)
55
super
6-
owner[reflection.foreign_type] = record.class
6+
unless record.nil?
7+
owner[reflection.foreign_type] = record.class.base_class
8+
end
79
end
810
end
911
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@
117117
it "properly finds the object with a find_by" do
118118
expect(Link.find_by(source: source, id: link.id)).to eql link
119119
end
120+
121+
context "when source and target are namespaced without modifying polymorphic_name" do
122+
it "properly finds the object" do
123+
plant = Namespaced::Plant.create(name: "Mighty", kind: "Oak", owner: owner)
124+
activity = Namespaced::Activity.create(name: "swaying")
125+
link = Link.create(source: plant, target: activity)
126+
expect(Link.where(source: plant, id: link.id).first).to eql link
127+
end
128+
end
120129
end
121130

122131
shared_examples "proper source" do

0 commit comments

Comments
 (0)