activerecord 6.1.4.1 → 7.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (237) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1132 -936
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +1 -1
  5. data/lib/active_record/aggregations.rb +1 -1
  6. data/lib/active_record/association_relation.rb +0 -10
  7. data/lib/active_record/associations/association.rb +33 -17
  8. data/lib/active_record/associations/association_scope.rb +1 -3
  9. data/lib/active_record/associations/belongs_to_association.rb +15 -4
  10. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
  11. data/lib/active_record/associations/builder/association.rb +8 -2
  12. data/lib/active_record/associations/builder/belongs_to.rb +19 -6
  13. data/lib/active_record/associations/builder/collection_association.rb +10 -3
  14. data/lib/active_record/associations/builder/has_many.rb +3 -2
  15. data/lib/active_record/associations/builder/has_one.rb +2 -1
  16. data/lib/active_record/associations/builder/singular_association.rb +2 -2
  17. data/lib/active_record/associations/collection_association.rb +34 -27
  18. data/lib/active_record/associations/collection_proxy.rb +8 -3
  19. data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
  20. data/lib/active_record/associations/has_many_association.rb +1 -1
  21. data/lib/active_record/associations/has_many_through_association.rb +2 -1
  22. data/lib/active_record/associations/has_one_association.rb +10 -7
  23. data/lib/active_record/associations/has_one_through_association.rb +1 -1
  24. data/lib/active_record/associations/join_dependency.rb +6 -2
  25. data/lib/active_record/associations/preloader/association.rb +187 -55
  26. data/lib/active_record/associations/preloader/batch.rb +48 -0
  27. data/lib/active_record/associations/preloader/branch.rb +147 -0
  28. data/lib/active_record/associations/preloader/through_association.rb +49 -13
  29. data/lib/active_record/associations/preloader.rb +39 -113
  30. data/lib/active_record/associations/singular_association.rb +8 -2
  31. data/lib/active_record/associations/through_association.rb +3 -3
  32. data/lib/active_record/associations.rb +118 -90
  33. data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
  34. data/lib/active_record/attribute_assignment.rb +1 -1
  35. data/lib/active_record/attribute_methods/before_type_cast.rb +7 -2
  36. data/lib/active_record/attribute_methods/dirty.rb +49 -16
  37. data/lib/active_record/attribute_methods/primary_key.rb +2 -2
  38. data/lib/active_record/attribute_methods/query.rb +2 -2
  39. data/lib/active_record/attribute_methods/read.rb +7 -5
  40. data/lib/active_record/attribute_methods/serialization.rb +66 -12
  41. data/lib/active_record/attribute_methods/time_zone_conversion.rb +4 -3
  42. data/lib/active_record/attribute_methods/write.rb +7 -10
  43. data/lib/active_record/attribute_methods.rb +13 -14
  44. data/lib/active_record/attributes.rb +24 -35
  45. data/lib/active_record/autosave_association.rb +6 -21
  46. data/lib/active_record/base.rb +19 -1
  47. data/lib/active_record/callbacks.rb +2 -2
  48. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +292 -0
  49. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +209 -0
  50. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +76 -0
  51. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +47 -561
  52. data/lib/active_record/connection_adapters/abstract/database_limits.rb +0 -17
  53. data/lib/active_record/connection_adapters/abstract/database_statements.rb +46 -22
  54. data/lib/active_record/connection_adapters/abstract/query_cache.rb +24 -12
  55. data/lib/active_record/connection_adapters/abstract/quoting.rb +42 -72
  56. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +4 -17
  57. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +34 -9
  58. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +78 -22
  59. data/lib/active_record/connection_adapters/abstract/transaction.rb +15 -22
  60. data/lib/active_record/connection_adapters/abstract_adapter.rb +149 -74
  61. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +97 -81
  62. data/lib/active_record/connection_adapters/column.rb +4 -0
  63. data/lib/active_record/connection_adapters/mysql/database_statements.rb +37 -23
  64. data/lib/active_record/connection_adapters/mysql/quoting.rb +35 -21
  65. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +5 -1
  66. data/lib/active_record/connection_adapters/mysql2_adapter.rb +12 -6
  67. data/lib/active_record/connection_adapters/pool_config.rb +7 -7
  68. data/lib/active_record/connection_adapters/postgresql/column.rb +17 -1
  69. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +21 -12
  70. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
  71. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
  72. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
  73. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +1 -1
  74. data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
  75. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
  76. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
  77. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
  78. data/lib/active_record/connection_adapters/postgresql/quoting.rb +50 -50
  79. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +32 -0
  80. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +21 -1
  81. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +22 -1
  82. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +25 -0
  83. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +29 -18
  84. data/lib/active_record/connection_adapters/postgresql_adapter.rb +205 -105
  85. data/lib/active_record/connection_adapters/schema_cache.rb +29 -4
  86. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +27 -19
  87. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +28 -16
  88. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +16 -14
  89. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +89 -30
  90. data/lib/active_record/connection_adapters.rb +6 -5
  91. data/lib/active_record/connection_handling.rb +47 -53
  92. data/lib/active_record/core.rb +122 -132
  93. data/lib/active_record/database_configurations/connection_url_resolver.rb +2 -1
  94. data/lib/active_record/database_configurations/database_config.rb +12 -9
  95. data/lib/active_record/database_configurations/hash_config.rb +63 -5
  96. data/lib/active_record/database_configurations/url_config.rb +2 -2
  97. data/lib/active_record/database_configurations.rb +16 -32
  98. data/lib/active_record/delegated_type.rb +52 -11
  99. data/lib/active_record/destroy_association_async_job.rb +1 -1
  100. data/lib/active_record/disable_joins_association_relation.rb +39 -0
  101. data/lib/active_record/dynamic_matchers.rb +1 -1
  102. data/lib/active_record/encryption/cipher/aes256_gcm.rb +98 -0
  103. data/lib/active_record/encryption/cipher.rb +53 -0
  104. data/lib/active_record/encryption/config.rb +44 -0
  105. data/lib/active_record/encryption/configurable.rb +61 -0
  106. data/lib/active_record/encryption/context.rb +35 -0
  107. data/lib/active_record/encryption/contexts.rb +72 -0
  108. data/lib/active_record/encryption/derived_secret_key_provider.rb +12 -0
  109. data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
  110. data/lib/active_record/encryption/encryptable_record.rb +208 -0
  111. data/lib/active_record/encryption/encrypted_attribute_type.rb +140 -0
  112. data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
  113. data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
  114. data/lib/active_record/encryption/encryptor.rb +155 -0
  115. data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
  116. data/lib/active_record/encryption/errors.rb +15 -0
  117. data/lib/active_record/encryption/extended_deterministic_queries.rb +160 -0
  118. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
  119. data/lib/active_record/encryption/key.rb +28 -0
  120. data/lib/active_record/encryption/key_generator.rb +42 -0
  121. data/lib/active_record/encryption/key_provider.rb +46 -0
  122. data/lib/active_record/encryption/message.rb +33 -0
  123. data/lib/active_record/encryption/message_serializer.rb +90 -0
  124. data/lib/active_record/encryption/null_encryptor.rb +21 -0
  125. data/lib/active_record/encryption/properties.rb +76 -0
  126. data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
  127. data/lib/active_record/encryption/scheme.rb +99 -0
  128. data/lib/active_record/encryption.rb +55 -0
  129. data/lib/active_record/enum.rb +49 -42
  130. data/lib/active_record/errors.rb +67 -4
  131. data/lib/active_record/explain_registry.rb +11 -6
  132. data/lib/active_record/fixture_set/file.rb +15 -1
  133. data/lib/active_record/fixture_set/table_row.rb +41 -6
  134. data/lib/active_record/fixture_set/table_rows.rb +4 -4
  135. data/lib/active_record/fixtures.rb +17 -20
  136. data/lib/active_record/future_result.rb +139 -0
  137. data/lib/active_record/gem_version.rb +4 -4
  138. data/lib/active_record/inheritance.rb +55 -17
  139. data/lib/active_record/insert_all.rb +80 -14
  140. data/lib/active_record/integration.rb +4 -3
  141. data/lib/active_record/internal_metadata.rb +3 -5
  142. data/lib/active_record/legacy_yaml_adapter.rb +2 -39
  143. data/lib/active_record/locking/optimistic.rb +10 -9
  144. data/lib/active_record/locking/pessimistic.rb +9 -3
  145. data/lib/active_record/log_subscriber.rb +14 -3
  146. data/lib/active_record/middleware/database_selector/resolver.rb +6 -10
  147. data/lib/active_record/middleware/database_selector.rb +8 -3
  148. data/lib/active_record/middleware/shard_selector.rb +60 -0
  149. data/lib/active_record/migration/command_recorder.rb +4 -4
  150. data/lib/active_record/migration/compatibility.rb +107 -3
  151. data/lib/active_record/migration/join_table.rb +1 -1
  152. data/lib/active_record/migration.rb +109 -79
  153. data/lib/active_record/model_schema.rb +45 -58
  154. data/lib/active_record/nested_attributes.rb +13 -12
  155. data/lib/active_record/no_touching.rb +3 -3
  156. data/lib/active_record/null_relation.rb +2 -6
  157. data/lib/active_record/persistence.rb +219 -52
  158. data/lib/active_record/query_cache.rb +2 -2
  159. data/lib/active_record/query_logs.rb +138 -0
  160. data/lib/active_record/querying.rb +15 -5
  161. data/lib/active_record/railtie.rb +127 -17
  162. data/lib/active_record/railties/controller_runtime.rb +1 -1
  163. data/lib/active_record/railties/databases.rake +66 -129
  164. data/lib/active_record/readonly_attributes.rb +11 -0
  165. data/lib/active_record/reflection.rb +67 -50
  166. data/lib/active_record/relation/batches/batch_enumerator.rb +19 -5
  167. data/lib/active_record/relation/batches.rb +3 -3
  168. data/lib/active_record/relation/calculations.rb +43 -38
  169. data/lib/active_record/relation/delegation.rb +6 -6
  170. data/lib/active_record/relation/finder_methods.rb +31 -35
  171. data/lib/active_record/relation/merger.rb +20 -13
  172. data/lib/active_record/relation/predicate_builder.rb +1 -6
  173. data/lib/active_record/relation/query_attribute.rb +5 -11
  174. data/lib/active_record/relation/query_methods.rb +243 -61
  175. data/lib/active_record/relation/record_fetch_warning.rb +7 -9
  176. data/lib/active_record/relation/spawn_methods.rb +2 -2
  177. data/lib/active_record/relation/where_clause.rb +10 -19
  178. data/lib/active_record/relation.rb +184 -84
  179. data/lib/active_record/result.rb +17 -7
  180. data/lib/active_record/runtime_registry.rb +9 -13
  181. data/lib/active_record/sanitization.rb +11 -7
  182. data/lib/active_record/schema_dumper.rb +10 -3
  183. data/lib/active_record/schema_migration.rb +4 -4
  184. data/lib/active_record/scoping/default.rb +61 -12
  185. data/lib/active_record/scoping/named.rb +3 -11
  186. data/lib/active_record/scoping.rb +64 -34
  187. data/lib/active_record/serialization.rb +1 -1
  188. data/lib/active_record/signed_id.rb +1 -1
  189. data/lib/active_record/suppressor.rb +11 -15
  190. data/lib/active_record/tasks/database_tasks.rb +120 -58
  191. data/lib/active_record/tasks/mysql_database_tasks.rb +1 -1
  192. data/lib/active_record/tasks/postgresql_database_tasks.rb +19 -12
  193. data/lib/active_record/test_databases.rb +1 -1
  194. data/lib/active_record/test_fixtures.rb +4 -4
  195. data/lib/active_record/timestamp.rb +3 -4
  196. data/lib/active_record/transactions.rb +9 -14
  197. data/lib/active_record/translation.rb +2 -2
  198. data/lib/active_record/type/adapter_specific_registry.rb +32 -7
  199. data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
  200. data/lib/active_record/type/internal/timezone.rb +2 -2
  201. data/lib/active_record/type/serialized.rb +1 -1
  202. data/lib/active_record/type/type_map.rb +17 -20
  203. data/lib/active_record/type.rb +1 -2
  204. data/lib/active_record/validations/associated.rb +1 -1
  205. data/lib/active_record/validations/uniqueness.rb +1 -1
  206. data/lib/active_record.rb +204 -28
  207. data/lib/arel/attributes/attribute.rb +0 -8
  208. data/lib/arel/crud.rb +28 -22
  209. data/lib/arel/delete_manager.rb +18 -4
  210. data/lib/arel/filter_predications.rb +9 -0
  211. data/lib/arel/insert_manager.rb +2 -3
  212. data/lib/arel/nodes/casted.rb +1 -1
  213. data/lib/arel/nodes/delete_statement.rb +12 -13
  214. data/lib/arel/nodes/filter.rb +10 -0
  215. data/lib/arel/nodes/function.rb +1 -0
  216. data/lib/arel/nodes/insert_statement.rb +2 -2
  217. data/lib/arel/nodes/select_core.rb +2 -2
  218. data/lib/arel/nodes/select_statement.rb +2 -2
  219. data/lib/arel/nodes/update_statement.rb +8 -3
  220. data/lib/arel/nodes.rb +1 -0
  221. data/lib/arel/predications.rb +11 -3
  222. data/lib/arel/select_manager.rb +10 -4
  223. data/lib/arel/table.rb +0 -1
  224. data/lib/arel/tree_manager.rb +0 -12
  225. data/lib/arel/update_manager.rb +18 -4
  226. data/lib/arel/visitors/dot.rb +80 -90
  227. data/lib/arel/visitors/mysql.rb +8 -2
  228. data/lib/arel/visitors/postgresql.rb +0 -10
  229. data/lib/arel/visitors/to_sql.rb +58 -2
  230. data/lib/arel.rb +2 -1
  231. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
  232. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
  233. data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
  234. data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
  235. data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
  236. data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
  237. metadata +59 -14
@@ -10,10 +10,6 @@ module ActiveRecord
10
10
  # to be executed the next time.
11
11
  class SchemaMigration < ActiveRecord::Base # :nodoc:
12
12
  class << self
13
- def _internal?
14
- true
15
- end
16
-
17
13
  def primary_key
18
14
  "version"
19
15
  end
@@ -45,6 +41,10 @@ module ActiveRecord
45
41
  def all_versions
46
42
  order(:version).pluck(:version)
47
43
  end
44
+
45
+ def table_exists?
46
+ connection.data_source_exists?(table_name)
47
+ end
48
48
  end
49
49
 
50
50
  def version
@@ -2,6 +2,15 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Scoping
5
+ class DefaultScope # :nodoc:
6
+ attr_reader :scope, :all_queries
7
+
8
+ def initialize(scope, all_queries = nil)
9
+ @scope = scope
10
+ @all_queries = all_queries
11
+ end
12
+ end
13
+
5
14
  module Default
6
15
  extend ActiveSupport::Concern
7
16
 
@@ -30,8 +39,8 @@ module ActiveRecord
30
39
  # Post.unscoped {
31
40
  # Post.limit(10) # Fires "SELECT * FROM posts LIMIT 10"
32
41
  # }
33
- def unscoped
34
- block_given? ? relation.scoping { yield } : relation
42
+ def unscoped(&block)
43
+ block_given? ? relation.scoping(&block) : relation
35
44
  end
36
45
 
37
46
  # Are there attributes associated with this scope?
@@ -39,10 +48,21 @@ module ActiveRecord
39
48
  super || default_scopes.any? || respond_to?(:default_scope)
40
49
  end
41
50
 
42
- def before_remove_const #:nodoc:
51
+ def before_remove_const # :nodoc:
43
52
  self.current_scope = nil
44
53
  end
45
54
 
55
+ # Checks if the model has any default scopes. If all_queries
56
+ # is set to true, the method will check if there are any
57
+ # default_scopes for the model where +all_queries+ is true.
58
+ def default_scopes?(all_queries: false)
59
+ if all_queries
60
+ self.default_scopes.any?(&:all_queries)
61
+ else
62
+ self.default_scopes.any?
63
+ end
64
+ end
65
+
46
66
  private
47
67
  # Use this macro in your model to set a default scope for all operations on
48
68
  # the model.
@@ -54,11 +74,26 @@ module ActiveRecord
54
74
  # Article.all # => SELECT * FROM articles WHERE published = true
55
75
  #
56
76
  # The #default_scope is also applied while creating/building a record.
57
- # It is not applied while updating a record.
77
+ # It is not applied while updating or deleting a record.
58
78
  #
59
79
  # Article.new.published # => true
60
80
  # Article.create.published # => true
61
81
  #
82
+ # To apply a #default_scope when updating or deleting a record, add
83
+ # <tt>all_queries: true</tt>:
84
+ #
85
+ # class Article < ActiveRecord::Base
86
+ # default_scope { where(blog_id: 1) }, all_queries: true
87
+ # end
88
+ #
89
+ # Applying a default scope to all queries will ensure that records
90
+ # are always queried by the additional conditions. Note that only
91
+ # where clauses apply, as it does not make sense to add order to
92
+ # queries that return a single object by primary key.
93
+ #
94
+ # Article.find(1).destroy
95
+ # => DELETE ... FROM `articles` where ID = 1 AND blog_id = 1;
96
+ #
62
97
  # (You can also pass any object which responds to +call+ to the
63
98
  # +default_scope+ macro, and it will be called when building the
64
99
  # default scope.)
@@ -85,7 +120,7 @@ module ActiveRecord
85
120
  # # Should return a scope, you can call 'super' here etc.
86
121
  # end
87
122
  # end
88
- def default_scope(scope = nil, &block) # :doc:
123
+ def default_scope(scope = nil, all_queries: nil, &block) # :doc:
89
124
  scope = block if block_given?
90
125
 
91
126
  if scope.is_a?(Relation) || !scope.respond_to?(:call)
@@ -96,10 +131,12 @@ module ActiveRecord
96
131
  "self.default_scope.)"
97
132
  end
98
133
 
99
- self.default_scopes += [scope]
134
+ default_scope = DefaultScope.new(scope, all_queries)
135
+
136
+ self.default_scopes += [default_scope]
100
137
  end
101
138
 
102
- def build_default_scope(relation = relation())
139
+ def build_default_scope(relation = relation(), all_queries: nil)
103
140
  return if abstract_class?
104
141
 
105
142
  if default_scope_override.nil?
@@ -113,20 +150,32 @@ module ActiveRecord
113
150
  end
114
151
  elsif default_scopes.any?
115
152
  evaluate_default_scope do
116
- default_scopes.inject(relation) do |default_scope, scope|
117
- scope = scope.respond_to?(:to_proc) ? scope : scope.method(:call)
118
- default_scope.instance_exec(&scope) || default_scope
153
+ default_scopes.inject(relation) do |default_scope, scope_obj|
154
+ if execute_scope?(all_queries, scope_obj)
155
+ scope = scope_obj.scope.respond_to?(:to_proc) ? scope_obj.scope : scope_obj.scope.method(:call)
156
+
157
+ default_scope.instance_exec(&scope) || default_scope
158
+ end
119
159
  end
120
160
  end
121
161
  end
122
162
  end
123
163
 
164
+ # If all_queries is nil, only execute on select and insert queries.
165
+ #
166
+ # If all_queries is true, check if the default_scope object has
167
+ # all_queries set, then execute on all queries; select, insert, update
168
+ # and delete.
169
+ def execute_scope?(all_queries, default_scope_obj)
170
+ all_queries.nil? || all_queries && default_scope_obj.all_queries
171
+ end
172
+
124
173
  def ignore_default_scope?
125
- ScopeRegistry.value_for(:ignore_default_scope, base_class)
174
+ ScopeRegistry.ignore_default_scope(base_class)
126
175
  end
127
176
 
128
177
  def ignore_default_scope=(ignore)
129
- ScopeRegistry.set_value_for(:ignore_default_scope, base_class, ignore)
178
+ ScopeRegistry.set_ignore_default_scope(base_class, ignore)
130
179
  end
131
180
 
132
181
  # The ignore_default_scope flag is used to prevent an infinite recursion
@@ -42,8 +42,8 @@ module ActiveRecord
42
42
  end
43
43
 
44
44
  # Returns a scope for the model with default scopes.
45
- def default_scoped(scope = relation)
46
- build_default_scope(scope) || scope
45
+ def default_scoped(scope = relation, all_queries: nil)
46
+ build_default_scope(scope, all_queries: all_queries) || scope
47
47
  end
48
48
 
49
49
  def default_extensions # :nodoc:
@@ -168,7 +168,6 @@ module ActiveRecord
168
168
  "an instance method with the same name."
169
169
  end
170
170
 
171
- valid_scope_name?(name)
172
171
  extension = Module.new(&block) if block
173
172
 
174
173
  if body.respond_to?(:to_proc)
@@ -184,7 +183,7 @@ module ActiveRecord
184
183
  scope
185
184
  end
186
185
  end
187
- singleton_class.send(:ruby2_keywords, name) if respond_to?(:ruby2_keywords, true)
186
+ singleton_class.send(:ruby2_keywords, name)
188
187
 
189
188
  generate_relation_method(name)
190
189
  end
@@ -193,13 +192,6 @@ module ActiveRecord
193
192
  def singleton_method_added(name)
194
193
  generate_relation_method(name) if Kernel.respond_to?(name) && !ActiveRecord::Relation.method_defined?(name)
195
194
  end
196
-
197
- def valid_scope_name?(name)
198
- if respond_to?(name, true) && logger
199
- logger.warn "Creating scope :#{name}. " \
200
- "Overwriting existing method #{self.name}.#{name}."
201
- end
202
- end
203
195
  end
204
196
  end
205
197
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/per_thread_registry"
3
+ require "active_support/core_ext/module/delegation"
4
4
 
5
5
  module ActiveRecord
6
6
  module Scoping
@@ -24,11 +24,23 @@ module ActiveRecord
24
24
  end
25
25
 
26
26
  def current_scope(skip_inherited_scope = false)
27
- ScopeRegistry.value_for(:current_scope, self, skip_inherited_scope)
27
+ ScopeRegistry.current_scope(self, skip_inherited_scope)
28
28
  end
29
29
 
30
30
  def current_scope=(scope)
31
- ScopeRegistry.set_value_for(:current_scope, self, scope)
31
+ ScopeRegistry.set_current_scope(self, scope)
32
+ end
33
+
34
+ def global_current_scope(skip_inherited_scope = false)
35
+ ScopeRegistry.global_current_scope(self, skip_inherited_scope)
36
+ end
37
+
38
+ def global_current_scope=(scope)
39
+ ScopeRegistry.set_global_current_scope(self, scope)
40
+ end
41
+
42
+ def scope_registry
43
+ ScopeRegistry.instance
32
44
  end
33
45
  end
34
46
 
@@ -45,8 +57,8 @@ module ActiveRecord
45
57
  end
46
58
 
47
59
  # This class stores the +:current_scope+ and +:ignore_default_scope+ values
48
- # for different classes. The registry is stored as a thread local, which is
49
- # accessed through +ScopeRegistry.current+.
60
+ # for different classes. The registry is stored as either a thread or fiber
61
+ # local depending on the application configuration.
50
62
  #
51
63
  # This class allows you to store and get the scope values on different
52
64
  # classes and different types of scopes. For example, if you are attempting
@@ -54,52 +66,70 @@ module ActiveRecord
54
66
  # following code:
55
67
  #
56
68
  # registry = ActiveRecord::Scoping::ScopeRegistry
57
- # registry.set_value_for(:current_scope, Board, some_new_scope)
69
+ # registry.set_current_scope(Board, some_new_scope)
58
70
  #
59
71
  # Now when you run:
60
72
  #
61
- # registry.value_for(:current_scope, Board)
62
- #
63
- # You will obtain whatever was defined in +some_new_scope+. The #value_for
64
- # and #set_value_for methods are delegated to the current ScopeRegistry
65
- # object, so the above example code can also be called as:
73
+ # registry.current_scope(Board)
66
74
  #
67
- # ActiveRecord::Scoping::ScopeRegistry.set_value_for(:current_scope,
68
- # Board, some_new_scope)
75
+ # You will obtain whatever was defined in +some_new_scope+.
69
76
  class ScopeRegistry # :nodoc:
70
- extend ActiveSupport::PerThreadRegistry
77
+ class << self
78
+ delegate :current_scope, :set_current_scope, :ignore_default_scope, :set_ignore_default_scope,
79
+ :global_current_scope, :set_global_current_scope, to: :instance
71
80
 
72
- VALID_SCOPE_TYPES = [:current_scope, :ignore_default_scope]
81
+ def instance
82
+ ActiveSupport::IsolatedExecutionState[:active_record_scope_registry] ||= new
83
+ end
84
+ end
73
85
 
74
86
  def initialize
75
- @registry = Hash.new { |hash, key| hash[key] = {} }
87
+ @current_scope = {}
88
+ @ignore_default_scope = {}
89
+ @global_current_scope = {}
76
90
  end
77
91
 
78
- # Obtains the value for a given +scope_type+ and +model+.
79
- def value_for(scope_type, model, skip_inherited_scope = false)
80
- raise_invalid_scope_type!(scope_type)
81
- return @registry[scope_type][model.name] if skip_inherited_scope
82
- klass = model
83
- base = model.base_class
84
- while klass <= base
85
- value = @registry[scope_type][klass.name]
86
- return value if value
87
- klass = klass.superclass
88
- end
92
+ def current_scope(model, skip_inherited_scope = false)
93
+ value_for(@current_scope, model, skip_inherited_scope)
89
94
  end
90
95
 
91
- # Sets the +value+ for a given +scope_type+ and +model+.
92
- def set_value_for(scope_type, model, value)
93
- raise_invalid_scope_type!(scope_type)
94
- @registry[scope_type][model.name] = value
96
+ def set_current_scope(model, value)
97
+ set_value_for(@current_scope, model, value)
98
+ end
99
+
100
+ def ignore_default_scope(model, skip_inherited_scope = false)
101
+ value_for(@ignore_default_scope, model, skip_inherited_scope)
102
+ end
103
+
104
+ def set_ignore_default_scope(model, value)
105
+ set_value_for(@ignore_default_scope, model, value)
106
+ end
107
+
108
+ def global_current_scope(model, skip_inherited_scope = false)
109
+ value_for(@global_current_scope, model, skip_inherited_scope)
110
+ end
111
+
112
+ def set_global_current_scope(model, value)
113
+ set_value_for(@global_current_scope, model, value)
95
114
  end
96
115
 
97
116
  private
98
- def raise_invalid_scope_type!(scope_type)
99
- if !VALID_SCOPE_TYPES.include?(scope_type)
100
- raise ArgumentError, "Invalid scope type '#{scope_type}' sent to the registry. Scope types must be included in VALID_SCOPE_TYPES"
117
+ # Obtains the value for a given +scope_type+ and +model+.
118
+ def value_for(scope_type, model, skip_inherited_scope = false)
119
+ return scope_type[model.name] if skip_inherited_scope
120
+ klass = model
121
+ base = model.base_class
122
+ while klass <= base
123
+ value = scope_type[klass.name]
124
+ return value if value
125
+ klass = klass.superclass
101
126
  end
102
127
  end
128
+
129
+ # Sets the +value+ for a given +scope_type+ and +model+.
130
+ def set_value_for(scope_type, model, value)
131
+ scope_type[model.name] = value
132
+ end
103
133
  end
104
134
  end
105
135
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ActiveRecord #:nodoc:
3
+ module ActiveRecord # :nodoc:
4
4
  # = Active Record \Serialization
5
5
  module Serialization
6
6
  extend ActiveSupport::Concern
@@ -10,7 +10,7 @@ module ActiveRecord
10
10
  # :singleton-method:
11
11
  # Set the secret used for the signed id verifier instance when using Active Record outside of Rails.
12
12
  # Within Rails, this is automatically set using the Rails application key generator.
13
- mattr_accessor :signed_id_verifier_secret, instance_writer: false
13
+ class_attribute :signed_id_verifier_secret, instance_writer: false
14
14
  end
15
15
 
16
16
  module ClassMethods
@@ -30,32 +30,28 @@ module ActiveRecord
30
30
  module Suppressor
31
31
  extend ActiveSupport::Concern
32
32
 
33
+ class << self
34
+ def registry # :nodoc:
35
+ ActiveSupport::IsolatedExecutionState[:active_record_suppresor_registry] ||= {}
36
+ end
37
+ end
38
+
33
39
  module ClassMethods
34
40
  def suppress(&block)
35
- previous_state = SuppressorRegistry.suppressed[name]
36
- SuppressorRegistry.suppressed[name] = true
41
+ previous_state = Suppressor.registry[name]
42
+ Suppressor.registry[name] = true
37
43
  yield
38
44
  ensure
39
- SuppressorRegistry.suppressed[name] = previous_state
45
+ Suppressor.registry[name] = previous_state
40
46
  end
41
47
  end
42
48
 
43
49
  def save(**) # :nodoc:
44
- SuppressorRegistry.suppressed[self.class.name] ? true : super
50
+ Suppressor.registry[self.class.name] ? true : super
45
51
  end
46
52
 
47
53
  def save!(**) # :nodoc:
48
- SuppressorRegistry.suppressed[self.class.name] ? true : super
49
- end
50
- end
51
-
52
- class SuppressorRegistry # :nodoc:
53
- extend ActiveSupport::PerThreadRegistry
54
-
55
- attr_reader :suppressed
56
-
57
- def initialize
58
- @suppressed = {}
54
+ Suppressor.registry[self.class.name] ? true : super
59
55
  end
60
56
  end
61
57
  end