Skip to content

Commit 847f09c

Browse files
committed
fix bug with selecting correc class name from type mapping values
1 parent 2ca2095 commit 847f09c

2 files changed

Lines changed: 10 additions & 1 deletion

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)

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)