store_base_sti_class 1.1.0 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Appraisals +9 -55
- data/LICENSE.txt +1 -1
- data/README.md +37 -10
- data/gemfiles/rails_5.0.7.gemfile +8 -0
- data/gemfiles/rails_5.1.7.gemfile +8 -0
- data/gemfiles/rails_5.2.3.gemfile +8 -0
- data/gemfiles/rails_5.2.4.gemfile +8 -0
- data/gemfiles/rails_5.2.5.gemfile +8 -0
- data/gemfiles/rails_6.0.3.gemfile +8 -0
- data/gemfiles/rails_6.1.0.gemfile +8 -0
- data/lib/store_base_sti_class.rb +7 -7
- data/lib/store_base_sti_class/version.rb +1 -1
- data/lib/store_base_sti_class_for_5_0.rb +16 -0
- data/lib/store_base_sti_class_for_5_1.rb +16 -0
- data/lib/store_base_sti_class_for_5_2.rb +176 -0
- data/lib/store_base_sti_class_for_6_0.rb +153 -0
- data/lib/store_base_sti_class_for_6_1.rb +156 -0
- data/store_base_sti_class.gemspec +1 -2
- metadata +17 -176
- data/Gemfile.lock +0 -47
- data/debug.log +0 -53835
- data/gemfiles/rails_3.0.10.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.11.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.12.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.13.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.14.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.15.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.16.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.17.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.18.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.19.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.20.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.5.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.6.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.7.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.8.gemfile.lock +0 -45
- data/gemfiles/rails_3.0.9.gemfile.lock +0 -45
- data/gemfiles/rails_3.1.0.gemfile.lock +0 -49
- data/gemfiles/rails_3.1.10.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.11.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.12.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.2.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.3.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.4.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.5.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.6.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.7.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.8.gemfile.lock +0 -47
- data/gemfiles/rails_3.1.9.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.0.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.1.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.10.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.11.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.12.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.13.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.14.gemfile.lock +0 -44
- data/gemfiles/rails_3.2.15.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.16.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.17.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.18.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.19.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.2.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.20.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.21.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.22.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.3.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.4.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.5.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.6.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.7.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.8.gemfile.lock +0 -47
- data/gemfiles/rails_3.2.9.gemfile.lock +0 -47
- data/gemfiles/rails_4.0.0.gemfile +0 -7
- data/gemfiles/rails_4.0.0.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.1.gemfile +0 -7
- data/gemfiles/rails_4.0.1.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.10.gemfile +0 -7
- data/gemfiles/rails_4.0.10.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.11.1.gemfile +0 -7
- data/gemfiles/rails_4.0.11.1.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.11.gemfile +0 -7
- data/gemfiles/rails_4.0.11.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.12.gemfile +0 -7
- data/gemfiles/rails_4.0.12.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.13.gemfile +0 -7
- data/gemfiles/rails_4.0.13.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.2.gemfile +0 -7
- data/gemfiles/rails_4.0.2.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.3.gemfile +0 -7
- data/gemfiles/rails_4.0.3.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.4.gemfile +0 -7
- data/gemfiles/rails_4.0.4.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.5.gemfile +0 -7
- data/gemfiles/rails_4.0.5.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.6.gemfile +0 -7
- data/gemfiles/rails_4.0.6.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.7.gemfile +0 -7
- data/gemfiles/rails_4.0.7.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.8.gemfile +0 -7
- data/gemfiles/rails_4.0.8.gemfile.lock +0 -49
- data/gemfiles/rails_4.0.9.gemfile +0 -7
- data/gemfiles/rails_4.0.9.gemfile.lock +0 -49
- data/gemfiles/rails_4.1.0.gemfile +0 -7
- data/gemfiles/rails_4.1.0.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.1.gemfile +0 -7
- data/gemfiles/rails_4.1.1.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.10.gemfile +0 -7
- data/gemfiles/rails_4.1.10.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.11.gemfile +0 -7
- data/gemfiles/rails_4.1.11.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.12.gemfile +0 -7
- data/gemfiles/rails_4.1.12.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.13.gemfile +0 -7
- data/gemfiles/rails_4.1.13.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.14.1.gemfile +0 -7
- data/gemfiles/rails_4.1.14.1.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.14.2.gemfile +0 -7
- data/gemfiles/rails_4.1.14.2.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.14.gemfile +0 -7
- data/gemfiles/rails_4.1.14.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.15.gemfile +0 -7
- data/gemfiles/rails_4.1.15.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.2.gemfile +0 -7
- data/gemfiles/rails_4.1.2.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.3.gemfile +0 -7
- data/gemfiles/rails_4.1.3.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.4.gemfile +0 -7
- data/gemfiles/rails_4.1.4.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.5.gemfile +0 -7
- data/gemfiles/rails_4.1.5.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.6.gemfile +0 -7
- data/gemfiles/rails_4.1.6.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.7.1.gemfile +0 -7
- data/gemfiles/rails_4.1.7.1.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.7.gemfile +0 -7
- data/gemfiles/rails_4.1.7.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.8.gemfile +0 -7
- data/gemfiles/rails_4.1.8.gemfile.lock +0 -48
- data/gemfiles/rails_4.1.9.gemfile +0 -7
- data/gemfiles/rails_4.1.9.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.0.gemfile +0 -7
- data/gemfiles/rails_4.2.0.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.1.gemfile +0 -7
- data/gemfiles/rails_4.2.1.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.2.gemfile +0 -7
- data/gemfiles/rails_4.2.2.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.3.gemfile +0 -7
- data/gemfiles/rails_4.2.3.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.4.gemfile +0 -7
- data/gemfiles/rails_4.2.4.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.5.1.gemfile +0 -7
- data/gemfiles/rails_4.2.5.1.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.5.2.gemfile +0 -7
- data/gemfiles/rails_4.2.5.2.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.5.gemfile +0 -7
- data/gemfiles/rails_4.2.5.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.6.gemfile +0 -7
- data/gemfiles/rails_4.2.6.gemfile.lock +0 -48
- data/gemfiles/rails_4.2.7.1.gemfile +0 -7
- data/gemfiles/rails_4.2.7.gemfile +0 -7
- data/gemfiles/rails_4.2.8.gemfile +0 -7
- data/gemfiles/rails_5.0.0.1.gemfile +0 -7
- data/gemfiles/rails_5.0.0.gemfile +0 -7
- data/gemfiles/rails_5.0.1.gemfile +0 -7
- data/gemfiles/rails_5.0.1.gemfile.lock +0 -48
- data/gemfiles/rails_5.0.2.gemfile +0 -7
- data/gemfiles/rails_5.0.3.gemfile +0 -7
- data/gemfiles/rails_5.1.0.gemfile +0 -7
- data/gemfiles/rails_5.1.1.gemfile +0 -7
- data/lib/store_base_sti_class_for_4_0.rb +0 -337
- data/lib/store_base_sti_class_for_4_1.rb +0 -360
- data/lib/store_base_sti_class_for_4_2.rb +0 -360
- data/storebasestiname_unittest.sql +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5e65e14ac35f409bec5e78c411a0b3fac80ca189d8e77f693fc2ecc9f4cf609d
|
4
|
+
data.tar.gz: c0aabbee8bba07b1818f5f2fca9f94a916f56e29bcff73d9f1919fa53d269bcc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6078f16de9958cead9799a9da35830c2c031c17bbead961b801f6194905e65e43ab6f002ab34fd34ef6431e0b8f9ab1fc52b0c502372a3957a768480459df882
|
7
|
+
data.tar.gz: 22e514b7f2ce662d2cfa8f8fe6b2bc62f4491defe8b182c76121922f01ca7929146f546cfb7eab0d8d42ba8417c4343ae54d616cb949a44bba48c98fa5b0469f
|
data/Appraisals
CHANGED
@@ -1,61 +1,15 @@
|
|
1
|
-
RAILS_VERSIONS = %w
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
4.0.7
|
10
|
-
4.0.8
|
11
|
-
4.0.9
|
12
|
-
4.0.10
|
13
|
-
4.0.11
|
14
|
-
4.0.11.1
|
15
|
-
4.0.12
|
16
|
-
4.0.13
|
17
|
-
4.1.0
|
18
|
-
4.1.1
|
19
|
-
4.1.2
|
20
|
-
4.1.3
|
21
|
-
4.1.4
|
22
|
-
4.1.5
|
23
|
-
4.1.6
|
24
|
-
4.1.7
|
25
|
-
4.1.7.1
|
26
|
-
4.1.8
|
27
|
-
4.1.9
|
28
|
-
4.1.10
|
29
|
-
4.1.11
|
30
|
-
4.1.12
|
31
|
-
4.1.13
|
32
|
-
4.1.14
|
33
|
-
4.1.14.1
|
34
|
-
4.1.14.2
|
35
|
-
4.1.15
|
36
|
-
4.2.0
|
37
|
-
4.2.1
|
38
|
-
4.2.2
|
39
|
-
4.2.3
|
40
|
-
4.2.4
|
41
|
-
4.2.5
|
42
|
-
4.2.5.1
|
43
|
-
4.2.5.2
|
44
|
-
4.2.6
|
45
|
-
4.2.7
|
46
|
-
4.2.7.1
|
47
|
-
4.2.8
|
48
|
-
5.0.0
|
49
|
-
5.0.0.1
|
50
|
-
5.0.1
|
51
|
-
5.0.2
|
52
|
-
5.0.3
|
53
|
-
5.1.0
|
54
|
-
5.1.1
|
55
|
-
)
|
1
|
+
RAILS_VERSIONS = %w[
|
2
|
+
5.0.7
|
3
|
+
5.1.7
|
4
|
+
5.2.3
|
5
|
+
5.2.4
|
6
|
+
6.0.3
|
7
|
+
6.1.0
|
8
|
+
].freeze
|
56
9
|
|
57
10
|
RAILS_VERSIONS.each do |version|
|
58
11
|
appraise "rails_#{version}" do
|
59
12
|
gem 'activerecord', version
|
13
|
+
gem 'sqlite3', ['6.0.3', '6.1.0'].include?(version) ? '~> 1.4.0' : '~> 1.3.0'
|
60
14
|
end
|
61
15
|
end
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
[![
|
1
|
+
[![CI](https://github.com/appfolio/store_base_sti_class/workflows/CI/badge.svg)](https://github.com/appfolio/store_base_sti_class/actions)
|
2
|
+
|
2
3
|
## Description
|
3
4
|
|
4
5
|
Given the following class definitions:
|
@@ -35,12 +36,18 @@ will output:
|
|
35
36
|
|
36
37
|
Notice that addressable_type column is Person even though the actual class is Vendor.
|
37
38
|
|
38
|
-
Normally, this isn't a problem, however, it can have negative performance
|
39
|
-
|
39
|
+
Normally, this isn't a problem, however, it can have negative performance
|
40
|
+
characteristics in certain circumstances. The most obvious one is that a join
|
41
|
+
with persons or an extra query is required to find out the actual type of
|
42
|
+
addressable.
|
40
43
|
|
41
|
-
This gem adds the ActiveRecord::Base.store_base_sti_class configuration
|
44
|
+
This gem adds the ActiveRecord::Base.store_base_sti_class configuration
|
45
|
+
option. It defaults to true for backwards compatibility. Setting it to false
|
46
|
+
will alter ActiveRecord's behavior to store the actual class in polymorphic
|
47
|
+
_type columns when STI is used.
|
42
48
|
|
43
49
|
In the example above, if the ActiveRecord::Base.store_base_sti_class is false, the output will be,
|
50
|
+
|
44
51
|
```
|
45
52
|
#<Vendor id: 1, type: "Vendor" ...>
|
46
53
|
#<Address id: 1, addressable_id: 1, addressable_type: 'Vendor' ...>
|
@@ -54,21 +61,41 @@ Add the following line to your Gemfile,
|
|
54
61
|
gem 'store_base_sti_class'
|
55
62
|
```
|
56
63
|
|
57
|
-
then bundle install. Once you have the gem installed, add the following to one
|
64
|
+
then bundle install. Once you have the gem installed, add the following to one
|
65
|
+
of the initializers (or make a new one) in config/initializers,
|
58
66
|
|
59
|
-
|
67
|
+
ActiveRecord::Base.store_base_sti_class = false
|
60
68
|
|
61
|
-
When changing this behavior, you will have write a migration to update all of
|
69
|
+
When changing this behavior, you will have write a migration to update all of
|
70
|
+
your existing _type columns accordingly. You may also need to change your
|
71
|
+
application if it explicitly relies on the _type columns.
|
62
72
|
|
63
73
|
## Notes
|
64
74
|
|
65
75
|
This gem incorporates work from:
|
76
|
+
|
66
77
|
- https://github.com/codepodu/store_base_sti_class_for_4_0
|
67
78
|
|
68
|
-
It currently works with ActiveRecord 4.
|
79
|
+
It currently works with ActiveRecord 4.2.x through 6.0.x. If you need support
|
80
|
+
for ActiveRecord 3.x, use a pre-1.0 version of the gem, or ActiveRecord < 4.2
|
81
|
+
use a pre-2.0 version of the gem.
|
82
|
+
|
83
|
+
## Conflicts
|
84
|
+
|
85
|
+
This gem produces known conflicts with these other gems:
|
86
|
+
|
87
|
+
### friendly_id
|
88
|
+
|
89
|
+
When using [friendly_id](https://github.com/norman/friendly_id) >= 5.2.5 with the [History module](https://norman.github.io/friendly_id/FriendlyId/History.html) enabled, duplicate slugs will be generated for STI subclasses with the same sluggable identifier (ex: name). This will either cause saves to fail if you have the proper indexes in place, or will cause slug lookups to be non-deterministic, either of which is undesirable.
|
90
|
+
|
91
|
+
## History
|
92
|
+
|
93
|
+
* https://github.com/rails/rails/issues/724
|
94
|
+
* https://github.com/rails/rails/issues/5441#issuecomment-4563865
|
95
|
+
* https://github.com/rails/rails/issues/4729#issuecomment-5729297
|
96
|
+
* https://github.com/rails/rails/issues/5441#issuecomment-264871920
|
69
97
|
|
70
98
|
## Copyright
|
71
99
|
|
72
|
-
Copyright (c) 2011-
|
100
|
+
Copyright (c) 2011-2019 AppFolio, inc. See LICENSE.txt for
|
73
101
|
further details.
|
74
|
-
|
data/lib/store_base_sti_class.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
|
3
|
-
if ActiveRecord::VERSION::STRING =~ /^
|
4
|
-
require 'store_base_sti_class_for_4_0'
|
5
|
-
elsif ActiveRecord::VERSION::STRING =~ /^4\.1/
|
6
|
-
require 'store_base_sti_class_for_4_1'
|
7
|
-
elsif ActiveRecord::VERSION::STRING =~ /^4\.2/
|
8
|
-
require 'store_base_sti_class_for_4_2'
|
9
|
-
elsif ActiveRecord::VERSION::STRING =~ /^5\.0/
|
3
|
+
if ActiveRecord::VERSION::STRING =~ /^5\.0/
|
10
4
|
require 'store_base_sti_class_for_5_0'
|
11
5
|
elsif ActiveRecord::VERSION::STRING =~ /^5\.1/
|
12
6
|
require 'store_base_sti_class_for_5_1'
|
7
|
+
elsif ActiveRecord::VERSION::STRING =~ /^5\.2/
|
8
|
+
require 'store_base_sti_class_for_5_2'
|
9
|
+
elsif ActiveRecord::VERSION::STRING =~ /^6\.0/
|
10
|
+
require 'store_base_sti_class_for_6_0'
|
11
|
+
elsif ActiveRecord::VERSION::STRING =~ /^6\.1/
|
12
|
+
require 'store_base_sti_class_for_6_1'
|
13
13
|
end
|
14
14
|
|
15
15
|
module StoreBaseSTIClass
|
@@ -8,6 +8,22 @@ if ActiveRecord::VERSION::STRING =~ /^5\.0/
|
|
8
8
|
self.store_base_sti_class = true
|
9
9
|
end
|
10
10
|
|
11
|
+
class PredicateBuilder
|
12
|
+
class AssociationQueryHandler
|
13
|
+
def call(attribute, value)
|
14
|
+
queries = {}
|
15
|
+
|
16
|
+
table = value.associated_table
|
17
|
+
if value.base_class
|
18
|
+
queries[table.association_foreign_type.to_s] = ActiveRecord::Base.store_base_sti_class ? value.base_class.name : value.value.class.name
|
19
|
+
end
|
20
|
+
|
21
|
+
queries[table.association_foreign_key.to_s] = value.ids
|
22
|
+
predicate_builder.build_from_hash(queries)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
11
27
|
module Associations
|
12
28
|
class Association
|
13
29
|
private
|
@@ -8,6 +8,22 @@ if ActiveRecord::VERSION::STRING =~ /^5\.1/
|
|
8
8
|
self.store_base_sti_class = true
|
9
9
|
end
|
10
10
|
|
11
|
+
class PredicateBuilder
|
12
|
+
class AssociationQueryHandler
|
13
|
+
def call(attribute, value)
|
14
|
+
queries = {}
|
15
|
+
|
16
|
+
table = value.associated_table
|
17
|
+
if value.base_class
|
18
|
+
queries[table.association_foreign_type.to_s] = ActiveRecord::Base.store_base_sti_class ? value.base_class.name : value.value.class.name
|
19
|
+
end
|
20
|
+
|
21
|
+
queries[table.association_foreign_key.to_s] = value.ids
|
22
|
+
predicate_builder.build_from_hash(queries)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
11
27
|
module Associations
|
12
28
|
class Association
|
13
29
|
private
|
@@ -0,0 +1,176 @@
|
|
1
|
+
require 'active_record/associations/join_dependency/join_part'
|
2
|
+
|
3
|
+
if ActiveRecord::VERSION::STRING =~ /^5\.2/
|
4
|
+
module ActiveRecord
|
5
|
+
|
6
|
+
class Base
|
7
|
+
class_attribute :store_base_sti_class
|
8
|
+
self.store_base_sti_class = true
|
9
|
+
end
|
10
|
+
|
11
|
+
module Inheritance
|
12
|
+
module ClassMethods
|
13
|
+
def polymorphic_name
|
14
|
+
ActiveRecord::Base.store_base_sti_class ? base_class.name : name
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Associations
|
20
|
+
class Preloader
|
21
|
+
class ThroughAssociation < Association
|
22
|
+
private
|
23
|
+
|
24
|
+
def through_scope
|
25
|
+
scope = through_reflection.klass.unscoped
|
26
|
+
options = reflection.options
|
27
|
+
|
28
|
+
if options[:source_type]
|
29
|
+
|
30
|
+
# BEGIN PATCH
|
31
|
+
# original:
|
32
|
+
# scope.where! reflection.foreign_type => options[:source_type]
|
33
|
+
|
34
|
+
adjusted_foreign_type =
|
35
|
+
if ActiveRecord::Base.store_base_sti_class
|
36
|
+
options[:source_type]
|
37
|
+
else
|
38
|
+
([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
|
39
|
+
end
|
40
|
+
|
41
|
+
scope.where! reflection.foreign_type => adjusted_foreign_type
|
42
|
+
# END PATCH
|
43
|
+
|
44
|
+
elsif !reflection_scope.where_clause.empty?
|
45
|
+
scope.where_clause = reflection_scope.where_clause
|
46
|
+
values = reflection_scope.values
|
47
|
+
|
48
|
+
if includes = values[:includes]
|
49
|
+
scope.includes!(source_reflection.name => includes)
|
50
|
+
else
|
51
|
+
scope.includes!(source_reflection.name)
|
52
|
+
end
|
53
|
+
|
54
|
+
if values[:references] && !values[:references].empty?
|
55
|
+
scope.references!(values[:references])
|
56
|
+
else
|
57
|
+
scope.references!(source_reflection.table_name)
|
58
|
+
end
|
59
|
+
|
60
|
+
if joins = values[:joins]
|
61
|
+
scope.joins!(source_reflection.name => joins)
|
62
|
+
end
|
63
|
+
|
64
|
+
if left_outer_joins = values[:left_outer_joins]
|
65
|
+
scope.left_outer_joins!(source_reflection.name => left_outer_joins)
|
66
|
+
end
|
67
|
+
|
68
|
+
if scope.eager_loading? && order_values = values[:order]
|
69
|
+
scope = scope.order(order_values)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
scope unless scope.empty_scope?
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class AssociationScope
|
79
|
+
private
|
80
|
+
|
81
|
+
def next_chain_scope(scope, reflection, next_reflection)
|
82
|
+
join_keys = reflection.join_keys
|
83
|
+
key = join_keys.key
|
84
|
+
foreign_key = join_keys.foreign_key
|
85
|
+
|
86
|
+
table = reflection.aliased_table
|
87
|
+
foreign_table = next_reflection.aliased_table
|
88
|
+
constraint = table[key].eq(foreign_table[foreign_key])
|
89
|
+
|
90
|
+
if reflection.type
|
91
|
+
# BEGIN PATCH
|
92
|
+
# original:
|
93
|
+
# value = transform_value(next_reflection.klass.polymorphic_name)
|
94
|
+
# scope = apply_scope(scope, table, reflection.type, value)
|
95
|
+
if ActiveRecord::Base.store_base_sti_class
|
96
|
+
value = transform_value(next_reflection.klass.polymorphic_name)
|
97
|
+
else
|
98
|
+
klass = next_reflection.klass
|
99
|
+
value = ([klass] + klass.descendants).map(&:name)
|
100
|
+
end
|
101
|
+
scope = apply_scope(scope, table, reflection.type, value)
|
102
|
+
# END PATCH
|
103
|
+
end
|
104
|
+
|
105
|
+
scope.joins!(join(foreign_table, constraint))
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
class HasManyThroughAssociation
|
111
|
+
private
|
112
|
+
|
113
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.2.4')
|
114
|
+
def build_through_record(record)
|
115
|
+
@through_records[record.object_id] ||= begin
|
116
|
+
ensure_mutable
|
117
|
+
|
118
|
+
attributes = through_scope_attributes
|
119
|
+
attributes[source_reflection.name] = record
|
120
|
+
|
121
|
+
# START PATCH
|
122
|
+
if ActiveRecord::Base.store_base_sti_class
|
123
|
+
if options[:source_type]
|
124
|
+
attributes[source_reflection.foreign_type] = options[:source_type]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
# END PATCH
|
128
|
+
|
129
|
+
through_association.build(attributes)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
else
|
133
|
+
def build_through_record(record)
|
134
|
+
@through_records[record.object_id] ||= begin
|
135
|
+
ensure_mutable
|
136
|
+
|
137
|
+
through_record = through_association.build(*options_for_through_record)
|
138
|
+
through_record.send("#{source_reflection.name}=", record)
|
139
|
+
|
140
|
+
# START PATCH
|
141
|
+
if ActiveRecord::Base.store_base_sti_class
|
142
|
+
if options[:source_type]
|
143
|
+
through_record.send("#{source_reflection.foreign_type}=", options[:source_type])
|
144
|
+
end
|
145
|
+
end
|
146
|
+
# END PATCH
|
147
|
+
|
148
|
+
through_record
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
module Reflection
|
156
|
+
class PolymorphicReflection
|
157
|
+
def source_type_scope
|
158
|
+
type = @previous_reflection.foreign_type
|
159
|
+
source_type = @previous_reflection.options[:source_type]
|
160
|
+
|
161
|
+
# START PATCH
|
162
|
+
adjusted_source_type =
|
163
|
+
if ActiveRecord::Base.store_base_sti_class
|
164
|
+
source_type
|
165
|
+
else
|
166
|
+
([source_type.constantize] + source_type.constantize.descendants).map(&:to_s)
|
167
|
+
end
|
168
|
+
# END PATCH
|
169
|
+
|
170
|
+
lambda { |object| where(type => adjusted_source_type) }
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|