polymorphic_integer_type 2.2.5 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: ffe9ae194b9a0be401d770ef0b2dbcef7ebf20f39a8d3c5a6c84bb8247085610
4
- data.tar.gz: 5df31e9e5463c7cf1ed62d3417d93a8162de83ad8d2bb9e7ace34eb995813491
2
+ SHA1:
3
+ metadata.gz: 6c2cf440c0f3c18a7617ca1afb6ef64df29f5318
4
+ data.tar.gz: c69f7e53591f29f1ca30f34824fcd82cf0d8347f
5
5
  SHA512:
6
- metadata.gz: 07d497236946c160efe5d19c19b6cd63254d79d45088794ba71f6e916ba96c5e65313151c57122010e4a72a354363b358da4fc4c29a96b921d29a574f2b7be13
7
- data.tar.gz: 36c0844e4b66881bd7df5052b0dd2d8dfa3de1a1505d5932c179721239a50efad292bb3f47a98d86ac16b6e74702158fc11c8da6a361483e20c5b045e39dc4bc
6
+ metadata.gz: 6d53e634ae33cd8ea9be786f8e5d8416154773a5795bff1a9d9b943961336755d416fef200cea78655d67d74253b52d95d2c18d1949cbd71f98550bc40050e12
7
+ data.tar.gz: 3a35e05e8c28d847255c7f16168bda71f176f247f6260e2d54879e2a295764fd8f5a93de7dbbb146505040477a7e0e162755d6f8983e57de0ded30ba796d8158
@@ -34,6 +34,7 @@ module PolymorphicIntegerType
34
34
  mapping = self.class.send("#{foreign_type}_mapping")
35
35
  enum = mapping.key(klass.to_s)
36
36
  if klass.kind_of?(Class) && klass <= ActiveRecord::Base
37
+ enum ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)
37
38
  enum ||= mapping.key(klass.sti_name)
38
39
  enum ||= mapping.key(klass.base_class.to_s)
39
40
  enum ||= mapping.key(klass.base_class.sti_name)
@@ -45,6 +46,7 @@ module PolymorphicIntegerType
45
46
  define_method "#{name}=" do |record|
46
47
  super(record)
47
48
  send("#{foreign_type}=", record.class)
49
+ association(name).loaded!
48
50
  end
49
51
  end
50
52
 
@@ -69,14 +71,15 @@ module PolymorphicIntegerType
69
71
  if options[:as] && (polymorphic_type_mapping || integer_type)
70
72
  poly_type = options.delete(:as)
71
73
  polymorphic_type_mapping ||= PolymorphicIntegerType::Mapping[poly_type]
72
- if polymorphic_type_mapping == nil
74
+ if polymorphic_type_mapping.nil?
73
75
  raise "Polymorphic type mapping missing for #{poly_type.inspect}"
74
76
  end
75
77
 
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)
77
80
 
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}"
80
83
  end
81
84
 
82
85
  options[:foreign_key] ||= "#{poly_type}_id"
@@ -1,3 +1,3 @@
1
1
  module PolymorphicIntegerType
2
- VERSION = "2.2.5"
2
+ VERSION = "2.3.0"
3
3
  end
@@ -25,6 +25,25 @@ describe PolymorphicIntegerType do
25
25
  end
26
26
 
27
27
  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
+
28
47
  it "properly sets the source_type to the modified class name" do
29
48
  link = Link.new(source: Namespaced::Animal.new)
30
49
  expect(link.source_type).to eql "Animal"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polymorphic_integer_type
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.5
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle d'Oliveira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-20 00:00:00.000000000 Z
11
+ date: 2020-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -160,7 +160,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  - !ruby/object:Gem::Version
161
161
  version: '0'
162
162
  requirements: []
163
- rubygems_version: 3.0.6
163
+ rubyforge_project:
164
+ rubygems_version: 2.6.14
164
165
  signing_key:
165
166
  specification_version: 4
166
167
  summary: Use integers rather than strings for the _type field