store_base_sti_class 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +5 -5
  2. data/Appraisals +8 -65
  3. data/Gemfile.lock +16 -18
  4. data/LICENSE.txt +1 -1
  5. data/README.md +20 -9
  6. data/gemfiles/rails_4.2.11.gemfile +8 -0
  7. data/gemfiles/rails_5.0.7.gemfile +8 -0
  8. data/gemfiles/rails_5.1.6.gemfile +8 -0
  9. data/gemfiles/rails_5.2.2.gemfile +8 -0
  10. data/gemfiles/rails_6.0.0.gemfile +8 -0
  11. data/lib/store_base_sti_class.rb +3 -5
  12. data/lib/store_base_sti_class/version.rb +1 -1
  13. data/lib/store_base_sti_class_for_5_0.rb +16 -0
  14. data/lib/store_base_sti_class_for_5_1.rb +16 -0
  15. data/lib/store_base_sti_class_for_6_0.rb +153 -0
  16. data/store_base_sti_class.gemspec +1 -2
  17. metadata +10 -85
  18. data/gemfiles/rails_4.0.0.gemfile +0 -7
  19. data/gemfiles/rails_4.0.1.gemfile +0 -7
  20. data/gemfiles/rails_4.0.10.gemfile +0 -7
  21. data/gemfiles/rails_4.0.11.1.gemfile +0 -7
  22. data/gemfiles/rails_4.0.11.gemfile +0 -7
  23. data/gemfiles/rails_4.0.12.gemfile +0 -7
  24. data/gemfiles/rails_4.0.13.gemfile +0 -7
  25. data/gemfiles/rails_4.0.2.gemfile +0 -7
  26. data/gemfiles/rails_4.0.3.gemfile +0 -7
  27. data/gemfiles/rails_4.0.4.gemfile +0 -7
  28. data/gemfiles/rails_4.0.5.gemfile +0 -7
  29. data/gemfiles/rails_4.0.6.gemfile +0 -7
  30. data/gemfiles/rails_4.0.7.gemfile +0 -7
  31. data/gemfiles/rails_4.0.8.gemfile +0 -7
  32. data/gemfiles/rails_4.0.9.gemfile +0 -7
  33. data/gemfiles/rails_4.1.0.gemfile +0 -7
  34. data/gemfiles/rails_4.1.1.gemfile +0 -7
  35. data/gemfiles/rails_4.1.10.gemfile +0 -7
  36. data/gemfiles/rails_4.1.11.gemfile +0 -7
  37. data/gemfiles/rails_4.1.12.gemfile +0 -7
  38. data/gemfiles/rails_4.1.13.gemfile +0 -7
  39. data/gemfiles/rails_4.1.14.1.gemfile +0 -7
  40. data/gemfiles/rails_4.1.14.2.gemfile +0 -7
  41. data/gemfiles/rails_4.1.14.gemfile +0 -7
  42. data/gemfiles/rails_4.1.15.gemfile +0 -7
  43. data/gemfiles/rails_4.1.2.gemfile +0 -7
  44. data/gemfiles/rails_4.1.3.gemfile +0 -7
  45. data/gemfiles/rails_4.1.4.gemfile +0 -7
  46. data/gemfiles/rails_4.1.5.gemfile +0 -7
  47. data/gemfiles/rails_4.1.6.gemfile +0 -7
  48. data/gemfiles/rails_4.1.7.1.gemfile +0 -7
  49. data/gemfiles/rails_4.1.7.gemfile +0 -7
  50. data/gemfiles/rails_4.1.8.gemfile +0 -7
  51. data/gemfiles/rails_4.1.9.gemfile +0 -7
  52. data/gemfiles/rails_4.2.0.gemfile +0 -7
  53. data/gemfiles/rails_4.2.1.gemfile +0 -7
  54. data/gemfiles/rails_4.2.10.gemfile +0 -7
  55. data/gemfiles/rails_4.2.2.gemfile +0 -7
  56. data/gemfiles/rails_4.2.3.gemfile +0 -7
  57. data/gemfiles/rails_4.2.4.gemfile +0 -7
  58. data/gemfiles/rails_4.2.5.1.gemfile +0 -7
  59. data/gemfiles/rails_4.2.5.2.gemfile +0 -7
  60. data/gemfiles/rails_4.2.5.gemfile +0 -7
  61. data/gemfiles/rails_4.2.6.gemfile +0 -7
  62. data/gemfiles/rails_4.2.7.1.gemfile +0 -7
  63. data/gemfiles/rails_4.2.7.gemfile +0 -7
  64. data/gemfiles/rails_4.2.8.gemfile +0 -7
  65. data/gemfiles/rails_4.2.9.gemfile +0 -7
  66. data/gemfiles/rails_5.0.0.1.gemfile +0 -7
  67. data/gemfiles/rails_5.0.0.gemfile +0 -7
  68. data/gemfiles/rails_5.0.1.gemfile +0 -7
  69. data/gemfiles/rails_5.0.2.gemfile +0 -7
  70. data/gemfiles/rails_5.0.3.gemfile +0 -7
  71. data/gemfiles/rails_5.0.4.gemfile +0 -7
  72. data/gemfiles/rails_5.0.5.gemfile +0 -7
  73. data/gemfiles/rails_5.0.6.gemfile +0 -7
  74. data/gemfiles/rails_5.1.0.gemfile +0 -7
  75. data/gemfiles/rails_5.1.1.gemfile +0 -7
  76. data/gemfiles/rails_5.1.2.gemfile +0 -7
  77. data/gemfiles/rails_5.1.3.gemfile +0 -7
  78. data/gemfiles/rails_5.1.4.gemfile +0 -7
  79. data/gemfiles/rails_5.1.5.gemfile +0 -7
  80. data/gemfiles/rails_5.2.0.gemfile +0 -7
  81. data/lib/store_base_sti_class_for_4_0.rb +0 -337
  82. data/lib/store_base_sti_class_for_4_1.rb +0 -360
  83. data/store_base_sti_class-1.1.0.gem +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: aabffbc0536524e6c2a17e341a8959ce740b8c46
4
- data.tar.gz: f9d1c2edbd97b6e0971abcb5eb0b567ec0c0350b
2
+ SHA256:
3
+ metadata.gz: ee6cec99ea0b5aba6ca085f1119ad70837f40502af8b248d78d466e649b66a02
4
+ data.tar.gz: 6b2e0d8ba7829f225efa6eeb98fc74b30568f922bfe4664dbf8c1b060e4a3b6b
5
5
  SHA512:
6
- metadata.gz: 25f4895ee9da5799f5ba11d65dc831c7c8fdfe07ce169eb74c471a50345674028bb57d8e15e8a56a37e71d18d365a4b457ea3f03d57111a2b3561915ce935607
7
- data.tar.gz: 9cab2026fa30b27d2901ad0574d0cf478405dce07068298a7f77f595959dd17dcba8c66936c3125a7822ee1b58e2af168ba5e2aad359cf71704456b1a9bd5717
6
+ metadata.gz: 229854ef106a719bde09b41a71e821aa2cd69ee73877125f5fbc83a9b2fe9467e1e4b4f0d934579f7c1650ba53adbc581a4b08efe50837ef03fa42be6381e870
7
+ data.tar.gz: 950d8d123ba09b1d26c88ac3601b676ba61018e24697792487bb07da21686a4ce3749c378f667f2eb69948a51b87a3631ee2308e2346012be80629188b351bb5
data/Appraisals CHANGED
@@ -1,71 +1,14 @@
1
- RAILS_VERSIONS = %w(
2
- 4.0.0
3
- 4.0.1
4
- 4.0.2
5
- 4.0.3
6
- 4.0.4
7
- 4.0.5
8
- 4.0.6
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
- 4.2.9
49
- 4.2.10
50
- 5.0.0
51
- 5.0.0.1
52
- 5.0.1
53
- 5.0.2
54
- 5.0.3
55
- 5.0.4
56
- 5.0.5
57
- 5.0.6
58
- 5.1.0
59
- 5.1.1
60
- 5.1.2
61
- 5.1.3
62
- 5.1.4
63
- 5.1.5
64
- 5.2.0
65
- )
1
+ RAILS_VERSIONS = %w[
2
+ 4.2.11
3
+ 5.0.7
4
+ 5.1.6
5
+ 5.2.2
6
+ 6.0.0
7
+ ].freeze
66
8
 
67
9
  RAILS_VERSIONS.each do |version|
68
10
  appraise "rails_#{version}" do
69
11
  gem 'activerecord', version
12
+ gem 'sqlite3', version == '6.0.0' ? '~> 1.4.0' : '~> 1.3.0'
70
13
  end
71
14
  end
@@ -1,38 +1,37 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- store_base_sti_class (1.1.0)
4
+ store_base_sti_class (2.0.0)
5
5
  activerecord (>= 4.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (5.1.4)
11
- activesupport (= 5.1.4)
12
- activerecord (5.1.4)
13
- activemodel (= 5.1.4)
14
- activesupport (= 5.1.4)
15
- arel (~> 8.0)
16
- activesupport (5.1.4)
10
+ activemodel (6.0.1)
11
+ activesupport (= 6.0.1)
12
+ activerecord (6.0.1)
13
+ activemodel (= 6.0.1)
14
+ activesupport (= 6.0.1)
15
+ activesupport (6.0.1)
17
16
  concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (~> 0.7)
17
+ i18n (>= 0.7, < 2)
19
18
  minitest (~> 5.1)
20
19
  tzinfo (~> 1.1)
20
+ zeitwerk (~> 2.2)
21
21
  appraisal (2.2.0)
22
22
  bundler
23
23
  rake
24
24
  thor (>= 0.14.0)
25
- arel (8.0.0)
26
- concurrent-ruby (1.0.5)
27
- i18n (0.9.5)
25
+ concurrent-ruby (1.1.5)
26
+ i18n (1.7.0)
28
27
  concurrent-ruby (~> 1.0)
29
- minitest (5.11.1)
30
- rake (12.3.1)
31
- sqlite3 (1.3.13)
32
- thor (0.20.0)
28
+ minitest (5.13.0)
29
+ rake (13.0.1)
30
+ thor (0.20.3)
33
31
  thread_safe (0.3.6)
34
32
  tzinfo (1.2.5)
35
33
  thread_safe (~> 0.1)
34
+ zeitwerk (2.2.1)
36
35
 
37
36
  PLATFORMS
38
37
  ruby
@@ -41,8 +40,7 @@ DEPENDENCIES
41
40
  appraisal
42
41
  bundler
43
42
  minitest (>= 4.0)
44
- sqlite3
45
43
  store_base_sti_class!
46
44
 
47
45
  BUNDLED WITH
48
- 1.16.1
46
+ 1.17.2
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011-2015 AppFolio, inc.
1
+ Copyright (c) 2011-2019 AppFolio, inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -35,12 +35,18 @@ will output:
35
35
 
36
36
  Notice that addressable_type column is Person even though the actual class is Vendor.
37
37
 
38
- Normally, this isn't a problem, however, it can have negative performance characteristics in certain circumstances. The most obvious one is that
39
- a join with persons or an extra query is required to find out the actual type of addressable.
38
+ Normally, this isn't a problem, however, it can have negative performance
39
+ characteristics in certain circumstances. The most obvious one is that a join
40
+ with persons or an extra query is required to find out the actual type of
41
+ addressable.
40
42
 
41
- This gem adds the ActiveRecord::Base.store_base_sti_class configuration option. It defaults to true for backwards compatibility. Setting it to false will alter ActiveRecord's behavior to store the actual class in polymorphic _type columns when STI is used.
43
+ This gem adds the ActiveRecord::Base.store_base_sti_class configuration
44
+ option. It defaults to true for backwards compatibility. Setting it to false
45
+ will alter ActiveRecord's behavior to store the actual class in polymorphic
46
+ _type columns when STI is used.
42
47
 
43
48
  In the example above, if the ActiveRecord::Base.store_base_sti_class is false, the output will be,
49
+
44
50
  ```
45
51
  #<Vendor id: 1, type: "Vendor" ...>
46
52
  #<Address id: 1, addressable_id: 1, addressable_type: 'Vendor' ...>
@@ -54,21 +60,26 @@ Add the following line to your Gemfile,
54
60
  gem 'store_base_sti_class'
55
61
  ```
56
62
 
57
- then bundle install. Once you have the gem installed, add the following to one of the initializers (or make a new one) in config/initializers,
63
+ then bundle install. Once you have the gem installed, add the following to one
64
+ of the initializers (or make a new one) in config/initializers,
58
65
 
59
- ActiveRecord::Base.store_base_sti_class = false
66
+ ActiveRecord::Base.store_base_sti_class = false
60
67
 
61
- When changing this behavior, you will have write a migration to update all of your existing _type columns accordingly. You may also need to change your application if it explicitly relies on the _type columns.
68
+ When changing this behavior, you will have write a migration to update all of
69
+ your existing _type columns accordingly. You may also need to change your
70
+ application if it explicitly relies on the _type columns.
62
71
 
63
72
  ## Notes
64
73
 
65
74
  This gem incorporates work from:
75
+
66
76
  - https://github.com/codepodu/store_base_sti_class_for_4_0
67
77
 
68
- It currently works with ActiveRecord 4.0.x through 5.0.x. If you need support for ActiveRecord 3.x, use a pre-1.0 version of the gem.
78
+ It currently works with ActiveRecord 4.2.x through 6.0.x. If you need support
79
+ for ActiveRecord 3.x, use a pre-1.0 version of the gem, or ActiveRecord < 4.2
80
+ use a pre-2.0 version of the gem.
69
81
 
70
82
  ## Copyright
71
83
 
72
- Copyright (c) 2011-2017 AppFolio, inc. See LICENSE.txt for
84
+ Copyright (c) 2011-2019 AppFolio, inc. See LICENSE.txt for
73
85
  further details.
74
-
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "4.2.11"
6
+ gem "sqlite3", "~> 1.3.0"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "5.0.7"
6
+ gem "sqlite3", "~> 1.3.0"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "5.1.6"
6
+ gem "sqlite3", "~> 1.3.0"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "5.2.2"
6
+ gem "sqlite3", "~> 1.3.0"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "6.0.0"
6
+ gem "sqlite3", "~> 1.4.0"
7
+
8
+ gemspec path: "../"
@@ -1,10 +1,6 @@
1
1
  require 'active_record'
2
2
 
3
- if ActiveRecord::VERSION::STRING =~ /^4\.0/
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/
3
+ if ActiveRecord::VERSION::STRING =~ /^4\.2/
8
4
  require 'store_base_sti_class_for_4_2'
9
5
  elsif ActiveRecord::VERSION::STRING =~ /^5\.0/
10
6
  require 'store_base_sti_class_for_5_0'
@@ -12,6 +8,8 @@ elsif ActiveRecord::VERSION::STRING =~ /^5\.1/
12
8
  require 'store_base_sti_class_for_5_1'
13
9
  elsif ActiveRecord::VERSION::STRING =~ /^5\.2/
14
10
  require 'store_base_sti_class_for_5_2'
11
+ elsif ActiveRecord::VERSION::STRING =~ /^6\.0/
12
+ require 'store_base_sti_class_for_6_0'
15
13
  end
16
14
 
17
15
  module StoreBaseSTIClass
@@ -1,3 +1,3 @@
1
1
  module StoreBaseSTIClass
2
- VERSION = '1.2.0'.freeze
2
+ VERSION = '2.0.0'.freeze
3
3
  end
@@ -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,153 @@
1
+ require 'active_record/associations/join_dependency/join_part'
2
+
3
+ if ActiveRecord::VERSION::STRING =~ /^6\.0/
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
+ def build_through_record(record)
114
+ @through_records[record.object_id] ||= begin
115
+ ensure_mutable
116
+
117
+ attributes = through_scope_attributes
118
+ attributes[source_reflection.name] = record
119
+
120
+ # START PATCH
121
+ if ActiveRecord::Base.store_base_sti_class
122
+ attributes[source_reflection.foreign_type] = options[:source_type] if options[:source_type]
123
+ end
124
+ # END PATCH
125
+
126
+ through_association.build(attributes)
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ module Reflection
133
+ class PolymorphicReflection
134
+ def source_type_scope
135
+ type = @previous_reflection.foreign_type
136
+ source_type = @previous_reflection.options[:source_type]
137
+
138
+ # START PATCH
139
+ adjusted_source_type =
140
+ if ActiveRecord::Base.store_base_sti_class
141
+ source_type
142
+ else
143
+ ([source_type.constantize] + source_type.constantize.descendants).map(&:to_s)
144
+ end
145
+ # END PATCH
146
+
147
+ lambda { |object| where(type => adjusted_source_type) }
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ end