Skip to content

Commit c1f717a

Browse files
author
Kyle d'Oliveira
committed
Introduce a new fallback that happens first
1 parent 22755f2 commit c1f717a

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

lib/polymorphic_integer_type/extensions.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def belongs_to(name, scope = nil, **options)
3434
mapping = self.class.send("#{foreign_type}_mapping")
3535
enum = mapping.key(klass.to_s)
3636
if klass.kind_of?(Class) && klass <= ActiveRecord::Base
37+
enum ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)
3738
enum ||= mapping.key(klass.sti_name)
3839
enum ||= mapping.key(klass.base_class.to_s)
3940
enum ||= mapping.key(klass.base_class.sti_name)
@@ -69,14 +70,15 @@ def remove_type_and_establish_mapping(name, options, scope)
6970
if options[:as] && (polymorphic_type_mapping || integer_type)
7071
poly_type = options.delete(:as)
7172
polymorphic_type_mapping ||= PolymorphicIntegerType::Mapping[poly_type]
72-
if polymorphic_type_mapping == nil
73+
if polymorphic_type_mapping.nil?
7374
raise "Polymorphic type mapping missing for #{poly_type.inspect}"
7475
end
7576

76-
klass_mapping = (polymorphic_type_mapping || {}).key(sti_name)
77+
klass_mapping = polymorphic_type_mapping.key(polymorphic_name) if respond_to?(:polymorphic_name)
78+
klass_mapping ||= polymorphic_type_mapping.key(sti_name)
7779

78-
if klass_mapping == nil
79-
raise "Class not found for #{sti_name.inspect} in polymorphic type mapping: #{polymorphic_type_mapping}"
80+
if klass_mapping.nil?
81+
raise "Class not found for #{inspect} in polymorphic type mapping: #{polymorphic_type_mapping}"
8082
end
8183

8284
options[:foreign_key] ||= "#{poly_type}_id"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module PolymorphicIntegerType
2-
VERSION = "2.2.5"
2+
VERSION = "2.3.0"
33
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@
2525
end
2626

2727
context "when the source is a class that modifies the sti_name or polymorphic_name" do
28+
context "and we leverage the polymorphic_name" do
29+
before do
30+
allow(PolymorphicIntegerType).to receive(:use_polymorphic_name).and_return(true)
31+
end
32+
33+
it "properly sets the source_type to the modified class name" do
34+
link = Link.new(source: Namespaced::Animal.new)
35+
expect(link.source_type).to eql "Animal"
36+
end
37+
38+
it "can read dirty attributes from an associated object" do
39+
animal = Namespaced::Animal.create!(name: "Oldie")
40+
animal.name = "Newton"
41+
link = Link.create!(source: animal)
42+
43+
expect(link.source.name).to eq("Newton")
44+
end
45+
end
46+
2847
it "properly sets the source_type to the modified class name" do
2948
link = Link.new(source: Namespaced::Animal.new)
3049
expect(link.source_type).to eql "Animal"

0 commit comments

Comments
 (0)