Skip to content

Commit fa3a809

Browse files
author
Kyle d'Oliveira
authored
Merge pull request #31 from doliveirakn/polymorphic-names-support
Add support for the rails 5.2 polymorphic name method
2 parents 22755f2 + 179c580 commit fa3a809

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

lib/polymorphic_integer_type/extensions.rb

Lines changed: 7 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)
@@ -45,6 +46,7 @@ def belongs_to(name, scope = nil, **options)
4546
define_method "#{name}=" do |record|
4647
super(record)
4748
send("#{foreign_type}=", record.class)
49+
association(name).loaded!
4850
end
4951
end
5052

@@ -69,14 +71,15 @@ def remove_type_and_establish_mapping(name, options, scope)
6971
if options[:as] && (polymorphic_type_mapping || integer_type)
7072
poly_type = options.delete(:as)
7173
polymorphic_type_mapping ||= PolymorphicIntegerType::Mapping[poly_type]
72-
if polymorphic_type_mapping == nil
74+
if polymorphic_type_mapping.nil?
7375
raise "Polymorphic type mapping missing for #{poly_type.inspect}"
7476
end
7577

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

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

8285
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)