polymorphic_integer_type 2.2.1 → 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 +5 -5
- data/Rakefile +11 -0
- data/gemfiles/Gemfile.rails-4.2-stable +8 -0
- data/gemfiles/Gemfile.rails-4.2-stable.lock +68 -0
- data/gemfiles/Gemfile.rails-5.0-stable +8 -0
- data/gemfiles/Gemfile.rails-5.0-stable.lock +66 -0
- data/gemfiles/Gemfile.rails-5.1-stable +7 -0
- data/gemfiles/Gemfile.rails-5.1-stable.lock +66 -0
- data/gemfiles/Gemfile.rails-5.2-stable +7 -0
- data/gemfiles/Gemfile.rails-5.2-stable.lock +66 -0
- data/lib/polymorphic_integer_type.rb +14 -3
- data/lib/polymorphic_integer_type/activerecord_4/belongs_to_polymorphic_association_extension.rb +10 -0
- data/lib/polymorphic_integer_type/{predicate_builder_extension.rb → activerecord_4/predicate_builder_extension.rb} +0 -0
- data/lib/polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension.rb +38 -0
- data/lib/polymorphic_integer_type/{polymorphic_array_value_extension.rb → activerecord_5_0_0/polymorphic_array_value_extension.rb} +0 -0
- data/lib/polymorphic_integer_type/extensions.rb +30 -18
- data/lib/polymorphic_integer_type/version.rb +1 -1
- data/spec/polymorphic_integer_type_spec.rb +45 -4
- data/spec/spec_helper.rb +3 -1
- data/spec/support/animal.rb +2 -1
- data/spec/support/link.rb +8 -0
- data/spec/support/namespaced_animal.rb +11 -0
- metadata +18 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 6c2cf440c0f3c18a7617ca1afb6ef64df29f5318
|
|
4
|
+
data.tar.gz: c69f7e53591f29f1ca30f34824fcd82cf0d8347f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6d53e634ae33cd8ea9be786f8e5d8416154773a5795bff1a9d9b943961336755d416fef200cea78655d67d74253b52d95d2c18d1949cbd71f98550bc40050e12
|
|
7
|
+
data.tar.gz: 3a35e05e8c28d847255c7f16168bda71f176f247f6260e2d54879e2a295764fd8f5a93de7dbbb146505040477a7e0e162755d6f8983e57de0ded30ba796d8158
|
data/Rakefile
CHANGED
|
@@ -2,6 +2,17 @@ require "bundler/gem_tasks"
|
|
|
2
2
|
require "yaml"
|
|
3
3
|
require "active_record"
|
|
4
4
|
|
|
5
|
+
namespace :test do
|
|
6
|
+
task :all do
|
|
7
|
+
Dir.glob("./gemfiles/Gemfile*").each do |gemfile|
|
|
8
|
+
next if gemfile.end_with?(".lock")
|
|
9
|
+
puts "Running specs for #{Pathname.new(gemfile).basename}"
|
|
10
|
+
system("BUNDLE_GEMFILE=#{gemfile} bundle install > /dev/null && BUNDLE_GEMFILE=#{gemfile} bundle exec rspec")
|
|
11
|
+
puts ""
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
5
16
|
namespace :db do
|
|
6
17
|
database_config = YAML.load(File.open("./spec/support/database.yml"))
|
|
7
18
|
admin_database_config = database_config.merge(database: "mysql")
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
GIT
|
|
2
|
+
remote: git://github.com/rails/rails.git
|
|
3
|
+
revision: e9d6b85f3e834ceea2aeabe4cbaa96a7c73eb896
|
|
4
|
+
branch: 4-2-stable
|
|
5
|
+
specs:
|
|
6
|
+
activemodel (4.2.11.1)
|
|
7
|
+
activesupport (= 4.2.11.1)
|
|
8
|
+
builder (~> 3.1)
|
|
9
|
+
activerecord (4.2.11.1)
|
|
10
|
+
activemodel (= 4.2.11.1)
|
|
11
|
+
activesupport (= 4.2.11.1)
|
|
12
|
+
arel (~> 6.0)
|
|
13
|
+
activesupport (4.2.11.1)
|
|
14
|
+
i18n (~> 0.7)
|
|
15
|
+
minitest (~> 5.1)
|
|
16
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
|
17
|
+
tzinfo (~> 1.1)
|
|
18
|
+
|
|
19
|
+
PATH
|
|
20
|
+
remote: ..
|
|
21
|
+
specs:
|
|
22
|
+
polymorphic_integer_type (2.2.4)
|
|
23
|
+
activerecord
|
|
24
|
+
|
|
25
|
+
GEM
|
|
26
|
+
remote: https://rubygems.org/
|
|
27
|
+
specs:
|
|
28
|
+
arel (6.0.4)
|
|
29
|
+
builder (3.2.4)
|
|
30
|
+
byebug (11.0.1)
|
|
31
|
+
concurrent-ruby (1.1.5)
|
|
32
|
+
diff-lcs (1.3)
|
|
33
|
+
i18n (0.9.5)
|
|
34
|
+
concurrent-ruby (~> 1.0)
|
|
35
|
+
minitest (5.13.0)
|
|
36
|
+
rake (13.0.1)
|
|
37
|
+
rspec (3.9.0)
|
|
38
|
+
rspec-core (~> 3.9.0)
|
|
39
|
+
rspec-expectations (~> 3.9.0)
|
|
40
|
+
rspec-mocks (~> 3.9.0)
|
|
41
|
+
rspec-core (3.9.0)
|
|
42
|
+
rspec-support (~> 3.9.0)
|
|
43
|
+
rspec-expectations (3.9.0)
|
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
45
|
+
rspec-support (~> 3.9.0)
|
|
46
|
+
rspec-mocks (3.9.0)
|
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
48
|
+
rspec-support (~> 3.9.0)
|
|
49
|
+
rspec-support (3.9.0)
|
|
50
|
+
sqlite3 (1.3.13)
|
|
51
|
+
thread_safe (0.3.6)
|
|
52
|
+
tzinfo (1.2.5)
|
|
53
|
+
thread_safe (~> 0.1)
|
|
54
|
+
|
|
55
|
+
PLATFORMS
|
|
56
|
+
ruby
|
|
57
|
+
|
|
58
|
+
DEPENDENCIES
|
|
59
|
+
activerecord!
|
|
60
|
+
bundler
|
|
61
|
+
byebug
|
|
62
|
+
polymorphic_integer_type!
|
|
63
|
+
rake
|
|
64
|
+
rspec
|
|
65
|
+
sqlite3 (~> 1.3.6)
|
|
66
|
+
|
|
67
|
+
BUNDLED WITH
|
|
68
|
+
1.16.1
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
GIT
|
|
2
|
+
remote: git://github.com/rails/rails.git
|
|
3
|
+
revision: ac6aa32f7cf66264ba87eabed7c042bb60bcf3a2
|
|
4
|
+
branch: 5-0-stable
|
|
5
|
+
specs:
|
|
6
|
+
activemodel (5.0.7.2)
|
|
7
|
+
activesupport (= 5.0.7.2)
|
|
8
|
+
activerecord (5.0.7.2)
|
|
9
|
+
activemodel (= 5.0.7.2)
|
|
10
|
+
activesupport (= 5.0.7.2)
|
|
11
|
+
arel (~> 7.0)
|
|
12
|
+
activesupport (5.0.7.2)
|
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
14
|
+
i18n (>= 0.7, < 2)
|
|
15
|
+
minitest (~> 5.1)
|
|
16
|
+
tzinfo (~> 1.1)
|
|
17
|
+
|
|
18
|
+
PATH
|
|
19
|
+
remote: ..
|
|
20
|
+
specs:
|
|
21
|
+
polymorphic_integer_type (2.2.4)
|
|
22
|
+
activerecord
|
|
23
|
+
|
|
24
|
+
GEM
|
|
25
|
+
remote: https://rubygems.org/
|
|
26
|
+
specs:
|
|
27
|
+
arel (7.1.4)
|
|
28
|
+
byebug (11.0.1)
|
|
29
|
+
concurrent-ruby (1.1.5)
|
|
30
|
+
diff-lcs (1.3)
|
|
31
|
+
i18n (1.7.0)
|
|
32
|
+
concurrent-ruby (~> 1.0)
|
|
33
|
+
minitest (5.13.0)
|
|
34
|
+
rake (13.0.1)
|
|
35
|
+
rspec (3.9.0)
|
|
36
|
+
rspec-core (~> 3.9.0)
|
|
37
|
+
rspec-expectations (~> 3.9.0)
|
|
38
|
+
rspec-mocks (~> 3.9.0)
|
|
39
|
+
rspec-core (3.9.0)
|
|
40
|
+
rspec-support (~> 3.9.0)
|
|
41
|
+
rspec-expectations (3.9.0)
|
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
43
|
+
rspec-support (~> 3.9.0)
|
|
44
|
+
rspec-mocks (3.9.0)
|
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
46
|
+
rspec-support (~> 3.9.0)
|
|
47
|
+
rspec-support (3.9.0)
|
|
48
|
+
sqlite3 (1.3.13)
|
|
49
|
+
thread_safe (0.3.6)
|
|
50
|
+
tzinfo (1.2.5)
|
|
51
|
+
thread_safe (~> 0.1)
|
|
52
|
+
|
|
53
|
+
PLATFORMS
|
|
54
|
+
ruby
|
|
55
|
+
|
|
56
|
+
DEPENDENCIES
|
|
57
|
+
activerecord!
|
|
58
|
+
bundler
|
|
59
|
+
byebug
|
|
60
|
+
polymorphic_integer_type!
|
|
61
|
+
rake
|
|
62
|
+
rspec
|
|
63
|
+
sqlite3 (~> 1.3.6)
|
|
64
|
+
|
|
65
|
+
BUNDLED WITH
|
|
66
|
+
1.16.1
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
GIT
|
|
2
|
+
remote: git://github.com/rails/rails.git
|
|
3
|
+
revision: 663206d20aec374a28a24bb43bc7b1233042ed9b
|
|
4
|
+
branch: 5-1-stable
|
|
5
|
+
specs:
|
|
6
|
+
activemodel (5.1.7)
|
|
7
|
+
activesupport (= 5.1.7)
|
|
8
|
+
activerecord (5.1.7)
|
|
9
|
+
activemodel (= 5.1.7)
|
|
10
|
+
activesupport (= 5.1.7)
|
|
11
|
+
arel (~> 8.0)
|
|
12
|
+
activesupport (5.1.7)
|
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
14
|
+
i18n (>= 0.7, < 2)
|
|
15
|
+
minitest (~> 5.1)
|
|
16
|
+
tzinfo (~> 1.1)
|
|
17
|
+
|
|
18
|
+
PATH
|
|
19
|
+
remote: ..
|
|
20
|
+
specs:
|
|
21
|
+
polymorphic_integer_type (2.2.4)
|
|
22
|
+
activerecord
|
|
23
|
+
|
|
24
|
+
GEM
|
|
25
|
+
remote: https://rubygems.org/
|
|
26
|
+
specs:
|
|
27
|
+
arel (8.0.0)
|
|
28
|
+
byebug (11.0.1)
|
|
29
|
+
concurrent-ruby (1.1.5)
|
|
30
|
+
diff-lcs (1.3)
|
|
31
|
+
i18n (1.7.0)
|
|
32
|
+
concurrent-ruby (~> 1.0)
|
|
33
|
+
minitest (5.13.0)
|
|
34
|
+
rake (13.0.1)
|
|
35
|
+
rspec (3.9.0)
|
|
36
|
+
rspec-core (~> 3.9.0)
|
|
37
|
+
rspec-expectations (~> 3.9.0)
|
|
38
|
+
rspec-mocks (~> 3.9.0)
|
|
39
|
+
rspec-core (3.9.0)
|
|
40
|
+
rspec-support (~> 3.9.0)
|
|
41
|
+
rspec-expectations (3.9.0)
|
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
43
|
+
rspec-support (~> 3.9.0)
|
|
44
|
+
rspec-mocks (3.9.0)
|
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
46
|
+
rspec-support (~> 3.9.0)
|
|
47
|
+
rspec-support (3.9.0)
|
|
48
|
+
sqlite3 (1.4.2)
|
|
49
|
+
thread_safe (0.3.6)
|
|
50
|
+
tzinfo (1.2.5)
|
|
51
|
+
thread_safe (~> 0.1)
|
|
52
|
+
|
|
53
|
+
PLATFORMS
|
|
54
|
+
ruby
|
|
55
|
+
|
|
56
|
+
DEPENDENCIES
|
|
57
|
+
activerecord!
|
|
58
|
+
bundler
|
|
59
|
+
byebug
|
|
60
|
+
polymorphic_integer_type!
|
|
61
|
+
rake
|
|
62
|
+
rspec
|
|
63
|
+
sqlite3
|
|
64
|
+
|
|
65
|
+
BUNDLED WITH
|
|
66
|
+
1.16.1
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
GIT
|
|
2
|
+
remote: git://github.com/rails/rails.git
|
|
3
|
+
revision: 892eab777c418135ce0646e91bc9ebb08a29ab9b
|
|
4
|
+
branch: 5-2-stable
|
|
5
|
+
specs:
|
|
6
|
+
activemodel (5.2.4.1)
|
|
7
|
+
activesupport (= 5.2.4.1)
|
|
8
|
+
activerecord (5.2.4.1)
|
|
9
|
+
activemodel (= 5.2.4.1)
|
|
10
|
+
activesupport (= 5.2.4.1)
|
|
11
|
+
arel (>= 9.0)
|
|
12
|
+
activesupport (5.2.4.1)
|
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
14
|
+
i18n (>= 0.7, < 2)
|
|
15
|
+
minitest (~> 5.1)
|
|
16
|
+
tzinfo (~> 1.1)
|
|
17
|
+
|
|
18
|
+
PATH
|
|
19
|
+
remote: ..
|
|
20
|
+
specs:
|
|
21
|
+
polymorphic_integer_type (2.2.4)
|
|
22
|
+
activerecord
|
|
23
|
+
|
|
24
|
+
GEM
|
|
25
|
+
remote: https://rubygems.org/
|
|
26
|
+
specs:
|
|
27
|
+
arel (9.0.0)
|
|
28
|
+
byebug (11.0.1)
|
|
29
|
+
concurrent-ruby (1.1.5)
|
|
30
|
+
diff-lcs (1.3)
|
|
31
|
+
i18n (1.7.0)
|
|
32
|
+
concurrent-ruby (~> 1.0)
|
|
33
|
+
minitest (5.13.0)
|
|
34
|
+
rake (13.0.1)
|
|
35
|
+
rspec (3.9.0)
|
|
36
|
+
rspec-core (~> 3.9.0)
|
|
37
|
+
rspec-expectations (~> 3.9.0)
|
|
38
|
+
rspec-mocks (~> 3.9.0)
|
|
39
|
+
rspec-core (3.9.0)
|
|
40
|
+
rspec-support (~> 3.9.0)
|
|
41
|
+
rspec-expectations (3.9.0)
|
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
43
|
+
rspec-support (~> 3.9.0)
|
|
44
|
+
rspec-mocks (3.9.0)
|
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
46
|
+
rspec-support (~> 3.9.0)
|
|
47
|
+
rspec-support (3.9.0)
|
|
48
|
+
sqlite3 (1.4.2)
|
|
49
|
+
thread_safe (0.3.6)
|
|
50
|
+
tzinfo (1.2.5)
|
|
51
|
+
thread_safe (~> 0.1)
|
|
52
|
+
|
|
53
|
+
PLATFORMS
|
|
54
|
+
ruby
|
|
55
|
+
|
|
56
|
+
DEPENDENCIES
|
|
57
|
+
activerecord!
|
|
58
|
+
bundler
|
|
59
|
+
byebug
|
|
60
|
+
polymorphic_integer_type!
|
|
61
|
+
rake
|
|
62
|
+
rspec
|
|
63
|
+
sqlite3
|
|
64
|
+
|
|
65
|
+
BUNDLED WITH
|
|
66
|
+
1.16.1
|
|
@@ -1,10 +1,21 @@
|
|
|
1
|
+
ACTIVE_RECORD_VERSION = Gem::Version.new(ActiveRecord::VERSION::STRING)
|
|
2
|
+
|
|
1
3
|
require "polymorphic_integer_type/version"
|
|
2
4
|
require "polymorphic_integer_type/extensions"
|
|
3
5
|
require "polymorphic_integer_type/mapping"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
|
|
7
|
+
if ACTIVE_RECORD_VERSION < Gem::Version.new("5")
|
|
8
|
+
require "polymorphic_integer_type/activerecord_4/predicate_builder_extension"
|
|
6
9
|
else
|
|
7
|
-
require "polymorphic_integer_type/polymorphic_array_value_extension"
|
|
10
|
+
require "polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
if ACTIVE_RECORD_VERSION >= Gem::Version.new("5.0") && ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
|
|
14
|
+
require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
if ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
|
|
18
|
+
require "polymorphic_integer_type/activerecord_4/belongs_to_polymorphic_association_extension"
|
|
8
19
|
end
|
|
9
20
|
|
|
10
21
|
module PolymorphicIntegerType; end
|
|
File without changes
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module PolymorphicIntegerType
|
|
2
|
+
module AssociationQueryHandlerExtension
|
|
3
|
+
def call(attribute, value)
|
|
4
|
+
queries = {}
|
|
5
|
+
table = value.associated_table
|
|
6
|
+
|
|
7
|
+
if value.base_class
|
|
8
|
+
queries[table.association_foreign_type.to_s] = polymorphic_value_for(value)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
queries[table.association_foreign_key.to_s] = value.ids
|
|
12
|
+
predicate_builder.build_from_hash(queries)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
protected
|
|
16
|
+
|
|
17
|
+
def polymorphic_value_for(query_value)
|
|
18
|
+
table = query_value.associated_table
|
|
19
|
+
association = table.send(:association)
|
|
20
|
+
klass = association.active_record
|
|
21
|
+
name = association.name
|
|
22
|
+
|
|
23
|
+
if klass.respond_to?("#{name}_type_mapping")
|
|
24
|
+
type_mapping = klass.send("#{name}_type_mapping")
|
|
25
|
+
|
|
26
|
+
type_mapping.key(query_value.value.class.sti_name) ||
|
|
27
|
+
type_mapping.key(query_value.base_class.to_s) ||
|
|
28
|
+
type_mapping.key(query_value.base_class.sti_name)
|
|
29
|
+
else
|
|
30
|
+
query_value.base_class.name
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
ActiveRecord::PredicateBuilder::AssociationQueryHandler.prepend(PolymorphicIntegerType::AssociationQueryHandlerExtension)
|
|
File without changes
|
|
@@ -24,23 +24,34 @@ module PolymorphicIntegerType
|
|
|
24
24
|
mapping
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
foreign_type_extension = Module.new do
|
|
28
|
+
define_method foreign_type do
|
|
29
|
+
t = super()
|
|
30
|
+
self.class.send("#{foreign_type}_mapping")[t]
|
|
31
|
+
end
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
define_method "#{foreign_type}=" do |klass|
|
|
34
|
+
mapping = self.class.send("#{foreign_type}_mapping")
|
|
35
|
+
enum = mapping.key(klass.to_s)
|
|
36
|
+
if klass.kind_of?(Class) && klass <= ActiveRecord::Base
|
|
37
|
+
enum ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)
|
|
38
|
+
enum ||= mapping.key(klass.sti_name)
|
|
39
|
+
enum ||= mapping.key(klass.base_class.to_s)
|
|
40
|
+
enum ||= mapping.key(klass.base_class.sti_name)
|
|
41
|
+
end
|
|
42
|
+
enum ||= klass if klass != NilClass
|
|
43
|
+
super(enum)
|
|
44
|
+
end
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
46
|
+
define_method "#{name}=" do |record|
|
|
47
|
+
super(record)
|
|
48
|
+
send("#{foreign_type}=", record.class)
|
|
49
|
+
association(name).loaded!
|
|
50
|
+
end
|
|
42
51
|
end
|
|
43
52
|
|
|
53
|
+
include(foreign_type_extension)
|
|
54
|
+
|
|
44
55
|
validate do
|
|
45
56
|
t = send(foreign_type)
|
|
46
57
|
unless t.nil? || mapping.values.include?(t)
|
|
@@ -60,14 +71,15 @@ module PolymorphicIntegerType
|
|
|
60
71
|
if options[:as] && (polymorphic_type_mapping || integer_type)
|
|
61
72
|
poly_type = options.delete(:as)
|
|
62
73
|
polymorphic_type_mapping ||= PolymorphicIntegerType::Mapping[poly_type]
|
|
63
|
-
if polymorphic_type_mapping
|
|
74
|
+
if polymorphic_type_mapping.nil?
|
|
64
75
|
raise "Polymorphic type mapping missing for #{poly_type.inspect}"
|
|
65
76
|
end
|
|
66
77
|
|
|
67
|
-
klass_mapping =
|
|
78
|
+
klass_mapping = polymorphic_type_mapping.key(polymorphic_name) if respond_to?(:polymorphic_name)
|
|
79
|
+
klass_mapping ||= polymorphic_type_mapping.key(sti_name)
|
|
68
80
|
|
|
69
|
-
if klass_mapping
|
|
70
|
-
raise "Class not found for #{
|
|
81
|
+
if klass_mapping.nil?
|
|
82
|
+
raise "Class not found for #{inspect} in polymorphic type mapping: #{polymorphic_type_mapping}"
|
|
71
83
|
end
|
|
72
84
|
|
|
73
85
|
options[:foreign_key] ||= "#{poly_type}_id"
|
|
@@ -86,7 +98,7 @@ module PolymorphicIntegerType
|
|
|
86
98
|
def retrieve_polymorphic_type_mapping(polymorphic_type:, class_name:)
|
|
87
99
|
return if polymorphic_type.nil?
|
|
88
100
|
|
|
89
|
-
belongs_to_class = class_name
|
|
101
|
+
belongs_to_class = compute_type(class_name)
|
|
90
102
|
method_name = "#{polymorphic_type}_type_mapping"
|
|
91
103
|
|
|
92
104
|
if belongs_to_class && belongs_to_class.respond_to?(method_name)
|
|
@@ -24,12 +24,52 @@ describe PolymorphicIntegerType do
|
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
26
|
|
|
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
|
+
|
|
47
|
+
it "properly sets the source_type to the modified class name" do
|
|
48
|
+
link = Link.new(source: Namespaced::Animal.new)
|
|
49
|
+
expect(link.source_type).to eql "Animal"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "can read dirty attributes from an associated object" do
|
|
53
|
+
animal = Namespaced::Animal.create!(name: "Oldie")
|
|
54
|
+
animal.name = "Newton"
|
|
55
|
+
link = Link.create!(source: animal)
|
|
56
|
+
|
|
57
|
+
expect(link.source.name).to eq("Newton")
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
27
61
|
context "when querying the associations" do
|
|
28
62
|
let(:source) { cat }
|
|
29
63
|
let(:target) { nil }
|
|
64
|
+
|
|
30
65
|
it "properly finds the object with a where" do
|
|
31
66
|
expect(Link.where(source: source, id: link.id).first).to eql link
|
|
32
67
|
end
|
|
68
|
+
|
|
69
|
+
it "properly finds the object when passing an array of sources" do
|
|
70
|
+
expect(Link.where(source: [source])).to eq [link]
|
|
71
|
+
end
|
|
72
|
+
|
|
33
73
|
it "properly finds the object with a find_by" do
|
|
34
74
|
expect(Link.find_by(source: source, id: link.id)).to eql link
|
|
35
75
|
end
|
|
@@ -96,7 +136,7 @@ describe PolymorphicIntegerType do
|
|
|
96
136
|
|
|
97
137
|
end
|
|
98
138
|
|
|
99
|
-
context "When using a relation to the links with
|
|
139
|
+
context "When using a relation to the links with eager loading" do
|
|
100
140
|
let!(:links){
|
|
101
141
|
[Link.create(source: source, target: kibble),
|
|
102
142
|
Link.create(source: source, target: water)]
|
|
@@ -111,7 +151,7 @@ describe PolymorphicIntegerType do
|
|
|
111
151
|
|
|
112
152
|
end
|
|
113
153
|
|
|
114
|
-
context "When using a through relation to the links with
|
|
154
|
+
context "When using a through relation to the links with eager loading" do
|
|
115
155
|
let!(:links){
|
|
116
156
|
[Link.create(source: source, target: kibble),
|
|
117
157
|
Link.create(source: source, target: water)]
|
|
@@ -126,7 +166,7 @@ describe PolymorphicIntegerType do
|
|
|
126
166
|
|
|
127
167
|
end
|
|
128
168
|
|
|
129
|
-
context "When
|
|
169
|
+
context "When eager loading the polymorphic association" do
|
|
130
170
|
let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) }
|
|
131
171
|
let(:source) { cat }
|
|
132
172
|
|
|
@@ -147,6 +187,7 @@ describe PolymorphicIntegerType do
|
|
|
147
187
|
end
|
|
148
188
|
|
|
149
189
|
|
|
190
|
+
|
|
150
191
|
end
|
|
151
192
|
|
|
152
193
|
context "when the association is an STI table" do
|
|
@@ -239,7 +280,7 @@ describe PolymorphicIntegerType do
|
|
|
239
280
|
|
|
240
281
|
self.table_name = "drinks"
|
|
241
282
|
|
|
242
|
-
has_many :
|
|
283
|
+
has_many :inline_link2s, as: :target
|
|
243
284
|
end
|
|
244
285
|
|
|
245
286
|
let!(:animal) { InlineAnimal2.create!(name: "Lucy") }
|
data/spec/spec_helper.rb
CHANGED
|
@@ -4,16 +4,18 @@ require 'polymorphic_integer_type'
|
|
|
4
4
|
require 'support/configuration'
|
|
5
5
|
require 'support/link'
|
|
6
6
|
require 'support/animal'
|
|
7
|
+
require 'support/namespaced_animal'
|
|
7
8
|
require 'support/dog'
|
|
8
9
|
require 'support/person'
|
|
9
10
|
require 'support/food'
|
|
10
11
|
require 'support/drink'
|
|
12
|
+
require 'byebug'
|
|
11
13
|
|
|
12
14
|
RSpec.configure do |config|
|
|
13
15
|
config.before(:suite) do
|
|
14
16
|
database_config = YAML.load(File.open("#{File.dirname(__FILE__)}/support/database.yml"))
|
|
15
17
|
ActiveRecord::Base.establish_connection(database_config)
|
|
16
|
-
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("5")
|
|
18
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("5.2.0")
|
|
17
19
|
ActiveRecord::MigrationContext.new("#{File.dirname(__FILE__)}/support/migrations").migrate
|
|
18
20
|
end
|
|
19
21
|
end
|
data/spec/support/animal.rb
CHANGED
data/spec/support/link.rb
CHANGED
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.
|
|
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:
|
|
11
|
+
date: 2020-10-28 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -108,11 +108,21 @@ files:
|
|
|
108
108
|
- README.md
|
|
109
109
|
- Rakefile
|
|
110
110
|
- bin/setup
|
|
111
|
+
- gemfiles/Gemfile.rails-4.2-stable
|
|
112
|
+
- gemfiles/Gemfile.rails-4.2-stable.lock
|
|
113
|
+
- gemfiles/Gemfile.rails-5.0-stable
|
|
114
|
+
- gemfiles/Gemfile.rails-5.0-stable.lock
|
|
115
|
+
- gemfiles/Gemfile.rails-5.1-stable
|
|
116
|
+
- gemfiles/Gemfile.rails-5.1-stable.lock
|
|
117
|
+
- gemfiles/Gemfile.rails-5.2-stable
|
|
118
|
+
- gemfiles/Gemfile.rails-5.2-stable.lock
|
|
111
119
|
- lib/polymorphic_integer_type.rb
|
|
120
|
+
- lib/polymorphic_integer_type/activerecord_4/belongs_to_polymorphic_association_extension.rb
|
|
121
|
+
- lib/polymorphic_integer_type/activerecord_4/predicate_builder_extension.rb
|
|
122
|
+
- lib/polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension.rb
|
|
123
|
+
- lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb
|
|
112
124
|
- lib/polymorphic_integer_type/extensions.rb
|
|
113
125
|
- lib/polymorphic_integer_type/mapping.rb
|
|
114
|
-
- lib/polymorphic_integer_type/polymorphic_array_value_extension.rb
|
|
115
|
-
- lib/polymorphic_integer_type/predicate_builder_extension.rb
|
|
116
126
|
- lib/polymorphic_integer_type/version.rb
|
|
117
127
|
- polymorphic_integer_type.gemspec
|
|
118
128
|
- spec/polymorphic_integer_type_spec.rb
|
|
@@ -129,6 +139,7 @@ files:
|
|
|
129
139
|
- spec/support/migrations/3_create_person_table.rb
|
|
130
140
|
- spec/support/migrations/4_create_food_table.rb
|
|
131
141
|
- spec/support/migrations/5_create_drink_table.rb
|
|
142
|
+
- spec/support/namespaced_animal.rb
|
|
132
143
|
- spec/support/person.rb
|
|
133
144
|
homepage: ''
|
|
134
145
|
licenses:
|
|
@@ -149,7 +160,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
149
160
|
- !ruby/object:Gem::Version
|
|
150
161
|
version: '0'
|
|
151
162
|
requirements: []
|
|
152
|
-
|
|
163
|
+
rubyforge_project:
|
|
164
|
+
rubygems_version: 2.6.14
|
|
153
165
|
signing_key:
|
|
154
166
|
specification_version: 4
|
|
155
167
|
summary: Use integers rather than strings for the _type field
|
|
@@ -168,4 +180,5 @@ test_files:
|
|
|
168
180
|
- spec/support/migrations/3_create_person_table.rb
|
|
169
181
|
- spec/support/migrations/4_create_food_table.rb
|
|
170
182
|
- spec/support/migrations/5_create_drink_table.rb
|
|
183
|
+
- spec/support/namespaced_animal.rb
|
|
171
184
|
- spec/support/person.rb
|