activerecord 7.0.8.1 → 7.2.2.1
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 +4 -4
- data/CHANGELOG.md +642 -1925
- data/MIT-LICENSE +1 -1
- data/README.rdoc +29 -29
- data/examples/performance.rb +2 -2
- data/lib/active_record/aggregations.rb +16 -13
- data/lib/active_record/association_relation.rb +2 -2
- data/lib/active_record/associations/alias_tracker.rb +25 -19
- data/lib/active_record/associations/association.rb +35 -12
- data/lib/active_record/associations/association_scope.rb +16 -9
- data/lib/active_record/associations/belongs_to_association.rb +23 -8
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +3 -2
- data/lib/active_record/associations/builder/association.rb +3 -3
- data/lib/active_record/associations/builder/belongs_to.rb +22 -8
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -7
- data/lib/active_record/associations/builder/has_many.rb +3 -4
- data/lib/active_record/associations/builder/has_one.rb +3 -4
- data/lib/active_record/associations/builder/singular_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +26 -14
- data/lib/active_record/associations/collection_proxy.rb +29 -11
- data/lib/active_record/associations/errors.rb +265 -0
- data/lib/active_record/associations/foreign_association.rb +10 -3
- data/lib/active_record/associations/has_many_association.rb +21 -14
- data/lib/active_record/associations/has_many_through_association.rb +17 -7
- data/lib/active_record/associations/has_one_association.rb +10 -3
- data/lib/active_record/associations/join_dependency/join_association.rb +30 -27
- data/lib/active_record/associations/join_dependency.rb +10 -10
- data/lib/active_record/associations/nested_error.rb +47 -0
- data/lib/active_record/associations/preloader/association.rb +33 -8
- data/lib/active_record/associations/preloader/branch.rb +7 -1
- data/lib/active_record/associations/preloader/through_association.rb +1 -3
- data/lib/active_record/associations/preloader.rb +13 -10
- data/lib/active_record/associations/singular_association.rb +7 -1
- data/lib/active_record/associations/through_association.rb +22 -11
- data/lib/active_record/associations.rb +354 -485
- data/lib/active_record/attribute_assignment.rb +0 -4
- data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
- data/lib/active_record/attribute_methods/composite_primary_key.rb +84 -0
- data/lib/active_record/attribute_methods/dirty.rb +53 -35
- data/lib/active_record/attribute_methods/primary_key.rb +45 -25
- data/lib/active_record/attribute_methods/query.rb +28 -16
- data/lib/active_record/attribute_methods/read.rb +8 -7
- data/lib/active_record/attribute_methods/serialization.rb +131 -32
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +11 -6
- data/lib/active_record/attribute_methods/write.rb +6 -6
- data/lib/active_record/attribute_methods.rb +148 -33
- data/lib/active_record/attributes.rb +64 -50
- data/lib/active_record/autosave_association.rb +69 -37
- data/lib/active_record/base.rb +9 -5
- data/lib/active_record/callbacks.rb +11 -25
- data/lib/active_record/coders/column_serializer.rb +61 -0
- data/lib/active_record/coders/json.rb +1 -1
- data/lib/active_record/coders/yaml_column.rb +70 -42
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +123 -131
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +4 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +323 -88
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +160 -45
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +217 -63
- data/lib/active_record/connection_adapters/abstract/quoting.rb +72 -63
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +307 -129
- data/lib/active_record/connection_adapters/abstract/transaction.rb +367 -75
- data/lib/active_record/connection_adapters/abstract_adapter.rb +510 -111
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +278 -125
- data/lib/active_record/connection_adapters/column.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +26 -139
- data/lib/active_record/connection_adapters/mysql/quoting.rb +53 -54
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +25 -13
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +152 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +101 -68
- data/lib/active_record/connection_adapters/pool_config.rb +20 -10
- data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/column.rb +14 -3
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +100 -43
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -0
- data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +14 -4
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +65 -61
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +151 -2
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +370 -63
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +367 -201
- data/lib/active_record/connection_adapters/schema_cache.rb +302 -79
- data/lib/active_record/connection_adapters/sqlite3/column.rb +62 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +60 -43
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +45 -46
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +14 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +16 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +50 -8
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +290 -110
- data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
- data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
- data/lib/active_record/connection_adapters/trilogy_adapter.rb +229 -0
- data/lib/active_record/connection_adapters.rb +124 -1
- data/lib/active_record/connection_handling.rb +96 -104
- data/lib/active_record/core.rb +251 -176
- data/lib/active_record/counter_cache.rb +68 -34
- data/lib/active_record/database_configurations/connection_url_resolver.rb +8 -3
- data/lib/active_record/database_configurations/database_config.rb +26 -5
- data/lib/active_record/database_configurations/hash_config.rb +52 -34
- data/lib/active_record/database_configurations/url_config.rb +37 -12
- data/lib/active_record/database_configurations.rb +87 -34
- data/lib/active_record/delegated_type.rb +39 -10
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +3 -1
- data/lib/active_record/dynamic_matchers.rb +2 -2
- data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
- data/lib/active_record/encryption/config.rb +25 -1
- data/lib/active_record/encryption/configurable.rb +12 -19
- data/lib/active_record/encryption/context.rb +10 -3
- data/lib/active_record/encryption/contexts.rb +5 -1
- data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
- data/lib/active_record/encryption/encryptable_record.rb +45 -21
- data/lib/active_record/encryption/encrypted_attribute_type.rb +47 -12
- data/lib/active_record/encryption/encryptor.rb +18 -3
- data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -69
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
- data/lib/active_record/encryption/key_generator.rb +12 -1
- data/lib/active_record/encryption/key_provider.rb +1 -1
- data/lib/active_record/encryption/message_pack_message_serializer.rb +76 -0
- data/lib/active_record/encryption/message_serializer.rb +6 -0
- data/lib/active_record/encryption/null_encryptor.rb +4 -0
- data/lib/active_record/encryption/properties.rb +3 -3
- data/lib/active_record/encryption/read_only_null_encryptor.rb +4 -0
- data/lib/active_record/encryption/scheme.rb +22 -21
- data/lib/active_record/encryption.rb +3 -0
- data/lib/active_record/enum.rb +129 -28
- data/lib/active_record/errors.rb +151 -31
- data/lib/active_record/explain.rb +21 -12
- data/lib/active_record/fixture_set/model_metadata.rb +14 -4
- data/lib/active_record/fixture_set/render_context.rb +2 -0
- data/lib/active_record/fixture_set/table_row.rb +29 -8
- data/lib/active_record/fixtures.rb +167 -97
- data/lib/active_record/future_result.rb +47 -8
- data/lib/active_record/gem_version.rb +3 -3
- data/lib/active_record/inheritance.rb +34 -18
- data/lib/active_record/insert_all.rb +72 -22
- data/lib/active_record/integration.rb +11 -8
- data/lib/active_record/internal_metadata.rb +124 -20
- data/lib/active_record/locking/optimistic.rb +8 -7
- data/lib/active_record/locking/pessimistic.rb +5 -2
- data/lib/active_record/log_subscriber.rb +18 -22
- data/lib/active_record/marshalling.rb +59 -0
- data/lib/active_record/message_pack.rb +124 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
- data/lib/active_record/middleware/database_selector.rb +6 -8
- data/lib/active_record/middleware/shard_selector.rb +3 -1
- data/lib/active_record/migration/command_recorder.rb +106 -8
- data/lib/active_record/migration/compatibility.rb +147 -5
- data/lib/active_record/migration/default_strategy.rb +22 -0
- data/lib/active_record/migration/execution_strategy.rb +19 -0
- data/lib/active_record/migration/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +234 -117
- data/lib/active_record/model_schema.rb +90 -102
- data/lib/active_record/nested_attributes.rb +48 -11
- data/lib/active_record/normalization.rb +163 -0
- data/lib/active_record/persistence.rb +168 -339
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +18 -25
- data/lib/active_record/query_logs.rb +92 -52
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +33 -8
- data/lib/active_record/railtie.rb +129 -85
- data/lib/active_record/railties/controller_runtime.rb +22 -7
- data/lib/active_record/railties/databases.rake +145 -154
- data/lib/active_record/railties/job_runtime.rb +23 -0
- data/lib/active_record/readonly_attributes.rb +32 -5
- data/lib/active_record/reflection.rb +267 -69
- data/lib/active_record/relation/batches/batch_enumerator.rb +20 -5
- data/lib/active_record/relation/batches.rb +198 -63
- data/lib/active_record/relation/calculations.rb +250 -93
- data/lib/active_record/relation/delegation.rb +30 -19
- data/lib/active_record/relation/finder_methods.rb +93 -18
- data/lib/active_record/relation/merger.rb +6 -6
- data/lib/active_record/relation/predicate_builder/array_handler.rb +2 -2
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +18 -3
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -7
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
- data/lib/active_record/relation/predicate_builder.rb +28 -16
- data/lib/active_record/relation/query_attribute.rb +2 -1
- data/lib/active_record/relation/query_methods.rb +576 -107
- data/lib/active_record/relation/record_fetch_warning.rb +3 -0
- data/lib/active_record/relation/spawn_methods.rb +5 -4
- data/lib/active_record/relation/where_clause.rb +7 -19
- data/lib/active_record/relation.rb +580 -90
- data/lib/active_record/result.rb +49 -48
- data/lib/active_record/runtime_registry.rb +63 -1
- data/lib/active_record/sanitization.rb +70 -25
- data/lib/active_record/schema.rb +8 -7
- data/lib/active_record/schema_dumper.rb +63 -14
- data/lib/active_record/schema_migration.rb +75 -24
- data/lib/active_record/scoping/default.rb +15 -5
- data/lib/active_record/scoping/named.rb +3 -2
- data/lib/active_record/scoping.rb +2 -1
- data/lib/active_record/secure_password.rb +60 -0
- data/lib/active_record/secure_token.rb +21 -3
- data/lib/active_record/signed_id.rb +27 -6
- data/lib/active_record/statement_cache.rb +7 -7
- data/lib/active_record/store.rb +8 -8
- data/lib/active_record/suppressor.rb +3 -1
- data/lib/active_record/table_metadata.rb +1 -1
- data/lib/active_record/tasks/database_tasks.rb +190 -118
- data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
- data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
- data/lib/active_record/tasks/sqlite_database_tasks.rb +16 -7
- data/lib/active_record/test_fixtures.rb +170 -155
- data/lib/active_record/testing/query_assertions.rb +121 -0
- data/lib/active_record/timestamp.rb +31 -17
- data/lib/active_record/token_for.rb +123 -0
- data/lib/active_record/touch_later.rb +12 -7
- data/lib/active_record/transaction.rb +132 -0
- data/lib/active_record/transactions.rb +106 -24
- data/lib/active_record/translation.rb +0 -2
- data/lib/active_record/type/adapter_specific_registry.rb +1 -8
- data/lib/active_record/type/internal/timezone.rb +7 -2
- data/lib/active_record/type/serialized.rb +1 -3
- data/lib/active_record/type/time.rb +4 -0
- data/lib/active_record/type_caster/connection.rb +4 -4
- data/lib/active_record/validations/absence.rb +1 -1
- data/lib/active_record/validations/associated.rb +9 -3
- data/lib/active_record/validations/numericality.rb +5 -4
- data/lib/active_record/validations/presence.rb +5 -28
- data/lib/active_record/validations/uniqueness.rb +61 -11
- data/lib/active_record/validations.rb +12 -5
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +247 -33
- data/lib/arel/alias_predication.rb +1 -1
- data/lib/arel/collectors/bind.rb +2 -0
- data/lib/arel/collectors/composite.rb +7 -0
- data/lib/arel/collectors/sql_string.rb +1 -1
- data/lib/arel/collectors/substitute_binds.rb +1 -1
- data/lib/arel/errors.rb +10 -0
- data/lib/arel/factory_methods.rb +4 -0
- data/lib/arel/nodes/binary.rb +6 -7
- data/lib/arel/nodes/bound_sql_literal.rb +65 -0
- data/lib/arel/nodes/cte.rb +36 -0
- data/lib/arel/nodes/fragments.rb +35 -0
- data/lib/arel/nodes/homogeneous_in.rb +1 -9
- data/lib/arel/nodes/leading_join.rb +8 -0
- data/lib/arel/nodes/{and.rb → nary.rb} +5 -2
- data/lib/arel/nodes/node.rb +115 -5
- data/lib/arel/nodes/sql_literal.rb +13 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes.rb +6 -2
- data/lib/arel/predications.rb +3 -1
- data/lib/arel/select_manager.rb +1 -1
- data/lib/arel/table.rb +9 -5
- data/lib/arel/tree_manager.rb +8 -3
- data/lib/arel/update_manager.rb +2 -1
- data/lib/arel/visitors/dot.rb +1 -0
- data/lib/arel/visitors/mysql.rb +17 -5
- data/lib/arel/visitors/postgresql.rb +1 -12
- data/lib/arel/visitors/sqlite.rb +25 -0
- data/lib/arel/visitors/to_sql.rb +112 -34
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +21 -3
- data/lib/rails/generators/active_record/application_record/USAGE +8 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +4 -1
- data/lib/rails/generators/active_record/migration.rb +3 -1
- data/lib/rails/generators/active_record/model/USAGE +113 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
- metadata +59 -17
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -63
@@ -6,12 +6,13 @@ module ActiveRecord
|
|
6
6
|
# See ActiveRecord::Attributes::ClassMethods for documentation
|
7
7
|
module Attributes
|
8
8
|
extend ActiveSupport::Concern
|
9
|
+
include ActiveModel::AttributeRegistration
|
9
10
|
|
10
|
-
|
11
|
-
class_attribute :attributes_to_define_after_schema_loads, instance_accessor: false, default: {} # :internal:
|
12
|
-
end
|
13
|
-
|
11
|
+
# = Active Record \Attributes
|
14
12
|
module ClassMethods
|
13
|
+
# :method: attribute
|
14
|
+
# :call-seq: attribute(name, cast_type = nil, **options)
|
15
|
+
#
|
15
16
|
# Defines an attribute with a type on this model. It will override the
|
16
17
|
# type of existing attributes if needed. This allows control over how
|
17
18
|
# values are converted to and from SQL when assigned to a model. It also
|
@@ -24,15 +25,17 @@ module ActiveRecord
|
|
24
25
|
# column which this will persist to.
|
25
26
|
#
|
26
27
|
# +cast_type+ A symbol such as +:string+ or +:integer+, or a type object
|
27
|
-
# to be used for this attribute.
|
28
|
-
#
|
28
|
+
# to be used for this attribute. If this parameter is not passed, the previously
|
29
|
+
# defined type (if any) will be used.
|
30
|
+
# Otherwise, the type will be ActiveModel::Type::Value.
|
31
|
+
# See the examples below for more information about providing custom type objects.
|
29
32
|
#
|
30
33
|
# ==== Options
|
31
34
|
#
|
32
35
|
# The following options are accepted:
|
33
36
|
#
|
34
37
|
# +default+ The default value to use when no value is provided. If this option
|
35
|
-
# is not passed, the
|
38
|
+
# is not passed, the previously defined default value (if any) on the superclass or in the schema will be used.
|
36
39
|
# Otherwise, the default will be +nil+.
|
37
40
|
#
|
38
41
|
# +array+ (PostgreSQL only) specifies that the type should be an array (see the
|
@@ -134,7 +137,7 @@ module ActiveRecord
|
|
134
137
|
# expected API. It is recommended that your type objects inherit from an
|
135
138
|
# existing type, or from ActiveRecord::Type::Value
|
136
139
|
#
|
137
|
-
# class
|
140
|
+
# class PriceType < ActiveRecord::Type::Integer
|
138
141
|
# def cast(value)
|
139
142
|
# if !value.kind_of?(Numeric) && value.include?('$')
|
140
143
|
# price_in_dollars = value.gsub(/\$/, '').to_f
|
@@ -146,11 +149,11 @@ module ActiveRecord
|
|
146
149
|
# end
|
147
150
|
#
|
148
151
|
# # config/initializers/types.rb
|
149
|
-
# ActiveRecord::Type.register(:
|
152
|
+
# ActiveRecord::Type.register(:price, PriceType)
|
150
153
|
#
|
151
154
|
# # app/models/store_listing.rb
|
152
155
|
# class StoreListing < ActiveRecord::Base
|
153
|
-
# attribute :price_in_cents, :
|
156
|
+
# attribute :price_in_cents, :price
|
154
157
|
# end
|
155
158
|
#
|
156
159
|
# store_listing = StoreListing.new(price_in_cents: '$10.00')
|
@@ -170,7 +173,7 @@ module ActiveRecord
|
|
170
173
|
# class Money < Struct.new(:amount, :currency)
|
171
174
|
# end
|
172
175
|
#
|
173
|
-
# class
|
176
|
+
# class PriceType < ActiveRecord::Type::Value
|
174
177
|
# def initialize(currency_converter:)
|
175
178
|
# @currency_converter = currency_converter
|
176
179
|
# end
|
@@ -185,19 +188,19 @@ module ActiveRecord
|
|
185
188
|
# end
|
186
189
|
#
|
187
190
|
# # config/initializers/types.rb
|
188
|
-
# ActiveRecord::Type.register(:
|
191
|
+
# ActiveRecord::Type.register(:price, PriceType)
|
189
192
|
#
|
190
193
|
# # app/models/product.rb
|
191
194
|
# class Product < ActiveRecord::Base
|
192
195
|
# currency_converter = ConversionRatesFromTheInternet.new
|
193
|
-
# attribute :price_in_bitcoins, :
|
196
|
+
# attribute :price_in_bitcoins, :price, currency_converter: currency_converter
|
194
197
|
# end
|
195
198
|
#
|
196
199
|
# Product.where(price_in_bitcoins: Money.new(5, "USD"))
|
197
|
-
# #
|
200
|
+
# # SELECT * FROM products WHERE price_in_bitcoins = 0.02230
|
198
201
|
#
|
199
202
|
# Product.where(price_in_bitcoins: Money.new(5, "GBP"))
|
200
|
-
# #
|
203
|
+
# # SELECT * FROM products WHERE price_in_bitcoins = 0.03412
|
201
204
|
#
|
202
205
|
# ==== Dirty Tracking
|
203
206
|
#
|
@@ -205,37 +208,12 @@ module ActiveRecord
|
|
205
208
|
# tracking is performed. The methods +changed?+ and +changed_in_place?+
|
206
209
|
# will be called from ActiveModel::Dirty. See the documentation for those
|
207
210
|
# methods in ActiveModel::Type::Value for more details.
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
reload_schema_from_cache
|
213
|
-
|
214
|
-
case cast_type
|
215
|
-
when Symbol
|
216
|
-
cast_type = Type.lookup(cast_type, **options, adapter: Type.adapter_name_from(self))
|
217
|
-
when nil
|
218
|
-
if (prev_cast_type, prev_default = attributes_to_define_after_schema_loads[name])
|
219
|
-
default = prev_default if default == NO_DEFAULT_PROVIDED
|
220
|
-
else
|
221
|
-
prev_cast_type = -> subtype { subtype }
|
222
|
-
end
|
223
|
-
|
224
|
-
cast_type = if block_given?
|
225
|
-
-> subtype { yield Proc === prev_cast_type ? prev_cast_type[subtype] : prev_cast_type }
|
226
|
-
else
|
227
|
-
prev_cast_type
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
self.attributes_to_define_after_schema_loads =
|
232
|
-
attributes_to_define_after_schema_loads.merge(name => [cast_type, default])
|
233
|
-
end
|
211
|
+
#
|
212
|
+
#--
|
213
|
+
# Implemented by ActiveModel::AttributeRegistration#attribute.
|
234
214
|
|
235
|
-
# This
|
236
|
-
#
|
237
|
-
# waiting for the schema to load. Automatic schema detection and
|
238
|
-
# ClassMethods#attribute both call this under the hood. While this method
|
215
|
+
# This API only accepts type objects, and will do its work immediately instead of
|
216
|
+
# waiting for the schema to load. While this method
|
239
217
|
# is provided so it can be used by plugin authors, application code
|
240
218
|
# should probably use ClassMethods#attribute.
|
241
219
|
#
|
@@ -260,14 +238,38 @@ module ActiveRecord
|
|
260
238
|
define_default_attribute(name, default, cast_type, from_user: user_provided_default)
|
261
239
|
end
|
262
240
|
|
263
|
-
def
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
241
|
+
def _default_attributes # :nodoc:
|
242
|
+
@default_attributes ||= begin
|
243
|
+
attributes_hash = with_connection do |connection|
|
244
|
+
columns_hash.transform_values do |column|
|
245
|
+
ActiveModel::Attribute.from_database(column.name, column.default, type_for_column(connection, column))
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
attribute_set = ActiveModel::AttributeSet.new(attributes_hash)
|
250
|
+
apply_pending_attribute_modifications(attribute_set)
|
251
|
+
attribute_set
|
268
252
|
end
|
269
253
|
end
|
270
254
|
|
255
|
+
##
|
256
|
+
# :method: type_for_attribute
|
257
|
+
# :call-seq: type_for_attribute(attribute_name, &block)
|
258
|
+
#
|
259
|
+
# See ActiveModel::Attributes::ClassMethods#type_for_attribute.
|
260
|
+
#
|
261
|
+
# This method will access the database and load the model's schema if
|
262
|
+
# necessary.
|
263
|
+
#--
|
264
|
+
# Implemented by ActiveModel::AttributeRegistration::ClassMethods#type_for_attribute.
|
265
|
+
|
266
|
+
##
|
267
|
+
protected
|
268
|
+
def reload_schema_from_cache(*)
|
269
|
+
reset_default_attributes!
|
270
|
+
super
|
271
|
+
end
|
272
|
+
|
271
273
|
private
|
272
274
|
NO_DEFAULT_PROVIDED = Object.new # :nodoc:
|
273
275
|
private_constant :NO_DEFAULT_PROVIDED
|
@@ -287,6 +289,18 @@ module ActiveRecord
|
|
287
289
|
end
|
288
290
|
_default_attributes[name] = default_attribute
|
289
291
|
end
|
292
|
+
|
293
|
+
def reset_default_attributes
|
294
|
+
reload_schema_from_cache
|
295
|
+
end
|
296
|
+
|
297
|
+
def resolve_type_name(name, **options)
|
298
|
+
Type.lookup(name, **options, adapter: Type.adapter_name_from(self))
|
299
|
+
end
|
300
|
+
|
301
|
+
def type_for_column(connection, column)
|
302
|
+
hook_attribute_type(column.name, super)
|
303
|
+
end
|
290
304
|
end
|
291
305
|
end
|
292
306
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_record/associations/nested_error"
|
4
|
+
|
3
5
|
module ActiveRecord
|
4
6
|
# = Active Record Autosave Association
|
5
7
|
#
|
@@ -26,7 +28,7 @@ module ActiveRecord
|
|
26
28
|
#
|
27
29
|
# Child records are validated unless <tt>:validate</tt> is +false+.
|
28
30
|
#
|
29
|
-
# == Callbacks
|
31
|
+
# == \Callbacks
|
30
32
|
#
|
31
33
|
# Association with autosave option defines several callbacks on your
|
32
34
|
# model (around_save, before_save, after_create, after_update). Please note that
|
@@ -273,6 +275,11 @@ module ActiveRecord
|
|
273
275
|
end
|
274
276
|
|
275
277
|
private
|
278
|
+
def init_internals
|
279
|
+
super
|
280
|
+
@_already_called = nil
|
281
|
+
end
|
282
|
+
|
276
283
|
# Returns the record for an association collection that should be validated
|
277
284
|
# or saved. If +autosave+ is +false+ only new records will be returned,
|
278
285
|
# unless the parent is/was a new record itself.
|
@@ -310,7 +317,7 @@ module ActiveRecord
|
|
310
317
|
def validate_single_association(reflection)
|
311
318
|
association = association_instance_get(reflection.name)
|
312
319
|
record = association && association.reader
|
313
|
-
association_valid?(
|
320
|
+
association_valid?(association, record) if record && (record.changed_for_autosave? || custom_validation_context?)
|
314
321
|
end
|
315
322
|
|
316
323
|
# Validate the associated records if <tt>:validate</tt> or
|
@@ -319,7 +326,7 @@ module ActiveRecord
|
|
319
326
|
def validate_collection_association(reflection)
|
320
327
|
if association = association_instance_get(reflection.name)
|
321
328
|
if records = associated_records_to_validate_or_save(association, new_record?, reflection.options[:autosave])
|
322
|
-
records.
|
329
|
+
records.each { |record| association_valid?(association, record) }
|
323
330
|
end
|
324
331
|
end
|
325
332
|
end
|
@@ -327,40 +334,25 @@ module ActiveRecord
|
|
327
334
|
# Returns whether or not the association is valid and applies any errors to
|
328
335
|
# the parent, <tt>self</tt>, if it wasn't. Skips any <tt>:autosave</tt>
|
329
336
|
# enabled records if they're marked_for_destruction? or destroyed.
|
330
|
-
def association_valid?(
|
331
|
-
return true if record.destroyed? || (
|
337
|
+
def association_valid?(association, record)
|
338
|
+
return true if record.destroyed? || (association.options[:autosave] && record.marked_for_destruction?)
|
332
339
|
|
333
340
|
context = validation_context if custom_validation_context?
|
334
341
|
|
335
342
|
unless valid = record.valid?(context)
|
336
|
-
if
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
errors.each { |error|
|
343
|
-
self.errors.import(
|
344
|
-
error,
|
345
|
-
attribute: attribute
|
346
|
-
)
|
347
|
-
}
|
343
|
+
if association.options[:autosave]
|
344
|
+
record.errors.each { |error|
|
345
|
+
self.errors.objects.append(
|
346
|
+
Associations::NestedError.new(association, error)
|
347
|
+
)
|
348
348
|
}
|
349
349
|
else
|
350
|
-
errors.add(reflection.name)
|
350
|
+
errors.add(association.reflection.name)
|
351
351
|
end
|
352
352
|
end
|
353
353
|
valid
|
354
354
|
end
|
355
355
|
|
356
|
-
def normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)
|
357
|
-
if indexed_attribute
|
358
|
-
"#{reflection.name}[#{index}].#{attribute}"
|
359
|
-
else
|
360
|
-
"#{reflection.name}.#{attribute}"
|
361
|
-
end
|
362
|
-
end
|
363
|
-
|
364
356
|
# Is used as an around_save callback to check while saving a collection
|
365
357
|
# association whether or not the parent was a new record before saving.
|
366
358
|
def around_save_collection_association
|
@@ -436,7 +428,9 @@ module ActiveRecord
|
|
436
428
|
# ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
|
437
429
|
def save_has_one_association(reflection)
|
438
430
|
association = association_instance_get(reflection.name)
|
439
|
-
|
431
|
+
return unless association && association.loaded?
|
432
|
+
|
433
|
+
record = association.load_target
|
440
434
|
|
441
435
|
if record && !record.destroyed?
|
442
436
|
autosave = reflection.options[:autosave]
|
@@ -444,11 +438,18 @@ module ActiveRecord
|
|
444
438
|
if autosave && record.marked_for_destruction?
|
445
439
|
record.destroy
|
446
440
|
elsif autosave != false
|
447
|
-
|
441
|
+
primary_key = Array(compute_primary_key(reflection, self)).map(&:to_s)
|
442
|
+
primary_key_value = primary_key.map { |key| _read_attribute(key) }
|
448
443
|
|
449
|
-
if (autosave && record.changed_for_autosave?) || _record_changed?(reflection, record,
|
444
|
+
if (autosave && record.changed_for_autosave?) || _record_changed?(reflection, record, primary_key_value)
|
450
445
|
unless reflection.through_reflection
|
451
|
-
|
446
|
+
foreign_key = Array(reflection.foreign_key)
|
447
|
+
primary_key_foreign_key_pairs = primary_key.zip(foreign_key)
|
448
|
+
|
449
|
+
primary_key_foreign_key_pairs.each do |primary_key, foreign_key|
|
450
|
+
association_id = _read_attribute(primary_key)
|
451
|
+
record[foreign_key] = association_id unless record[foreign_key] == association_id
|
452
|
+
end
|
452
453
|
association.set_inverse_instance(record)
|
453
454
|
end
|
454
455
|
|
@@ -463,14 +464,26 @@ module ActiveRecord
|
|
463
464
|
# If the record is new or it has changed, returns true.
|
464
465
|
def _record_changed?(reflection, record, key)
|
465
466
|
record.new_record? ||
|
466
|
-
association_foreign_key_changed?(reflection, record, key) ||
|
467
|
+
(association_foreign_key_changed?(reflection, record, key) ||
|
468
|
+
inverse_polymorphic_association_changed?(reflection, record)) ||
|
467
469
|
record.will_save_change_to_attribute?(reflection.foreign_key)
|
468
470
|
end
|
469
471
|
|
470
472
|
def association_foreign_key_changed?(reflection, record, key)
|
471
473
|
return false if reflection.through_reflection?
|
472
474
|
|
473
|
-
|
475
|
+
foreign_key = Array(reflection.foreign_key)
|
476
|
+
return false unless foreign_key.all? { |key| record._has_attribute?(key) }
|
477
|
+
|
478
|
+
foreign_key.map { |key| record._read_attribute(key) } != Array(key)
|
479
|
+
end
|
480
|
+
|
481
|
+
def inverse_polymorphic_association_changed?(reflection, record)
|
482
|
+
return false unless reflection.inverse_of&.polymorphic?
|
483
|
+
|
484
|
+
class_name = record._read_attribute(reflection.inverse_of.foreign_type)
|
485
|
+
|
486
|
+
reflection.active_record != record.class.polymorphic_class_for(class_name)
|
474
487
|
end
|
475
488
|
|
476
489
|
# Saves the associated record if it's new or <tt>:autosave</tt> is enabled.
|
@@ -485,14 +498,21 @@ module ActiveRecord
|
|
485
498
|
autosave = reflection.options[:autosave]
|
486
499
|
|
487
500
|
if autosave && record.marked_for_destruction?
|
488
|
-
|
501
|
+
foreign_key = Array(reflection.foreign_key)
|
502
|
+
foreign_key.each { |key| self[key] = nil }
|
489
503
|
record.destroy
|
490
504
|
elsif autosave != false
|
491
505
|
saved = record.save(validate: !autosave) if record.new_record? || (autosave && record.changed_for_autosave?)
|
492
506
|
|
493
507
|
if association.updated?
|
494
|
-
|
495
|
-
|
508
|
+
primary_key = Array(compute_primary_key(reflection, record)).map(&:to_s)
|
509
|
+
foreign_key = Array(reflection.foreign_key)
|
510
|
+
|
511
|
+
primary_key_foreign_key_pairs = primary_key.zip(foreign_key)
|
512
|
+
primary_key_foreign_key_pairs.each do |primary_key, foreign_key|
|
513
|
+
association_id = record._read_attribute(primary_key)
|
514
|
+
self[foreign_key] = association_id unless self[foreign_key] == association_id
|
515
|
+
end
|
496
516
|
association.loaded!
|
497
517
|
end
|
498
518
|
|
@@ -501,8 +521,20 @@ module ActiveRecord
|
|
501
521
|
end
|
502
522
|
end
|
503
523
|
|
504
|
-
def
|
505
|
-
|
524
|
+
def compute_primary_key(reflection, record)
|
525
|
+
if primary_key_options = reflection.options[:primary_key]
|
526
|
+
primary_key_options
|
527
|
+
elsif reflection.options[:query_constraints] && (query_constraints = record.class.query_constraints_list)
|
528
|
+
query_constraints
|
529
|
+
elsif record.class.has_query_constraints? && !reflection.options[:foreign_key]
|
530
|
+
record.class.query_constraints_list
|
531
|
+
elsif record.class.composite_primary_key?
|
532
|
+
# If record has composite primary key of shape [:<tenant_key>, :id], infer primary_key as :id
|
533
|
+
primary_key = record.class.primary_key
|
534
|
+
primary_key.include?("id") ? "id" : primary_key
|
535
|
+
else
|
536
|
+
record.class.primary_key
|
537
|
+
end
|
506
538
|
end
|
507
539
|
|
508
540
|
def _ensure_no_duplicate_errors
|
data/lib/active_record/base.rb
CHANGED
@@ -233,7 +233,7 @@ module ActiveRecord # :nodoc:
|
|
233
233
|
#
|
234
234
|
# Connections are usually created through
|
235
235
|
# {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] and retrieved
|
236
|
-
# by ActiveRecord::Base.
|
236
|
+
# by ActiveRecord::Base.lease_connection. All classes inheriting from ActiveRecord::Base will use this
|
237
237
|
# connection. But you can also set a class-specific connection. For example, if Course is an
|
238
238
|
# ActiveRecord::Base, but resides in a different database, you can just say <tt>Course.establish_connection</tt>
|
239
239
|
# and Course and all of its subclasses will use this connection instead.
|
@@ -280,7 +280,7 @@ module ActiveRecord # :nodoc:
|
|
280
280
|
# So it's possible to assign a logger to the class through <tt>Base.logger=</tt> which will then be used by all
|
281
281
|
# instances in the current object space.
|
282
282
|
class Base
|
283
|
-
|
283
|
+
include ActiveModel::API
|
284
284
|
|
285
285
|
extend ActiveSupport::Benchmarkable
|
286
286
|
extend ActiveSupport::DescendantsTracker
|
@@ -304,18 +304,18 @@ module ActiveRecord # :nodoc:
|
|
304
304
|
include Scoping
|
305
305
|
include Sanitization
|
306
306
|
include AttributeAssignment
|
307
|
-
include ActiveModel::Conversion
|
308
307
|
include Integration
|
309
308
|
include Validations
|
310
309
|
include CounterCache
|
311
310
|
include Attributes
|
312
311
|
include Locking::Optimistic
|
313
312
|
include Locking::Pessimistic
|
313
|
+
include Encryption::EncryptableRecord
|
314
314
|
include AttributeMethods
|
315
315
|
include Callbacks
|
316
316
|
include Timestamp
|
317
317
|
include Associations
|
318
|
-
include
|
318
|
+
include SecurePassword
|
319
319
|
include AutosaveAssociation
|
320
320
|
include NestedAttributes
|
321
321
|
include Transactions
|
@@ -325,9 +325,13 @@ module ActiveRecord # :nodoc:
|
|
325
325
|
include Serialization
|
326
326
|
include Store
|
327
327
|
include SecureToken
|
328
|
+
include TokenFor
|
328
329
|
include SignedId
|
329
330
|
include Suppressor
|
330
|
-
include
|
331
|
+
include Normalization
|
332
|
+
include Marshalling::Methods
|
333
|
+
|
334
|
+
self.param_delimiter = "_"
|
331
335
|
end
|
332
336
|
|
333
337
|
ActiveSupport.run_load_hooks(:active_record, Base)
|
@@ -84,7 +84,7 @@ module ActiveRecord
|
|
84
84
|
# == Types of callbacks
|
85
85
|
#
|
86
86
|
# There are three types of callbacks accepted by the callback macros: method references (symbol), callback objects,
|
87
|
-
# inline methods (using a proc). Method references and callback objects are the recommended approaches,
|
87
|
+
# inline methods (using a proc). \Method references and callback objects are the recommended approaches,
|
88
88
|
# inline methods using a proc are sometimes appropriate (such as for creating mix-ins).
|
89
89
|
#
|
90
90
|
# The method reference callbacks work by specifying a protected or private method available in the object, like this:
|
@@ -173,7 +173,7 @@ module ActiveRecord
|
|
173
173
|
#
|
174
174
|
# If a <tt>before_*</tt> callback throws +:abort+, all the later callbacks and
|
175
175
|
# the associated action are cancelled.
|
176
|
-
# Callbacks are generally run in the order they are defined, with the exception of callbacks defined as
|
176
|
+
# \Callbacks are generally run in the order they are defined, with the exception of callbacks defined as
|
177
177
|
# methods on the model, which are called last.
|
178
178
|
#
|
179
179
|
# == Ordering callbacks
|
@@ -234,30 +234,16 @@ module ActiveRecord
|
|
234
234
|
# end
|
235
235
|
#
|
236
236
|
# In this case the +log_children+ is executed before +do_something_else+.
|
237
|
-
#
|
237
|
+
# This applies to all non-transactional callbacks, and to +before_commit+.
|
238
238
|
#
|
239
|
-
#
|
240
|
-
#
|
239
|
+
# For transactional +after_+ callbacks (+after_commit+, +after_rollback+, etc), the order
|
240
|
+
# can be set via configuration.
|
241
241
|
#
|
242
|
-
#
|
243
|
-
#
|
244
|
-
# class Topic < ActiveRecord::Base
|
245
|
-
# has_many :children
|
246
|
-
#
|
247
|
-
# after_commit :log_children
|
248
|
-
# after_commit :do_something_else
|
249
|
-
#
|
250
|
-
# private
|
251
|
-
# def log_children
|
252
|
-
# # Child processing
|
253
|
-
# end
|
254
|
-
#
|
255
|
-
# def do_something_else
|
256
|
-
# # Something else
|
257
|
-
# end
|
258
|
-
# end
|
242
|
+
# config.active_record.run_after_transaction_callbacks_in_order_defined = false
|
259
243
|
#
|
260
|
-
#
|
244
|
+
# When set to +true+ (the default from \Rails 7.1), callbacks are executed in the order they
|
245
|
+
# are defined, just like the example above. When set to +false+, the order is reversed, so
|
246
|
+
# +do_something_else+ is executed before +log_children+.
|
261
247
|
#
|
262
248
|
# == \Transactions
|
263
249
|
#
|
@@ -432,7 +418,7 @@ module ActiveRecord
|
|
432
418
|
|
433
419
|
def destroy # :nodoc:
|
434
420
|
@_destroy_callback_already_called ||= false
|
435
|
-
return if @_destroy_callback_already_called
|
421
|
+
return true if @_destroy_callback_already_called
|
436
422
|
@_destroy_callback_already_called = true
|
437
423
|
_run_destroy_callbacks { super }
|
438
424
|
rescue RecordNotDestroyed => e
|
@@ -460,7 +446,7 @@ module ActiveRecord
|
|
460
446
|
end
|
461
447
|
|
462
448
|
def _update_record
|
463
|
-
_run_update_callbacks { super }
|
449
|
+
_run_update_callbacks { record_update_timestamps { super } }
|
464
450
|
end
|
465
451
|
end
|
466
452
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Coders # :nodoc:
|
5
|
+
class ColumnSerializer # :nodoc:
|
6
|
+
attr_reader :object_class
|
7
|
+
attr_reader :coder
|
8
|
+
|
9
|
+
def initialize(attr_name, coder, object_class = Object)
|
10
|
+
@attr_name = attr_name
|
11
|
+
@object_class = object_class
|
12
|
+
@coder = coder
|
13
|
+
check_arity_of_constructor
|
14
|
+
end
|
15
|
+
|
16
|
+
def init_with(coder) # :nodoc:
|
17
|
+
@attr_name = coder["attr_name"]
|
18
|
+
@object_class = coder["object_class"]
|
19
|
+
@coder = coder["coder"]
|
20
|
+
end
|
21
|
+
|
22
|
+
def dump(object)
|
23
|
+
return if object.nil?
|
24
|
+
|
25
|
+
assert_valid_value(object, action: "dump")
|
26
|
+
coder.dump(object)
|
27
|
+
end
|
28
|
+
|
29
|
+
def load(payload)
|
30
|
+
if payload.nil?
|
31
|
+
if @object_class != ::Object
|
32
|
+
return @object_class.new
|
33
|
+
end
|
34
|
+
return nil
|
35
|
+
end
|
36
|
+
|
37
|
+
object = coder.load(payload)
|
38
|
+
|
39
|
+
assert_valid_value(object, action: "load")
|
40
|
+
object ||= object_class.new if object_class != Object
|
41
|
+
|
42
|
+
object
|
43
|
+
end
|
44
|
+
|
45
|
+
# Public because it's called by Type::Serialized
|
46
|
+
def assert_valid_value(object, action:)
|
47
|
+
unless object.nil? || object_class === object
|
48
|
+
raise SerializationTypeMismatch,
|
49
|
+
"can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{object.class}. -- #{object.inspect}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def check_arity_of_constructor
|
55
|
+
load(nil)
|
56
|
+
rescue ArgumentError
|
57
|
+
raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|