polymorphic_integer_type 2.2.3 → 3.0.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/README.md +9 -11
- data/Rakefile +11 -0
- data/gemfiles/Gemfile.rails-5.0-stable +8 -0
- data/gemfiles/Gemfile.rails-5.0-stable.lock +74 -0
- data/gemfiles/Gemfile.rails-5.1-stable +7 -0
- data/gemfiles/Gemfile.rails-5.1-stable.lock +74 -0
- data/gemfiles/Gemfile.rails-5.2-stable +7 -0
- data/gemfiles/Gemfile.rails-5.2-stable.lock +74 -0
- data/gemfiles/Gemfile.rails-6-0-stable +7 -0
- data/gemfiles/Gemfile.rails-6-0-stable.lock +74 -0
- data/lib/polymorphic_integer_type.rb +8 -4
- data/lib/polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension.rb +38 -0
- data/lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb +34 -0
- data/lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb +12 -0
- data/lib/polymorphic_integer_type/extensions.rb +8 -31
- data/lib/polymorphic_integer_type/mapping.rb +1 -1
- data/lib/polymorphic_integer_type/module_generator.rb +34 -0
- data/lib/polymorphic_integer_type/version.rb +1 -1
- data/polymorphic_integer_type.gemspec +2 -2
- data/spec/polymorphic_integer_type_spec.rb +94 -20
- data/spec/spec_helper.rb +11 -2
- data/spec/support/configuration.rb +2 -2
- data/spec/support/link.rb +0 -8
- data/spec/support/migrations/6_create_plant_table.rb +17 -0
- data/spec/support/migrations/7_create_activity_table.rb +15 -0
- data/spec/support/namespaced_activity.rb +11 -0
- data/spec/support/namespaced_animal.rb +5 -1
- data/spec/support/namespaced_plant.rb +11 -0
- metadata +31 -14
- data/lib/polymorphic_integer_type/polymorphic_array_value_extension.rb +0 -20
- data/lib/polymorphic_integer_type/predicate_builder_extension.rb +0 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 96fa454bca5e242711a9cc17183c3dbfe7b8c9303bd0bbe1cd246d030761639d
|
4
|
+
data.tar.gz: 536c3c02769ef0e020e300225e9e1cf453235d5341adc6f98cd973e1d7ea79d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dae28edc37bb45b1852a9d1f60347e74496d2ba371645514654ae9807a680de40d5487779b219a0fef3cad4ce220b2f0444e51c9907dbd6269653d2082eff1bf
|
7
|
+
data.tar.gz: 32e6df91034dd5423d8c3787512246e570a1cec2d5d85bdfee2eed2d1932640562dc9ef08881aa6fe09edb1d218597613833ac56389f3807327a28cef7e517db
|
data/README.md
CHANGED
@@ -6,11 +6,11 @@ Rails' polymorphic associations are pretty useful. The example they give to set
|
|
6
6
|
class Picture < ActiveRecord::Base
|
7
7
|
belongs_to :imageable, polymorphic: true
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
class Employee < ActiveRecord::Base
|
11
11
|
has_many :pictures, as: :imageable
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
class Product < ActiveRecord::Base
|
15
15
|
has_many :pictures, as: :imageable
|
16
16
|
end
|
@@ -31,7 +31,7 @@ class CreatePictures < ActiveRecord::Migration
|
|
31
31
|
end
|
32
32
|
```
|
33
33
|
|
34
|
-
The problem with this approach is that `imageable_type` is a string (and by default it is 255 characters). This is a little ridiculous. For comparison, if we had a state machine with X states, would we describe the states with strings `"State1", "State2", etc` or would we just enumerate the state column and make it an integer? This gem will allow us to use an integer for the `imageable_type` column.
|
34
|
+
The problem with this approach is that `imageable_type` is a string (and by default it is 255 characters). This is a little ridiculous. For comparison, if we had a state machine with X states, would we describe the states with strings `"State1", "State2", etc` or would we just enumerate the state column and make it an integer? This gem will allow us to use an integer for the `imageable_type` column.
|
35
35
|
|
36
36
|
## Installation
|
37
37
|
|
@@ -47,8 +47,6 @@ Or install it yourself as:
|
|
47
47
|
|
48
48
|
$ gem install polymorphic_integer_type
|
49
49
|
|
50
|
-
For Rails 3.2 use version < 2. Version >= 2 has been tested on Rails 4.2 and Ruby 2.1
|
51
|
-
|
52
50
|
## Usage
|
53
51
|
|
54
52
|
For the model where the `belongs_to` is defined, include `PolymorphicIntegerType::Extensions` and set the `polymorphic:` option to a hash that maps an integer stored in the database to the name of a Ruby class.
|
@@ -60,7 +58,7 @@ class Picture < ActiveRecord::Base
|
|
60
58
|
belongs_to :imageable, polymorphic: {1 => "Employee", 2 => "Product"}
|
61
59
|
end
|
62
60
|
```
|
63
|
-
|
61
|
+
|
64
62
|
Next, include `PolymorphicIntegerType::Extensions` into any of the models that point back to the polymorphic integer type association (e.g., `Picture#imageable`) and add a [polymorphic association using `as:`](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations).
|
65
63
|
|
66
64
|
```ruby
|
@@ -69,7 +67,7 @@ class Employee < ActiveRecord::Base
|
|
69
67
|
|
70
68
|
has_many :pictures, as: :imageable
|
71
69
|
end
|
72
|
-
|
70
|
+
|
73
71
|
class Product < ActiveRecord::Base
|
74
72
|
include PolymorphicIntegerType::Extensions
|
75
73
|
|
@@ -84,10 +82,10 @@ You can also store polymorphic type mappings separate from your models. This sho
|
|
84
82
|
```ruby
|
85
83
|
PolymorphicIntegerType::Mapping.configuration do |config|
|
86
84
|
config.add :imageable, {1 => "Employee", 2 => "Product" }
|
87
|
-
end
|
85
|
+
end
|
88
86
|
```
|
89
87
|
|
90
|
-
Note: The mapping here can start from whatever integer you wish, but I would advise not using 0. The reason being that if you had a new class, for instance `Avatar`, and also wanted to use this polymorphic association but forgot to include it in the mapping, it would effectively get `to_i` called on it and stored in the database. `"Avatar".to_i == 0`, so if your mapping included 0, this would create a weird bug.
|
88
|
+
Note: The mapping here can start from whatever integer you wish, but I would advise not using 0. The reason being that if you had a new class, for instance `Avatar`, and also wanted to use this polymorphic association but forgot to include it in the mapping, it would effectively get `to_i` called on it and stored in the database. `"Avatar".to_i == 0`, so if your mapping included 0, this would create a weird bug.
|
91
89
|
|
92
90
|
### Migrating an existing association
|
93
91
|
|
@@ -95,14 +93,14 @@ If you want to convert a polymorphic association that is already a string, you'l
|
|
95
93
|
|
96
94
|
```ruby
|
97
95
|
class PictureToPolymorphicIntegerType < ActiveRecord::Migration
|
98
|
-
|
96
|
+
|
99
97
|
def up
|
100
98
|
change_table :pictures do |t|
|
101
99
|
t.integer :new_imageable_type
|
102
100
|
end
|
103
101
|
|
104
102
|
execute <<-SQL
|
105
|
-
UPDATE
|
103
|
+
UPDATE picture
|
106
104
|
SET new_imageable_type = CASE imageable_type
|
107
105
|
WHEN 'Employee' THEN 1
|
108
106
|
WHEN 'Product' THEN 2
|
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,74 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://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 (3.0.0)
|
22
|
+
activerecord (< 6.1)
|
23
|
+
|
24
|
+
GEM
|
25
|
+
remote: https://rubygems.org/
|
26
|
+
specs:
|
27
|
+
arel (7.1.4)
|
28
|
+
byebug (11.1.3)
|
29
|
+
coderay (1.1.3)
|
30
|
+
concurrent-ruby (1.1.8)
|
31
|
+
diff-lcs (1.4.4)
|
32
|
+
i18n (1.8.10)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
method_source (1.0.0)
|
35
|
+
minitest (5.14.4)
|
36
|
+
pry (0.13.1)
|
37
|
+
coderay (~> 1.1)
|
38
|
+
method_source (~> 1.0)
|
39
|
+
pry-byebug (3.9.0)
|
40
|
+
byebug (~> 11.0)
|
41
|
+
pry (~> 0.13.0)
|
42
|
+
rake (13.0.3)
|
43
|
+
rspec (3.10.0)
|
44
|
+
rspec-core (~> 3.10.0)
|
45
|
+
rspec-expectations (~> 3.10.0)
|
46
|
+
rspec-mocks (~> 3.10.0)
|
47
|
+
rspec-core (3.10.1)
|
48
|
+
rspec-support (~> 3.10.0)
|
49
|
+
rspec-expectations (3.10.1)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.10.0)
|
52
|
+
rspec-mocks (3.10.2)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.10.0)
|
55
|
+
rspec-support (3.10.2)
|
56
|
+
sqlite3 (1.3.13)
|
57
|
+
thread_safe (0.3.6)
|
58
|
+
tzinfo (1.2.9)
|
59
|
+
thread_safe (~> 0.1)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
activerecord!
|
66
|
+
bundler
|
67
|
+
polymorphic_integer_type!
|
68
|
+
pry-byebug
|
69
|
+
rake
|
70
|
+
rspec
|
71
|
+
sqlite3 (~> 1.3.6)
|
72
|
+
|
73
|
+
BUNDLED WITH
|
74
|
+
2.1.4
|
@@ -0,0 +1,74 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://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 (3.0.0)
|
22
|
+
activerecord (< 6.1)
|
23
|
+
|
24
|
+
GEM
|
25
|
+
remote: https://rubygems.org/
|
26
|
+
specs:
|
27
|
+
arel (8.0.0)
|
28
|
+
byebug (11.1.3)
|
29
|
+
coderay (1.1.3)
|
30
|
+
concurrent-ruby (1.1.8)
|
31
|
+
diff-lcs (1.4.4)
|
32
|
+
i18n (1.8.10)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
method_source (1.0.0)
|
35
|
+
minitest (5.14.4)
|
36
|
+
pry (0.13.1)
|
37
|
+
coderay (~> 1.1)
|
38
|
+
method_source (~> 1.0)
|
39
|
+
pry-byebug (3.9.0)
|
40
|
+
byebug (~> 11.0)
|
41
|
+
pry (~> 0.13.0)
|
42
|
+
rake (13.0.3)
|
43
|
+
rspec (3.10.0)
|
44
|
+
rspec-core (~> 3.10.0)
|
45
|
+
rspec-expectations (~> 3.10.0)
|
46
|
+
rspec-mocks (~> 3.10.0)
|
47
|
+
rspec-core (3.10.1)
|
48
|
+
rspec-support (~> 3.10.0)
|
49
|
+
rspec-expectations (3.10.1)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.10.0)
|
52
|
+
rspec-mocks (3.10.2)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.10.0)
|
55
|
+
rspec-support (3.10.2)
|
56
|
+
sqlite3 (1.4.2)
|
57
|
+
thread_safe (0.3.6)
|
58
|
+
tzinfo (1.2.9)
|
59
|
+
thread_safe (~> 0.1)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
activerecord!
|
66
|
+
bundler
|
67
|
+
polymorphic_integer_type!
|
68
|
+
pry-byebug
|
69
|
+
rake
|
70
|
+
rspec
|
71
|
+
sqlite3
|
72
|
+
|
73
|
+
BUNDLED WITH
|
74
|
+
2.1.4
|
@@ -0,0 +1,74 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/rails/rails.git
|
3
|
+
revision: 48661542a2607d55f436438fe21001d262e61fec
|
4
|
+
branch: 5-2-stable
|
5
|
+
specs:
|
6
|
+
activemodel (5.2.6)
|
7
|
+
activesupport (= 5.2.6)
|
8
|
+
activerecord (5.2.6)
|
9
|
+
activemodel (= 5.2.6)
|
10
|
+
activesupport (= 5.2.6)
|
11
|
+
arel (>= 9.0)
|
12
|
+
activesupport (5.2.6)
|
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 (3.0.0)
|
22
|
+
activerecord (< 6.1)
|
23
|
+
|
24
|
+
GEM
|
25
|
+
remote: https://rubygems.org/
|
26
|
+
specs:
|
27
|
+
arel (9.0.0)
|
28
|
+
byebug (11.1.3)
|
29
|
+
coderay (1.1.3)
|
30
|
+
concurrent-ruby (1.1.8)
|
31
|
+
diff-lcs (1.4.4)
|
32
|
+
i18n (1.8.10)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
method_source (1.0.0)
|
35
|
+
minitest (5.14.4)
|
36
|
+
pry (0.13.1)
|
37
|
+
coderay (~> 1.1)
|
38
|
+
method_source (~> 1.0)
|
39
|
+
pry-byebug (3.9.0)
|
40
|
+
byebug (~> 11.0)
|
41
|
+
pry (~> 0.13.0)
|
42
|
+
rake (13.0.3)
|
43
|
+
rspec (3.10.0)
|
44
|
+
rspec-core (~> 3.10.0)
|
45
|
+
rspec-expectations (~> 3.10.0)
|
46
|
+
rspec-mocks (~> 3.10.0)
|
47
|
+
rspec-core (3.10.1)
|
48
|
+
rspec-support (~> 3.10.0)
|
49
|
+
rspec-expectations (3.10.1)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.10.0)
|
52
|
+
rspec-mocks (3.10.2)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.10.0)
|
55
|
+
rspec-support (3.10.2)
|
56
|
+
sqlite3 (1.4.2)
|
57
|
+
thread_safe (0.3.6)
|
58
|
+
tzinfo (1.2.9)
|
59
|
+
thread_safe (~> 0.1)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
activerecord!
|
66
|
+
bundler
|
67
|
+
polymorphic_integer_type!
|
68
|
+
pry-byebug
|
69
|
+
rake
|
70
|
+
rspec
|
71
|
+
sqlite3
|
72
|
+
|
73
|
+
BUNDLED WITH
|
74
|
+
2.1.4
|
@@ -0,0 +1,74 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/rails/rails.git
|
3
|
+
revision: ef97441036e0ebbe1aa2108d59c408707f998ffd
|
4
|
+
branch: 6-0-stable
|
5
|
+
specs:
|
6
|
+
activemodel (6.0.3.7)
|
7
|
+
activesupport (= 6.0.3.7)
|
8
|
+
activerecord (6.0.3.7)
|
9
|
+
activemodel (= 6.0.3.7)
|
10
|
+
activesupport (= 6.0.3.7)
|
11
|
+
activesupport (6.0.3.7)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
17
|
+
|
18
|
+
PATH
|
19
|
+
remote: ..
|
20
|
+
specs:
|
21
|
+
polymorphic_integer_type (3.0.0)
|
22
|
+
activerecord (< 6.1)
|
23
|
+
|
24
|
+
GEM
|
25
|
+
remote: https://rubygems.org/
|
26
|
+
specs:
|
27
|
+
byebug (11.1.3)
|
28
|
+
coderay (1.1.3)
|
29
|
+
concurrent-ruby (1.1.8)
|
30
|
+
diff-lcs (1.4.4)
|
31
|
+
i18n (1.8.10)
|
32
|
+
concurrent-ruby (~> 1.0)
|
33
|
+
method_source (1.0.0)
|
34
|
+
minitest (5.14.4)
|
35
|
+
pry (0.13.1)
|
36
|
+
coderay (~> 1.1)
|
37
|
+
method_source (~> 1.0)
|
38
|
+
pry-byebug (3.9.0)
|
39
|
+
byebug (~> 11.0)
|
40
|
+
pry (~> 0.13.0)
|
41
|
+
rake (13.0.3)
|
42
|
+
rspec (3.10.0)
|
43
|
+
rspec-core (~> 3.10.0)
|
44
|
+
rspec-expectations (~> 3.10.0)
|
45
|
+
rspec-mocks (~> 3.10.0)
|
46
|
+
rspec-core (3.10.1)
|
47
|
+
rspec-support (~> 3.10.0)
|
48
|
+
rspec-expectations (3.10.1)
|
49
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
50
|
+
rspec-support (~> 3.10.0)
|
51
|
+
rspec-mocks (3.10.2)
|
52
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
53
|
+
rspec-support (~> 3.10.0)
|
54
|
+
rspec-support (3.10.2)
|
55
|
+
sqlite3 (1.4.2)
|
56
|
+
thread_safe (0.3.6)
|
57
|
+
tzinfo (1.2.9)
|
58
|
+
thread_safe (~> 0.1)
|
59
|
+
zeitwerk (2.4.2)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
activerecord!
|
66
|
+
bundler
|
67
|
+
polymorphic_integer_type!
|
68
|
+
pry-byebug
|
69
|
+
rake
|
70
|
+
rspec
|
71
|
+
sqlite3
|
72
|
+
|
73
|
+
BUNDLED WITH
|
74
|
+
2.1.4
|
@@ -1,10 +1,14 @@
|
|
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
|
-
|
6
|
+
require "polymorphic_integer_type/module_generator"
|
7
|
+
require "polymorphic_integer_type/belongs_to_polymorphic_association_extension"
|
8
|
+
require "polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension"
|
9
|
+
|
10
|
+
if ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
|
11
|
+
require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
|
8
12
|
end
|
9
13
|
|
10
14
|
module PolymorphicIntegerType; end
|
@@ -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)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module PolymorphicIntegerType
|
2
|
+
module PolymorphicArrayValueExtension
|
3
|
+
|
4
|
+
# original method:
|
5
|
+
# def type_to_ids_mapping
|
6
|
+
# default_hash = Hash.new { |hsh, key| hsh[key] = [] }
|
7
|
+
# result = values.each_with_object(default_hash) do |value, hash|
|
8
|
+
# hash[klass(value).polymorphic_name] << convert_to_id(value)
|
9
|
+
# end
|
10
|
+
# end
|
11
|
+
|
12
|
+
def type_to_ids_mapping
|
13
|
+
association = @associated_table.send(:association)
|
14
|
+
name = association.name
|
15
|
+
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
|
16
|
+
values.each_with_object(default_hash) do |value, hash|
|
17
|
+
klass = respond_to?(:klass, true) ? klass(value) : value.class
|
18
|
+
if association.active_record.respond_to?("#{name}_type_mapping")
|
19
|
+
mapping = association.active_record.send("#{name}_type_mapping")
|
20
|
+
key ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)
|
21
|
+
key ||= mapping.key(klass.sti_name)
|
22
|
+
key ||= mapping.key(klass.base_class.to_s)
|
23
|
+
key ||= mapping.key(klass.base_class.sti_name)
|
24
|
+
|
25
|
+
hash[key] << convert_to_id(value)
|
26
|
+
else
|
27
|
+
hash[klass.polymorphic_name] << convert_to_id(value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
ActiveRecord::PredicateBuilder::PolymorphicArrayValue.prepend(PolymorphicIntegerType::PolymorphicArrayValueExtension)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Associations
|
3
|
+
class BelongsToPolymorphicAssociation < BelongsToAssociation
|
4
|
+
private def replace_keys(record)
|
5
|
+
super
|
6
|
+
unless record.nil?
|
7
|
+
owner[reflection.foreign_type] = record.class.base_class
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -20,35 +20,11 @@ module PolymorphicIntegerType
|
|
20
20
|
_polymorphic_foreign_types << foreign_type
|
21
21
|
|
22
22
|
# Required way to dynamically define a class method on the model
|
23
|
-
|
23
|
+
define_singleton_method("#{foreign_type}_mapping") do
|
24
24
|
mapping
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
define_method foreign_type do
|
29
|
-
t = super()
|
30
|
-
self.class.send("#{foreign_type}_mapping")[t]
|
31
|
-
end
|
32
|
-
|
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.sti_name)
|
38
|
-
enum ||= mapping.key(klass.base_class.to_s)
|
39
|
-
enum ||= mapping.key(klass.base_class.sti_name)
|
40
|
-
end
|
41
|
-
enum ||= klass if klass != NilClass
|
42
|
-
super(enum)
|
43
|
-
end
|
44
|
-
|
45
|
-
define_method "#{name}=" do |record|
|
46
|
-
super(record)
|
47
|
-
send("#{foreign_type}=", record.class)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
include(foreign_type_extension)
|
27
|
+
ModuleGenerator.generate_and_include(self, foreign_type, name)
|
52
28
|
|
53
29
|
validate do
|
54
30
|
t = send(foreign_type)
|
@@ -69,14 +45,15 @@ module PolymorphicIntegerType
|
|
69
45
|
if options[:as] && (polymorphic_type_mapping || integer_type)
|
70
46
|
poly_type = options.delete(:as)
|
71
47
|
polymorphic_type_mapping ||= PolymorphicIntegerType::Mapping[poly_type]
|
72
|
-
if polymorphic_type_mapping
|
48
|
+
if polymorphic_type_mapping.nil?
|
73
49
|
raise "Polymorphic type mapping missing for #{poly_type.inspect}"
|
74
50
|
end
|
75
51
|
|
76
|
-
klass_mapping =
|
52
|
+
klass_mapping = polymorphic_type_mapping.key(polymorphic_name) if respond_to?(:polymorphic_name)
|
53
|
+
klass_mapping ||= polymorphic_type_mapping.key(sti_name)
|
77
54
|
|
78
|
-
if klass_mapping
|
79
|
-
raise "Class not found for #{
|
55
|
+
if klass_mapping.nil?
|
56
|
+
raise "Class not found for #{inspect} in polymorphic type mapping: #{polymorphic_type_mapping}"
|
80
57
|
end
|
81
58
|
|
82
59
|
options[:foreign_key] ||= "#{poly_type}_id"
|
@@ -95,7 +72,7 @@ module PolymorphicIntegerType
|
|
95
72
|
def retrieve_polymorphic_type_mapping(polymorphic_type:, class_name:)
|
96
73
|
return if polymorphic_type.nil?
|
97
74
|
|
98
|
-
belongs_to_class = class_name
|
75
|
+
belongs_to_class = compute_type(class_name)
|
99
76
|
method_name = "#{polymorphic_type}_type_mapping"
|
100
77
|
|
101
78
|
if belongs_to_class && belongs_to_class.respond_to?(method_name)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module PolymorphicIntegerType
|
2
|
+
class ModuleGenerator
|
3
|
+
def self.generate_and_include(klass,foreign_type, name)
|
4
|
+
foreign_type_extension = Module.new do
|
5
|
+
define_method foreign_type do
|
6
|
+
t = super()
|
7
|
+
self.class.send("#{foreign_type}_mapping")[t]
|
8
|
+
end
|
9
|
+
|
10
|
+
define_method "#{foreign_type}=" do |klass|
|
11
|
+
mapping = self.class.send("#{foreign_type}_mapping")
|
12
|
+
enum = mapping.key(klass.to_s)
|
13
|
+
if klass.kind_of?(Class) && klass <= ActiveRecord::Base
|
14
|
+
enum ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)
|
15
|
+
enum ||= mapping.key(klass.sti_name)
|
16
|
+
enum ||= mapping.key(klass.base_class.to_s)
|
17
|
+
enum ||= mapping.key(klass.base_class.sti_name)
|
18
|
+
end
|
19
|
+
enum ||= klass if klass != NilClass
|
20
|
+
super(enum)
|
21
|
+
end
|
22
|
+
|
23
|
+
define_method "#{name}=" do |record|
|
24
|
+
super(record)
|
25
|
+
send("#{foreign_type}=", record.class)
|
26
|
+
association(name).loaded!
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
klass.include(foreign_type_extension)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "activerecord"
|
21
|
+
spec.add_dependency "activerecord", "< 6.1"
|
22
22
|
spec.add_development_dependency "bundler"
|
23
23
|
spec.add_development_dependency "rake"
|
24
24
|
spec.add_development_dependency "rspec"
|
25
25
|
spec.add_development_dependency "sqlite3"
|
26
|
-
spec.add_development_dependency "byebug"
|
26
|
+
spec.add_development_dependency "pry-byebug"
|
27
27
|
end
|
@@ -14,6 +14,50 @@ describe PolymorphicIntegerType do
|
|
14
14
|
|
15
15
|
let(:link) { Link.create(source: source, target: target) }
|
16
16
|
|
17
|
+
|
18
|
+
context "when creating associations" do
|
19
|
+
it "sets the source_type" do
|
20
|
+
link = dog.source_links.new
|
21
|
+
expect(link.source_type).to eq("Animal")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "sets the target_type" do
|
25
|
+
link = kibble.target_links.new
|
26
|
+
expect(link.target_type).to eq("Food")
|
27
|
+
end
|
28
|
+
|
29
|
+
context "when models are namespaced" do
|
30
|
+
context "and mappings include namespaces" do
|
31
|
+
it "sets the source_type" do
|
32
|
+
allow(Link).to receive(:source_type_mapping).and_return({3 => "Namespaced::Plant"})
|
33
|
+
allow(Link).to receive(:source_type_mapping2).and_return({3 => "Namespaced::Plant"})
|
34
|
+
|
35
|
+
link = Namespaced::Plant.create(name: "Oak").source_links.new
|
36
|
+
expect(link.source_type).to eq("Namespaced::Plant")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "sets the target_type" do
|
40
|
+
allow(Link).to receive(:target_type_mapping).and_return({3 => "Namespaced::Activity"})
|
41
|
+
link = Namespaced::Activity.create(name: "swaying").target_links.new
|
42
|
+
expect(link.target_type).to eq("Namespaced::Activity")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "and mappings don't include namespaces" do
|
47
|
+
it "sets the source type" do
|
48
|
+
Link.source_type_mapping
|
49
|
+
link = Namespaced::Plant.create(name: "Oak").source_links.new
|
50
|
+
expect(link.source_type).to eq("Plant")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "sets the target type" do
|
54
|
+
link = Namespaced::Activity.create(name:"swaying").target_links.new
|
55
|
+
expect(link.target_type).to eq("Activity")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
17
61
|
context "when the source is nil" do
|
18
62
|
let(:source) { nil }
|
19
63
|
let(:target) { nil }
|
@@ -24,22 +68,64 @@ describe PolymorphicIntegerType do
|
|
24
68
|
end
|
25
69
|
end
|
26
70
|
|
27
|
-
context "when the source is a class that modifies the sti_name" do
|
71
|
+
context "when the source is a class that modifies the sti_name or polymorphic_name" do
|
72
|
+
context "and we leverage the polymorphic_name" do
|
73
|
+
before do
|
74
|
+
allow(PolymorphicIntegerType).to receive(:use_polymorphic_name).and_return(true)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "properly sets the source_type to the modified class name" do
|
78
|
+
link = Link.new(source: Namespaced::Animal.new)
|
79
|
+
expect(link.source_type).to eql "Animal"
|
80
|
+
end
|
81
|
+
|
82
|
+
it "can read dirty attributes from an associated object" do
|
83
|
+
animal = Namespaced::Animal.create!(name: "Oldie")
|
84
|
+
animal.name = "Newton"
|
85
|
+
link = Link.create!(source: animal)
|
86
|
+
|
87
|
+
expect(link.source.name).to eq("Newton")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
28
91
|
it "properly sets the source_type to the modified class name" do
|
29
92
|
link = Link.new(source: Namespaced::Animal.new)
|
30
93
|
expect(link.source_type).to eql "Animal"
|
31
94
|
end
|
95
|
+
|
96
|
+
it "can read dirty attributes from an associated object" do
|
97
|
+
animal = Namespaced::Animal.create!(name: "Oldie")
|
98
|
+
animal.name = "Newton"
|
99
|
+
link = Link.create!(source: animal)
|
100
|
+
|
101
|
+
expect(link.source.name).to eq("Newton")
|
102
|
+
end
|
32
103
|
end
|
33
104
|
|
34
105
|
context "when querying the associations" do
|
35
106
|
let(:source) { cat }
|
36
107
|
let(:target) { nil }
|
108
|
+
|
37
109
|
it "properly finds the object with a where" do
|
38
110
|
expect(Link.where(source: source, id: link.id).first).to eql link
|
39
111
|
end
|
112
|
+
|
113
|
+
it "properly finds the object when passing an array of sources" do
|
114
|
+
expect(Link.where(source: [source])).to eq [link]
|
115
|
+
end
|
116
|
+
|
40
117
|
it "properly finds the object with a find_by" do
|
41
118
|
expect(Link.find_by(source: source, id: link.id)).to eql link
|
42
119
|
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
|
43
129
|
end
|
44
130
|
|
45
131
|
shared_examples "proper source" do
|
@@ -70,7 +156,6 @@ describe PolymorphicIntegerType do
|
|
70
156
|
expect(source.source_links[0].source).to eql source
|
71
157
|
end
|
72
158
|
end
|
73
|
-
|
74
159
|
end
|
75
160
|
context "When a link is given polymorphic record" do
|
76
161
|
let(:link) { Link.create(source: source) }
|
@@ -79,13 +164,11 @@ describe PolymorphicIntegerType do
|
|
79
164
|
|
80
165
|
context "and when it already has a polymorphic record" do
|
81
166
|
let(:target) { kibble }
|
82
|
-
before { link.
|
167
|
+
before { link.update(target: target) }
|
83
168
|
|
84
169
|
include_examples "proper source"
|
85
170
|
include_examples "proper target"
|
86
|
-
|
87
171
|
end
|
88
|
-
|
89
172
|
end
|
90
173
|
|
91
174
|
context "When a link is given polymorphic id and type" do
|
@@ -95,15 +178,13 @@ describe PolymorphicIntegerType do
|
|
95
178
|
|
96
179
|
context "and when it already has a polymorphic id and type" do
|
97
180
|
let(:target) { kibble }
|
98
|
-
before { link.
|
181
|
+
before { link.update(target_id: target.id, target_type: target.class.to_s) }
|
99
182
|
include_examples "proper source"
|
100
183
|
include_examples "proper target"
|
101
|
-
|
102
184
|
end
|
103
|
-
|
104
185
|
end
|
105
186
|
|
106
|
-
context "When using a relation to the links with
|
187
|
+
context "When using a relation to the links with eager loading" do
|
107
188
|
let!(:links){
|
108
189
|
[Link.create(source: source, target: kibble),
|
109
190
|
Link.create(source: source, target: water)]
|
@@ -113,12 +194,10 @@ describe PolymorphicIntegerType do
|
|
113
194
|
it "should be able to return the links and the targets" do
|
114
195
|
expect(cat.source_links).to match_array links
|
115
196
|
expect(cat.source_links.includes(:target).collect(&:target)).to match_array [water, kibble]
|
116
|
-
|
117
197
|
end
|
118
|
-
|
119
198
|
end
|
120
199
|
|
121
|
-
context "When using a through relation to the links with
|
200
|
+
context "When using a through relation to the links with eager loading" do
|
122
201
|
let!(:links){
|
123
202
|
[Link.create(source: source, target: kibble),
|
124
203
|
Link.create(source: source, target: water)]
|
@@ -128,12 +207,10 @@ describe PolymorphicIntegerType do
|
|
128
207
|
it "should be able to return the links and the targets" do
|
129
208
|
expect(owner.pet_source_links).to match_array links
|
130
209
|
expect(owner.pet_source_links.includes(:target).collect(&:target)).to match_array [water, kibble]
|
131
|
-
|
132
210
|
end
|
133
|
-
|
134
211
|
end
|
135
212
|
|
136
|
-
context "When
|
213
|
+
context "When eager loading the polymorphic association" do
|
137
214
|
let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) }
|
138
215
|
let(:source) { cat }
|
139
216
|
|
@@ -145,15 +222,12 @@ describe PolymorphicIntegerType do
|
|
145
222
|
expect(links.first.source).to eql cat
|
146
223
|
expect(links.last.source).to eql dog
|
147
224
|
end
|
148
|
-
|
149
225
|
end
|
150
226
|
|
151
227
|
it "should be able to preload the association" do
|
152
228
|
l = Link.includes(:source).where(id: link.id).first
|
153
229
|
expect(l.source).to eql cat
|
154
230
|
end
|
155
|
-
|
156
|
-
|
157
231
|
end
|
158
232
|
|
159
233
|
context "when the association is an STI table" do
|
@@ -205,7 +279,7 @@ describe PolymorphicIntegerType do
|
|
205
279
|
include_examples "proper target"
|
206
280
|
|
207
281
|
it "creates foreign_type mapping method" do
|
208
|
-
expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal"})
|
282
|
+
expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal", 3 => "Plant"})
|
209
283
|
expect(InlineLink.source_type_mapping).to eq({10 => "Person", 11 => "InlineAnimal"})
|
210
284
|
end
|
211
285
|
|
@@ -246,7 +320,7 @@ describe PolymorphicIntegerType do
|
|
246
320
|
|
247
321
|
self.table_name = "drinks"
|
248
322
|
|
249
|
-
has_many :
|
323
|
+
has_many :inline_link2s, as: :target
|
250
324
|
end
|
251
325
|
|
252
326
|
let!(:animal) { InlineAnimal2.create!(name: "Lucy") }
|
data/spec/spec_helper.rb
CHANGED
@@ -5,17 +5,26 @@ require 'support/configuration'
|
|
5
5
|
require 'support/link'
|
6
6
|
require 'support/animal'
|
7
7
|
require 'support/namespaced_animal'
|
8
|
+
require 'support/namespaced_plant'
|
8
9
|
require 'support/dog'
|
9
10
|
require 'support/person'
|
10
11
|
require 'support/food'
|
11
12
|
require 'support/drink'
|
13
|
+
require 'support/namespaced_activity'
|
14
|
+
require 'byebug'
|
15
|
+
require 'pry'
|
12
16
|
|
13
17
|
RSpec.configure do |config|
|
14
18
|
config.before(:suite) do
|
15
19
|
database_config = YAML.load(File.open("#{File.dirname(__FILE__)}/support/database.yml"))
|
16
20
|
ActiveRecord::Base.establish_connection(database_config)
|
17
|
-
|
18
|
-
|
21
|
+
|
22
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) == Gem::Version.new("5.2.0")
|
23
|
+
ActiveRecord::MigrationContext.new("#{File.dirname(__FILE__)}/support/migrations").migrate
|
24
|
+
end
|
25
|
+
|
26
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("6.0")
|
27
|
+
ActiveRecord::MigrationContext.new("#{File.dirname(__FILE__)}/support/migrations", ActiveRecord::SchemaMigration).migrate
|
19
28
|
end
|
20
29
|
end
|
21
30
|
|
@@ -1,4 +1,4 @@
|
|
1
1
|
PolymorphicIntegerType::Mapping.configuration do |config|
|
2
|
-
config.add :source, {1 => "Person", 2 => "Animal"}
|
3
|
-
config.add :target, {1 => "Food", 2 => "Drink"}
|
2
|
+
config.add :source, {1 => "Person", 2 => "Animal", 3 => "Plant"}
|
3
|
+
config.add :target, {1 => "Food", 2 => "Drink", 3 => "Activity"}
|
4
4
|
end
|
data/spec/support/link.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
module Namespaced
|
2
|
+
class Activity < ActiveRecord::Base
|
3
|
+
include PolymorphicIntegerType::Extensions
|
4
|
+
|
5
|
+
self.store_full_sti_class = false
|
6
|
+
self.table_name = "activities"
|
7
|
+
|
8
|
+
has_many :target_links, as: :target, inverse_of: :target, integer_type: true, class_name: "Link"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Namespaced
|
2
|
+
class Plant < ActiveRecord::Base
|
3
|
+
include PolymorphicIntegerType::Extensions
|
4
|
+
|
5
|
+
self.store_full_sti_class = false
|
6
|
+
self.table_name = "plants"
|
7
|
+
|
8
|
+
belongs_to :owner, class_name: "Person"
|
9
|
+
has_many :source_links, as: :source, inverse_of: :source, integer_type: true, class_name: "Link"
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorphic_integer_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyle d'Oliveira
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "<"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "<"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name: byebug
|
84
|
+
name: pry-byebug
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -108,11 +108,21 @@ files:
|
|
108
108
|
- README.md
|
109
109
|
- Rakefile
|
110
110
|
- bin/setup
|
111
|
+
- gemfiles/Gemfile.rails-5.0-stable
|
112
|
+
- gemfiles/Gemfile.rails-5.0-stable.lock
|
113
|
+
- gemfiles/Gemfile.rails-5.1-stable
|
114
|
+
- gemfiles/Gemfile.rails-5.1-stable.lock
|
115
|
+
- gemfiles/Gemfile.rails-5.2-stable
|
116
|
+
- gemfiles/Gemfile.rails-5.2-stable.lock
|
117
|
+
- gemfiles/Gemfile.rails-6-0-stable
|
118
|
+
- gemfiles/Gemfile.rails-6-0-stable.lock
|
111
119
|
- lib/polymorphic_integer_type.rb
|
120
|
+
- lib/polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension.rb
|
121
|
+
- lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb
|
122
|
+
- lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb
|
112
123
|
- lib/polymorphic_integer_type/extensions.rb
|
113
124
|
- lib/polymorphic_integer_type/mapping.rb
|
114
|
-
- lib/polymorphic_integer_type/
|
115
|
-
- lib/polymorphic_integer_type/predicate_builder_extension.rb
|
125
|
+
- lib/polymorphic_integer_type/module_generator.rb
|
116
126
|
- lib/polymorphic_integer_type/version.rb
|
117
127
|
- polymorphic_integer_type.gemspec
|
118
128
|
- spec/polymorphic_integer_type_spec.rb
|
@@ -129,13 +139,17 @@ 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/migrations/6_create_plant_table.rb
|
143
|
+
- spec/support/migrations/7_create_activity_table.rb
|
144
|
+
- spec/support/namespaced_activity.rb
|
132
145
|
- spec/support/namespaced_animal.rb
|
146
|
+
- spec/support/namespaced_plant.rb
|
133
147
|
- spec/support/person.rb
|
134
148
|
homepage: ''
|
135
149
|
licenses:
|
136
150
|
- MIT
|
137
151
|
metadata: {}
|
138
|
-
post_install_message:
|
152
|
+
post_install_message:
|
139
153
|
rdoc_options: []
|
140
154
|
require_paths:
|
141
155
|
- lib
|
@@ -150,9 +164,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
164
|
- !ruby/object:Gem::Version
|
151
165
|
version: '0'
|
152
166
|
requirements: []
|
153
|
-
|
154
|
-
|
155
|
-
signing_key:
|
167
|
+
rubygems_version: 3.1.6
|
168
|
+
signing_key:
|
156
169
|
specification_version: 4
|
157
170
|
summary: Use integers rather than strings for the _type field
|
158
171
|
test_files:
|
@@ -170,5 +183,9 @@ test_files:
|
|
170
183
|
- spec/support/migrations/3_create_person_table.rb
|
171
184
|
- spec/support/migrations/4_create_food_table.rb
|
172
185
|
- spec/support/migrations/5_create_drink_table.rb
|
186
|
+
- spec/support/migrations/6_create_plant_table.rb
|
187
|
+
- spec/support/migrations/7_create_activity_table.rb
|
188
|
+
- spec/support/namespaced_activity.rb
|
173
189
|
- spec/support/namespaced_animal.rb
|
190
|
+
- spec/support/namespaced_plant.rb
|
174
191
|
- spec/support/person.rb
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module PolymorphicIntegerType
|
2
|
-
module PolymorphicArrayValueExtension
|
3
|
-
def type_to_ids_mapping
|
4
|
-
super.tap do |result|
|
5
|
-
association = @associated_table.send(:association)
|
6
|
-
klass = association.active_record
|
7
|
-
name = association.name
|
8
|
-
|
9
|
-
if klass.respond_to?("#{name}_type_mapping")
|
10
|
-
result.transform_keys! do |key|
|
11
|
-
klass.send("#{name}_type_mapping").key(key)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
result
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
ActiveRecord::PredicateBuilder::PolymorphicArrayValue.prepend(PolymorphicIntegerType::PolymorphicArrayValueExtension)
|
@@ -1,65 +0,0 @@
|
|
1
|
-
module PolymorphicIntegerType
|
2
|
-
module PredicateBuilderExtension
|
3
|
-
|
4
|
-
# Original Code:
|
5
|
-
# def self.expand(klass, table, column, value)
|
6
|
-
# queries = []
|
7
|
-
|
8
|
-
# # Find the foreign key when using queries such as:
|
9
|
-
# # Post.where(author: author)
|
10
|
-
# #
|
11
|
-
# # For polymorphic relationships, find the foreign key and type:
|
12
|
-
# # PriceEstimate.where(estimate_of: treasure)
|
13
|
-
# if klass && reflection = klass._reflect_on_association(column)
|
14
|
-
# base_class = polymorphic_base_class_from_value(value)
|
15
|
-
|
16
|
-
# if reflection.polymorphic? && base_class
|
17
|
-
# queries << build(table[reflection.foreign_type], base_class)
|
18
|
-
# end
|
19
|
-
|
20
|
-
# column = reflection.foreign_key
|
21
|
-
|
22
|
-
# if base_class
|
23
|
-
# primary_key = reflection.association_primary_key(base_class)
|
24
|
-
# value = convert_value_to_association_ids(value, primary_key)
|
25
|
-
# end
|
26
|
-
# end
|
27
|
-
|
28
|
-
# queries << build(table[column], value)
|
29
|
-
# queries
|
30
|
-
# end
|
31
|
-
|
32
|
-
def expand(klass, table, column, value)
|
33
|
-
queries = []
|
34
|
-
|
35
|
-
# Find the foreign key when using queries such as:
|
36
|
-
# Post.where(author: author)
|
37
|
-
#
|
38
|
-
# For polymorphic relationships, find the foreign key and type:
|
39
|
-
# PriceEstimate.where(estimate_of: treasure)
|
40
|
-
if klass && reflection = klass._reflect_on_association(column)
|
41
|
-
base_class = polymorphic_base_class_from_value(value)
|
42
|
-
|
43
|
-
if reflection.polymorphic? && base_class
|
44
|
-
if klass.respond_to?("#{column}_type_mapping")
|
45
|
-
queries << build(table[reflection.foreign_type], klass.send("#{column}_type_mapping").key(base_class.to_s))
|
46
|
-
else
|
47
|
-
queries << build(table[reflection.foreign_type], base_class)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
column = reflection.foreign_key
|
52
|
-
|
53
|
-
if base_class
|
54
|
-
primary_key = reflection.association_primary_key(base_class)
|
55
|
-
value = convert_value_to_association_ids(value, primary_key)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
queries << build(table[column], value)
|
60
|
-
queries
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
ActiveRecord::PredicateBuilder.singleton_class.prepend(PolymorphicIntegerType::PredicateBuilderExtension)
|