activerecord 6.1.6 → 7.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1627 -983
- data/MIT-LICENSE +1 -1
- data/README.rdoc +18 -18
- data/lib/active_record/aggregations.rb +17 -14
- data/lib/active_record/association_relation.rb +1 -11
- data/lib/active_record/associations/association.rb +50 -19
- data/lib/active_record/associations/association_scope.rb +17 -12
- data/lib/active_record/associations/belongs_to_association.rb +28 -9
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
- data/lib/active_record/associations/builder/association.rb +11 -5
- data/lib/active_record/associations/builder/belongs_to.rb +40 -14
- data/lib/active_record/associations/builder/collection_association.rb +10 -3
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
- data/lib/active_record/associations/builder/has_many.rb +3 -2
- data/lib/active_record/associations/builder/has_one.rb +2 -1
- data/lib/active_record/associations/builder/singular_association.rb +6 -2
- data/lib/active_record/associations/collection_association.rb +35 -31
- data/lib/active_record/associations/collection_proxy.rb +30 -15
- data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
- data/lib/active_record/associations/foreign_association.rb +10 -3
- data/lib/active_record/associations/has_many_association.rb +28 -18
- data/lib/active_record/associations/has_many_through_association.rb +12 -7
- data/lib/active_record/associations/has_one_association.rb +20 -10
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/join_dependency.rb +26 -16
- data/lib/active_record/associations/preloader/association.rb +207 -52
- data/lib/active_record/associations/preloader/batch.rb +48 -0
- data/lib/active_record/associations/preloader/branch.rb +147 -0
- data/lib/active_record/associations/preloader/through_association.rb +50 -14
- data/lib/active_record/associations/preloader.rb +50 -121
- data/lib/active_record/associations/singular_association.rb +9 -3
- data/lib/active_record/associations/through_association.rb +25 -14
- data/lib/active_record/associations.rb +439 -305
- data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
- data/lib/active_record/attribute_assignment.rb +1 -3
- data/lib/active_record/attribute_methods/before_type_cast.rb +24 -2
- data/lib/active_record/attribute_methods/dirty.rb +73 -22
- data/lib/active_record/attribute_methods/primary_key.rb +78 -26
- data/lib/active_record/attribute_methods/query.rb +31 -19
- data/lib/active_record/attribute_methods/read.rb +25 -10
- data/lib/active_record/attribute_methods/serialization.rb +194 -37
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +4 -3
- data/lib/active_record/attribute_methods/write.rb +10 -13
- data/lib/active_record/attribute_methods.rb +121 -40
- data/lib/active_record/attributes.rb +27 -38
- data/lib/active_record/autosave_association.rb +61 -30
- data/lib/active_record/base.rb +25 -2
- data/lib/active_record/callbacks.rb +18 -34
- 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 -34
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +367 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +211 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +78 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +96 -590
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -17
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +172 -50
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +77 -27
- data/lib/active_record/connection_adapters/abstract/quoting.rb +87 -73
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +21 -20
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +186 -31
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -1
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +360 -138
- data/lib/active_record/connection_adapters/abstract/transaction.rb +281 -59
- data/lib/active_record/connection_adapters/abstract_adapter.rb +631 -149
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +285 -156
- data/lib/active_record/connection_adapters/column.rb +13 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +25 -134
- data/lib/active_record/connection_adapters/mysql/quoting.rb +56 -25
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +10 -1
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +8 -2
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +38 -14
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +104 -53
- data/lib/active_record/connection_adapters/pool_config.rb +20 -11
- data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +89 -52
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +12 -3
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
- data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +89 -56
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +92 -2
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +153 -3
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +78 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +394 -74
- data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +509 -247
- data/lib/active_record/connection_adapters/schema_cache.rb +319 -90
- data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +72 -53
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +37 -21
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +43 -22
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +294 -102
- 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 +254 -0
- data/lib/active_record/connection_adapters.rb +9 -6
- data/lib/active_record/connection_handling.rb +107 -136
- data/lib/active_record/core.rb +202 -223
- data/lib/active_record/counter_cache.rb +46 -25
- data/lib/active_record/database_configurations/connection_url_resolver.rb +2 -1
- data/lib/active_record/database_configurations/database_config.rb +21 -12
- data/lib/active_record/database_configurations/hash_config.rb +84 -16
- data/lib/active_record/database_configurations/url_config.rb +18 -12
- data/lib/active_record/database_configurations.rb +95 -59
- data/lib/active_record/delegated_type.rb +61 -15
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +3 -1
- data/lib/active_record/disable_joins_association_relation.rb +39 -0
- data/lib/active_record/dynamic_matchers.rb +1 -1
- data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +101 -0
- data/lib/active_record/encryption/cipher.rb +53 -0
- data/lib/active_record/encryption/config.rb +68 -0
- data/lib/active_record/encryption/configurable.rb +60 -0
- data/lib/active_record/encryption/context.rb +42 -0
- data/lib/active_record/encryption/contexts.rb +76 -0
- data/lib/active_record/encryption/derived_secret_key_provider.rb +18 -0
- data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
- data/lib/active_record/encryption/encryptable_record.rb +224 -0
- data/lib/active_record/encryption/encrypted_attribute_type.rb +151 -0
- data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
- data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
- data/lib/active_record/encryption/encryptor.rb +155 -0
- data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
- data/lib/active_record/encryption/errors.rb +15 -0
- data/lib/active_record/encryption/extended_deterministic_queries.rb +157 -0
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
- data/lib/active_record/encryption/key.rb +28 -0
- data/lib/active_record/encryption/key_generator.rb +53 -0
- data/lib/active_record/encryption/key_provider.rb +46 -0
- data/lib/active_record/encryption/message.rb +33 -0
- data/lib/active_record/encryption/message_serializer.rb +92 -0
- data/lib/active_record/encryption/null_encryptor.rb +21 -0
- data/lib/active_record/encryption/properties.rb +76 -0
- data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
- data/lib/active_record/encryption/scheme.rb +96 -0
- data/lib/active_record/encryption.rb +56 -0
- data/lib/active_record/enum.rb +154 -63
- data/lib/active_record/errors.rb +171 -15
- data/lib/active_record/explain.rb +23 -3
- data/lib/active_record/explain_registry.rb +11 -6
- data/lib/active_record/explain_subscriber.rb +1 -1
- data/lib/active_record/fixture_set/file.rb +15 -1
- 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 +70 -14
- data/lib/active_record/fixture_set/table_rows.rb +4 -4
- data/lib/active_record/fixtures.rb +131 -86
- data/lib/active_record/future_result.rb +164 -0
- data/lib/active_record/gem_version.rb +3 -3
- data/lib/active_record/inheritance.rb +81 -29
- data/lib/active_record/insert_all.rb +135 -22
- data/lib/active_record/integration.rb +11 -10
- data/lib/active_record/internal_metadata.rb +119 -33
- data/lib/active_record/legacy_yaml_adapter.rb +2 -39
- data/lib/active_record/locking/optimistic.rb +36 -21
- data/lib/active_record/locking/pessimistic.rb +15 -6
- data/lib/active_record/log_subscriber.rb +52 -19
- data/lib/active_record/marshalling.rb +56 -0
- data/lib/active_record/message_pack.rb +124 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +10 -10
- data/lib/active_record/middleware/database_selector.rb +23 -13
- data/lib/active_record/middleware/shard_selector.rb +62 -0
- data/lib/active_record/migration/command_recorder.rb +112 -14
- data/lib/active_record/migration/compatibility.rb +221 -48
- data/lib/active_record/migration/default_strategy.rb +23 -0
- data/lib/active_record/migration/execution_strategy.rb +19 -0
- data/lib/active_record/migration/join_table.rb +1 -1
- data/lib/active_record/migration/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +358 -171
- data/lib/active_record/model_schema.rb +120 -101
- data/lib/active_record/nested_attributes.rb +37 -18
- data/lib/active_record/no_touching.rb +3 -3
- data/lib/active_record/normalization.rb +167 -0
- data/lib/active_record/persistence.rb +405 -85
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +3 -21
- data/lib/active_record/query_logs.rb +174 -0
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +29 -6
- data/lib/active_record/railtie.rb +219 -43
- data/lib/active_record/railties/controller_runtime.rb +13 -9
- data/lib/active_record/railties/databases.rake +188 -252
- data/lib/active_record/railties/job_runtime.rb +23 -0
- data/lib/active_record/readonly_attributes.rb +41 -3
- data/lib/active_record/reflection.rb +241 -80
- data/lib/active_record/relation/batches/batch_enumerator.rb +23 -7
- data/lib/active_record/relation/batches.rb +192 -63
- data/lib/active_record/relation/calculations.rb +219 -90
- data/lib/active_record/relation/delegation.rb +27 -13
- data/lib/active_record/relation/finder_methods.rb +108 -51
- data/lib/active_record/relation/merger.rb +22 -13
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
- data/lib/active_record/relation/predicate_builder.rb +27 -20
- data/lib/active_record/relation/query_attribute.rb +30 -12
- data/lib/active_record/relation/query_methods.rb +654 -127
- data/lib/active_record/relation/record_fetch_warning.rb +7 -9
- data/lib/active_record/relation/spawn_methods.rb +20 -3
- data/lib/active_record/relation/where_clause.rb +10 -19
- data/lib/active_record/relation.rb +262 -120
- data/lib/active_record/result.rb +37 -11
- data/lib/active_record/runtime_registry.rb +18 -13
- data/lib/active_record/sanitization.rb +65 -20
- data/lib/active_record/schema.rb +36 -22
- data/lib/active_record/schema_dumper.rb +73 -24
- data/lib/active_record/schema_migration.rb +68 -33
- data/lib/active_record/scoping/default.rb +72 -15
- data/lib/active_record/scoping/named.rb +5 -13
- data/lib/active_record/scoping.rb +65 -34
- data/lib/active_record/secure_password.rb +60 -0
- data/lib/active_record/secure_token.rb +21 -3
- data/lib/active_record/serialization.rb +6 -1
- data/lib/active_record/signed_id.rb +10 -8
- data/lib/active_record/store.rb +16 -11
- data/lib/active_record/suppressor.rb +13 -15
- data/lib/active_record/table_metadata.rb +16 -3
- data/lib/active_record/tasks/database_tasks.rb +225 -136
- data/lib/active_record/tasks/mysql_database_tasks.rb +16 -7
- data/lib/active_record/tasks/postgresql_database_tasks.rb +35 -26
- data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
- data/lib/active_record/test_databases.rb +1 -1
- data/lib/active_record/test_fixtures.rb +123 -99
- data/lib/active_record/timestamp.rb +29 -18
- data/lib/active_record/token_for.rb +113 -0
- data/lib/active_record/touch_later.rb +11 -6
- data/lib/active_record/transactions.rb +48 -27
- data/lib/active_record/translation.rb +3 -3
- data/lib/active_record/type/adapter_specific_registry.rb +32 -14
- data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
- data/lib/active_record/type/internal/timezone.rb +7 -2
- data/lib/active_record/type/serialized.rb +9 -5
- data/lib/active_record/type/time.rb +4 -0
- data/lib/active_record/type/type_map.rb +17 -20
- data/lib/active_record/type.rb +1 -2
- data/lib/active_record/validations/absence.rb +1 -1
- data/lib/active_record/validations/associated.rb +4 -4
- 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 +51 -6
- data/lib/active_record/validations.rb +8 -4
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +335 -32
- data/lib/arel/attributes/attribute.rb +0 -8
- data/lib/arel/crud.rb +28 -22
- data/lib/arel/delete_manager.rb +18 -4
- data/lib/arel/errors.rb +10 -0
- data/lib/arel/factory_methods.rb +4 -0
- data/lib/arel/filter_predications.rb +9 -0
- data/lib/arel/insert_manager.rb +2 -3
- data/lib/arel/nodes/and.rb +4 -0
- data/lib/arel/nodes/binary.rb +6 -1
- data/lib/arel/nodes/bound_sql_literal.rb +61 -0
- data/lib/arel/nodes/casted.rb +1 -1
- data/lib/arel/nodes/cte.rb +36 -0
- data/lib/arel/nodes/delete_statement.rb +12 -13
- data/lib/arel/nodes/filter.rb +10 -0
- data/lib/arel/nodes/fragments.rb +35 -0
- data/lib/arel/nodes/function.rb +1 -0
- data/lib/arel/nodes/homogeneous_in.rb +1 -9
- data/lib/arel/nodes/insert_statement.rb +2 -2
- data/lib/arel/nodes/leading_join.rb +8 -0
- data/lib/arel/nodes/node.rb +111 -2
- data/lib/arel/nodes/select_core.rb +2 -2
- data/lib/arel/nodes/select_statement.rb +2 -2
- data/lib/arel/nodes/sql_literal.rb +6 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes/update_statement.rb +8 -3
- data/lib/arel/nodes.rb +5 -0
- data/lib/arel/predications.rb +13 -3
- data/lib/arel/select_manager.rb +10 -4
- data/lib/arel/table.rb +9 -6
- data/lib/arel/tree_manager.rb +0 -12
- data/lib/arel/update_manager.rb +18 -4
- data/lib/arel/visitors/dot.rb +80 -90
- data/lib/arel/visitors/mysql.rb +16 -3
- data/lib/arel/visitors/postgresql.rb +0 -10
- data/lib/arel/visitors/to_sql.rb +139 -19
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +18 -3
- data/lib/rails/generators/active_record/application_record/USAGE +8 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -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
- data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
- data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
- data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
- data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
- metadata +93 -13
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -67
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/object/json"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module TokenFor
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
class_attribute :token_definitions, instance_accessor: false, instance_predicate: false, default: {}
|
11
|
+
class_attribute :generated_token_verifier, instance_accessor: false, instance_predicate: false
|
12
|
+
end
|
13
|
+
|
14
|
+
TokenDefinition = Struct.new(:defining_class, :purpose, :expires_in, :block) do # :nodoc:
|
15
|
+
def full_purpose
|
16
|
+
@full_purpose ||= [defining_class.name, purpose, expires_in].join("\n")
|
17
|
+
end
|
18
|
+
|
19
|
+
def message_verifier
|
20
|
+
defining_class.generated_token_verifier
|
21
|
+
end
|
22
|
+
|
23
|
+
def payload_for(model)
|
24
|
+
block ? [model.id, model.instance_eval(&block).as_json] : [model.id]
|
25
|
+
end
|
26
|
+
|
27
|
+
def generate_token(model)
|
28
|
+
message_verifier.generate(payload_for(model), expires_in: expires_in, purpose: full_purpose)
|
29
|
+
end
|
30
|
+
|
31
|
+
def resolve_token(token)
|
32
|
+
payload = message_verifier.verified(token, purpose: full_purpose)
|
33
|
+
model = yield(payload[0]) if payload
|
34
|
+
model if model && payload_for(model) == payload
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ClassMethods
|
39
|
+
# Defines the behavior of tokens generated for a specific +purpose+.
|
40
|
+
# A token can be generated by calling TokenFor#generate_token_for on a
|
41
|
+
# record. Later, that record can be fetched by calling #find_by_token_for
|
42
|
+
# (or #find_by_token_for!) with the same purpose and token.
|
43
|
+
#
|
44
|
+
# Tokens are signed so that they are tamper-proof. Thus they can be
|
45
|
+
# exposed to outside world as, for example, password reset tokens.
|
46
|
+
#
|
47
|
+
# By default, tokens do not expire. They can be configured to expire by
|
48
|
+
# specifying a duration via the +expires_in+ option. The duration becomes
|
49
|
+
# part of the token's signature, so changing the value of +expires_in+
|
50
|
+
# will automatically invalidate previously generated tokens.
|
51
|
+
#
|
52
|
+
# A block may also be specified. When generating a token with
|
53
|
+
# TokenFor#generate_token_for, the block will be evaluated in the context
|
54
|
+
# of the record, and its return value will be embedded in the token as
|
55
|
+
# JSON. Later, when fetching the record with #find_by_token_for, the block
|
56
|
+
# will be evaluated again in the context of the fetched record. If the two
|
57
|
+
# JSON values do not match, the token will be treated as invalid. Note
|
58
|
+
# that the value returned by the block <b>should not contain sensitive
|
59
|
+
# information</b> because it will be embedded in the token as
|
60
|
+
# <b>human-readable plaintext JSON</b>.
|
61
|
+
#
|
62
|
+
# ==== Examples
|
63
|
+
#
|
64
|
+
# class User < ActiveRecord::Base
|
65
|
+
# has_secure_password
|
66
|
+
#
|
67
|
+
# generates_token_for :password_reset, expires_in: 15.minutes do
|
68
|
+
# # Last 10 characters of password salt, which changes when password is updated:
|
69
|
+
# password_salt&.last(10)
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# user = User.first
|
74
|
+
#
|
75
|
+
# token = user.generate_token_for(:password_reset)
|
76
|
+
# User.find_by_token_for(:password_reset, token) # => user
|
77
|
+
# # 16 minutes later...
|
78
|
+
# User.find_by_token_for(:password_reset, token) # => nil
|
79
|
+
#
|
80
|
+
# token = user.generate_token_for(:password_reset)
|
81
|
+
# User.find_by_token_for(:password_reset, token) # => user
|
82
|
+
# user.update!(password: "new password")
|
83
|
+
# User.find_by_token_for(:password_reset, token) # => nil
|
84
|
+
def generates_token_for(purpose, expires_in: nil, &block)
|
85
|
+
self.token_definitions = token_definitions.merge(purpose => TokenDefinition.new(self, purpose, expires_in, block))
|
86
|
+
end
|
87
|
+
|
88
|
+
# Finds a record using a given +token+ for a predefined +purpose+. Returns
|
89
|
+
# +nil+ if the token is invalid or the record was not found.
|
90
|
+
def find_by_token_for(purpose, token)
|
91
|
+
raise UnknownPrimaryKey.new(self) unless primary_key
|
92
|
+
token_definitions.fetch(purpose).resolve_token(token) { |id| find_by(primary_key => id) }
|
93
|
+
end
|
94
|
+
|
95
|
+
# Finds a record using a given +token+ for a predefined +purpose+. Raises
|
96
|
+
# ActiveSupport::MessageVerifier::InvalidSignature if the token is invalid
|
97
|
+
# (e.g. expired, bad format, etc). Raises ActiveRecord::RecordNotFound if
|
98
|
+
# the token is valid but the record was not found.
|
99
|
+
def find_by_token_for!(purpose, token)
|
100
|
+
token_definitions.fetch(purpose).resolve_token(token) { |id| find(id) } ||
|
101
|
+
(raise ActiveSupport::MessageVerifier::InvalidSignature)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Generates a token for a predefined +purpose+.
|
106
|
+
#
|
107
|
+
# Use ClassMethods#generates_token_for to define a token purpose and
|
108
|
+
# behavior.
|
109
|
+
def generate_token_for(purpose)
|
110
|
+
self.class.token_definitions.fetch(purpose).generate_token(self)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -24,9 +24,13 @@ module ActiveRecord
|
|
24
24
|
@_new_record_before_last_commit ||= false
|
25
25
|
|
26
26
|
# touch the parents as we are not calling the after_save callbacks
|
27
|
-
self.class.reflect_on_all_associations
|
27
|
+
self.class.reflect_on_all_associations.each do |r|
|
28
28
|
if touch = r.options[:touch]
|
29
|
-
|
29
|
+
if r.macro == :belongs_to
|
30
|
+
ActiveRecord::Associations::Builder::BelongsTo.touch_record(self, changes_to_save, r.foreign_key, r.name, touch)
|
31
|
+
elsif r.macro == :has_one
|
32
|
+
ActiveRecord::Associations::Builder::HasOne.touch_record(self, r.name, touch)
|
33
|
+
end
|
30
34
|
end
|
31
35
|
end
|
32
36
|
end
|
@@ -42,6 +46,11 @@ module ActiveRecord
|
|
42
46
|
end
|
43
47
|
|
44
48
|
private
|
49
|
+
def init_internals
|
50
|
+
super
|
51
|
+
@_defer_touch_attrs = nil
|
52
|
+
end
|
53
|
+
|
45
54
|
def surreptitiously_touch(attr_names)
|
46
55
|
attr_names.each do |attr_name|
|
47
56
|
_write_attribute(attr_name, @_touch_time)
|
@@ -57,9 +66,5 @@ module ActiveRecord
|
|
57
66
|
def has_defer_touch_attrs?
|
58
67
|
defined?(@_defer_touch_attrs) && @_defer_touch_attrs.present?
|
59
68
|
end
|
60
|
-
|
61
|
-
def belongs_to_touch_method
|
62
|
-
:touch_later
|
63
|
-
end
|
64
69
|
end
|
65
70
|
end
|
@@ -4,7 +4,7 @@ module ActiveRecord
|
|
4
4
|
# See ActiveRecord::Transactions::ClassMethods for documentation.
|
5
5
|
module Transactions
|
6
6
|
extend ActiveSupport::Concern
|
7
|
-
|
7
|
+
# :nodoc:
|
8
8
|
ACTIONS = [:create, :destroy, :update]
|
9
9
|
|
10
10
|
included do
|
@@ -13,7 +13,9 @@ module ActiveRecord
|
|
13
13
|
scope: [:kind, :name]
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
attr_accessor :_new_record_before_last_commit # :nodoc:
|
17
|
+
|
18
|
+
# = Active Record \Transactions
|
17
19
|
#
|
18
20
|
# \Transactions are protective blocks where SQL statements are only permanent
|
19
21
|
# if they can all succeed as one atomic action. The classic example is a
|
@@ -98,7 +100,8 @@ module ActiveRecord
|
|
98
100
|
# catch those in your application code.
|
99
101
|
#
|
100
102
|
# One exception is the ActiveRecord::Rollback exception, which will trigger
|
101
|
-
# a ROLLBACK when raised, but not be re-raised by the transaction block.
|
103
|
+
# a ROLLBACK when raised, but not be re-raised by the transaction block. Any
|
104
|
+
# other exception will be re-raised.
|
102
105
|
#
|
103
106
|
# *Warning*: one should not catch ActiveRecord::StatementInvalid exceptions
|
104
107
|
# inside a transaction block. ActiveRecord::StatementInvalid exceptions indicate that an
|
@@ -227,31 +230,31 @@ module ActiveRecord
|
|
227
230
|
# after_commit :do_bar_baz, on: [:update, :destroy]
|
228
231
|
#
|
229
232
|
def after_commit(*args, &block)
|
230
|
-
set_options_for_callbacks!(args)
|
233
|
+
set_options_for_callbacks!(args, prepend_option)
|
231
234
|
set_callback(:commit, :after, *args, &block)
|
232
235
|
end
|
233
236
|
|
234
237
|
# Shortcut for <tt>after_commit :hook, on: [ :create, :update ]</tt>.
|
235
238
|
def after_save_commit(*args, &block)
|
236
|
-
set_options_for_callbacks!(args, on: [ :create, :update ])
|
239
|
+
set_options_for_callbacks!(args, on: [ :create, :update ], **prepend_option)
|
237
240
|
set_callback(:commit, :after, *args, &block)
|
238
241
|
end
|
239
242
|
|
240
243
|
# Shortcut for <tt>after_commit :hook, on: :create</tt>.
|
241
244
|
def after_create_commit(*args, &block)
|
242
|
-
set_options_for_callbacks!(args, on: :create)
|
245
|
+
set_options_for_callbacks!(args, on: :create, **prepend_option)
|
243
246
|
set_callback(:commit, :after, *args, &block)
|
244
247
|
end
|
245
248
|
|
246
249
|
# Shortcut for <tt>after_commit :hook, on: :update</tt>.
|
247
250
|
def after_update_commit(*args, &block)
|
248
|
-
set_options_for_callbacks!(args, on: :update)
|
251
|
+
set_options_for_callbacks!(args, on: :update, **prepend_option)
|
249
252
|
set_callback(:commit, :after, *args, &block)
|
250
253
|
end
|
251
254
|
|
252
255
|
# Shortcut for <tt>after_commit :hook, on: :destroy</tt>.
|
253
256
|
def after_destroy_commit(*args, &block)
|
254
|
-
set_options_for_callbacks!(args, on: :destroy)
|
257
|
+
set_options_for_callbacks!(args, on: :destroy, **prepend_option)
|
255
258
|
set_callback(:commit, :after, *args, &block)
|
256
259
|
end
|
257
260
|
|
@@ -259,11 +262,19 @@ module ActiveRecord
|
|
259
262
|
#
|
260
263
|
# Please check the documentation of #after_commit for options.
|
261
264
|
def after_rollback(*args, &block)
|
262
|
-
set_options_for_callbacks!(args)
|
265
|
+
set_options_for_callbacks!(args, prepend_option)
|
263
266
|
set_callback(:rollback, :after, *args, &block)
|
264
267
|
end
|
265
268
|
|
266
269
|
private
|
270
|
+
def prepend_option
|
271
|
+
if ActiveRecord.run_after_transaction_callbacks_in_order_defined
|
272
|
+
{ prepend: true }
|
273
|
+
else
|
274
|
+
{}
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
267
278
|
def set_options_for_callbacks!(args, enforced_options = {})
|
268
279
|
options = args.extract_options!.merge!(enforced_options)
|
269
280
|
args << options
|
@@ -290,19 +301,19 @@ module ActiveRecord
|
|
290
301
|
self.class.transaction(**options, &block)
|
291
302
|
end
|
292
303
|
|
293
|
-
def destroy
|
304
|
+
def destroy # :nodoc:
|
294
305
|
with_transaction_returning_status { super }
|
295
306
|
end
|
296
307
|
|
297
|
-
def save(**)
|
308
|
+
def save(**) # :nodoc:
|
298
309
|
with_transaction_returning_status { super }
|
299
310
|
end
|
300
311
|
|
301
|
-
def save!(**)
|
312
|
+
def save!(**) # :nodoc:
|
302
313
|
with_transaction_returning_status { super }
|
303
314
|
end
|
304
315
|
|
305
|
-
def touch(*, **)
|
316
|
+
def touch(*, **) # :nodoc:
|
306
317
|
with_transaction_returning_status { super }
|
307
318
|
end
|
308
319
|
|
@@ -314,8 +325,8 @@ module ActiveRecord
|
|
314
325
|
#
|
315
326
|
# Ensure that it is not called if the object was never persisted (failed create),
|
316
327
|
# but call it after the commit of a destroyed object.
|
317
|
-
def committed!(should_run_callbacks: true)
|
318
|
-
|
328
|
+
def committed!(should_run_callbacks: true) # :nodoc:
|
329
|
+
@_start_transaction_state = nil
|
319
330
|
if should_run_callbacks
|
320
331
|
@_committed_already_called = true
|
321
332
|
_run_commit_callbacks
|
@@ -326,7 +337,7 @@ module ActiveRecord
|
|
326
337
|
|
327
338
|
# Call the #after_rollback callbacks. The +force_restore_state+ argument indicates if the record
|
328
339
|
# state should be rolled back to the beginning or just to the last savepoint.
|
329
|
-
def rolledback!(force_restore_state: false, should_run_callbacks: true)
|
340
|
+
def rolledback!(force_restore_state: false, should_run_callbacks: true) # :nodoc:
|
330
341
|
if should_run_callbacks
|
331
342
|
_run_rollback_callbacks
|
332
343
|
end
|
@@ -336,9 +347,9 @@ module ActiveRecord
|
|
336
347
|
@_trigger_update_callback = @_trigger_destroy_callback = false if force_restore_state
|
337
348
|
end
|
338
349
|
|
339
|
-
# Executes
|
340
|
-
# status flag. If the status is true the transaction is committed,
|
341
|
-
# a ROLLBACK is issued. In any case the status flag is returned.
|
350
|
+
# Executes a block within a transaction and captures its return value as a
|
351
|
+
# status flag. If the status is true, the transaction is committed,
|
352
|
+
# otherwise a ROLLBACK is issued. In any case, the status flag is returned.
|
342
353
|
#
|
343
354
|
# This method is available within the context of an ActiveRecord::Base
|
344
355
|
# instance.
|
@@ -365,6 +376,13 @@ module ActiveRecord
|
|
365
376
|
private
|
366
377
|
attr_reader :_committed_already_called, :_trigger_update_callback, :_trigger_destroy_callback
|
367
378
|
|
379
|
+
def init_internals
|
380
|
+
super
|
381
|
+
@_start_transaction_state = nil
|
382
|
+
@_committed_already_called = nil
|
383
|
+
@_new_record_before_last_commit = nil
|
384
|
+
end
|
385
|
+
|
368
386
|
# Save the new record state and id of a record so it can be restored later if a transaction fails.
|
369
387
|
def remember_transaction_record_state
|
370
388
|
@_start_transaction_state ||= {
|
@@ -389,12 +407,7 @@ module ActiveRecord
|
|
389
407
|
def clear_transaction_record_state
|
390
408
|
return unless @_start_transaction_state
|
391
409
|
@_start_transaction_state[:level] -= 1
|
392
|
-
|
393
|
-
end
|
394
|
-
|
395
|
-
# Force to clear the transaction record state.
|
396
|
-
def force_clear_transaction_record_state
|
397
|
-
@_start_transaction_state = nil
|
410
|
+
@_start_transaction_state = nil if @_start_transaction_state[:level] < 1
|
398
411
|
end
|
399
412
|
|
400
413
|
# Restore the new record state and id of a record that was previously saved by a call to save_record_state.
|
@@ -411,8 +424,16 @@ module ActiveRecord
|
|
411
424
|
end
|
412
425
|
@mutations_from_database = nil
|
413
426
|
@mutations_before_last_save = nil
|
414
|
-
if
|
415
|
-
|
427
|
+
if self.class.composite_primary_key?
|
428
|
+
if restore_state[:id] != @primary_key.map { |col| @attributes.fetch_value(col) }
|
429
|
+
@primary_key.zip(restore_state[:id]).each do |col, val|
|
430
|
+
@attributes.write_from_user(col, val)
|
431
|
+
end
|
432
|
+
end
|
433
|
+
else
|
434
|
+
if @attributes.fetch_value(@primary_key) != restore_state[:id]
|
435
|
+
@attributes.write_from_user(@primary_key, restore_state[:id])
|
436
|
+
end
|
416
437
|
end
|
417
438
|
freeze if restore_state[:frozen?]
|
418
439
|
end
|
@@ -5,7 +5,7 @@ module ActiveRecord
|
|
5
5
|
include ActiveModel::Translation
|
6
6
|
|
7
7
|
# Set the lookup ancestors for ActiveModel.
|
8
|
-
def lookup_ancestors
|
8
|
+
def lookup_ancestors # :nodoc:
|
9
9
|
klass = self
|
10
10
|
classes = [klass]
|
11
11
|
return classes if klass == ActiveRecord::Base
|
@@ -16,8 +16,8 @@ module ActiveRecord
|
|
16
16
|
classes
|
17
17
|
end
|
18
18
|
|
19
|
-
# Set the i18n scope to
|
20
|
-
def i18n_scope
|
19
|
+
# Set the i18n scope to override ActiveModel.
|
20
|
+
def i18n_scope # :nodoc:
|
21
21
|
:activerecord
|
22
22
|
end
|
23
23
|
end
|
@@ -1,19 +1,41 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "active_model/type/registry"
|
4
|
-
|
5
3
|
module ActiveRecord
|
6
4
|
# :stopdoc:
|
7
5
|
module Type
|
8
|
-
class AdapterSpecificRegistry
|
6
|
+
class AdapterSpecificRegistry # :nodoc:
|
7
|
+
def initialize
|
8
|
+
@registrations = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize_copy(other)
|
12
|
+
@registrations = @registrations.dup
|
13
|
+
end
|
14
|
+
|
9
15
|
def add_modifier(options, klass, **args)
|
10
16
|
registrations << DecorationRegistration.new(options, klass, **args)
|
11
17
|
end
|
12
18
|
|
13
|
-
|
14
|
-
|
15
|
-
|
19
|
+
def register(type_name, klass = nil, **options, &block)
|
20
|
+
unless block_given?
|
21
|
+
block = proc { |_, *args| klass.new(*args) }
|
22
|
+
block.ruby2_keywords if block.respond_to?(:ruby2_keywords)
|
23
|
+
end
|
24
|
+
registrations << Registration.new(type_name, block, **options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def lookup(symbol, *args, **kwargs)
|
28
|
+
registration = find_registration(symbol, *args, **kwargs)
|
29
|
+
|
30
|
+
if registration
|
31
|
+
registration.call(self, symbol, *args, **kwargs)
|
32
|
+
else
|
33
|
+
raise ArgumentError, "Unknown type #{symbol.inspect}"
|
16
34
|
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
attr_reader :registrations
|
17
39
|
|
18
40
|
def find_registration(symbol, *args, **kwargs)
|
19
41
|
registrations
|
@@ -22,7 +44,7 @@ module ActiveRecord
|
|
22
44
|
end
|
23
45
|
end
|
24
46
|
|
25
|
-
class Registration
|
47
|
+
class Registration # :nodoc:
|
26
48
|
def initialize(name, block, adapter: nil, override: nil)
|
27
49
|
@name = name
|
28
50
|
@block = block
|
@@ -31,11 +53,7 @@ module ActiveRecord
|
|
31
53
|
end
|
32
54
|
|
33
55
|
def call(_registry, *args, adapter: nil, **kwargs)
|
34
|
-
|
35
|
-
block.call(*args, **kwargs)
|
36
|
-
else
|
37
|
-
block.call(*args)
|
38
|
-
end
|
56
|
+
block.call(*args, **kwargs)
|
39
57
|
end
|
40
58
|
|
41
59
|
def matches?(type_name, *args, **kwargs)
|
@@ -89,7 +107,7 @@ module ActiveRecord
|
|
89
107
|
end
|
90
108
|
end
|
91
109
|
|
92
|
-
class DecorationRegistration < Registration
|
110
|
+
class DecorationRegistration < Registration # :nodoc:
|
93
111
|
def initialize(options, klass, adapter: nil)
|
94
112
|
@options = options
|
95
113
|
@klass = klass
|
@@ -120,7 +138,7 @@ module ActiveRecord
|
|
120
138
|
end
|
121
139
|
end
|
122
140
|
|
123
|
-
class TypeConflictError < StandardError
|
141
|
+
class TypeConflictError < StandardError # :nodoc:
|
124
142
|
end
|
125
143
|
# :startdoc:
|
126
144
|
end
|
@@ -2,7 +2,40 @@
|
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Type
|
5
|
-
class HashLookupTypeMap
|
5
|
+
class HashLookupTypeMap # :nodoc:
|
6
|
+
def initialize(parent = nil)
|
7
|
+
@mapping = {}
|
8
|
+
@cache = Concurrent::Map.new do |h, key|
|
9
|
+
h.fetch_or_store(key, Concurrent::Map.new)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def lookup(lookup_key, *args)
|
14
|
+
fetch(lookup_key, *args) { Type.default_value }
|
15
|
+
end
|
16
|
+
|
17
|
+
def fetch(lookup_key, *args, &block)
|
18
|
+
@cache[lookup_key].fetch_or_store(args) do
|
19
|
+
perform_fetch(lookup_key, *args, &block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def register_type(key, value = nil, &block)
|
24
|
+
raise ::ArgumentError unless value || block
|
25
|
+
|
26
|
+
if block
|
27
|
+
@mapping[key] = block
|
28
|
+
else
|
29
|
+
@mapping[key] = proc { value }
|
30
|
+
end
|
31
|
+
@cache.clear
|
32
|
+
end
|
33
|
+
|
34
|
+
def clear
|
35
|
+
@mapping.clear
|
36
|
+
@cache.clear
|
37
|
+
end
|
38
|
+
|
6
39
|
def alias_type(type, alias_type)
|
7
40
|
register_type(type) { |_, *args| lookup(alias_type, *args) }
|
8
41
|
end
|
@@ -4,12 +4,17 @@ module ActiveRecord
|
|
4
4
|
module Type
|
5
5
|
module Internal
|
6
6
|
module Timezone
|
7
|
+
def initialize(timezone: nil, **kwargs)
|
8
|
+
super(**kwargs)
|
9
|
+
@timezone = timezone
|
10
|
+
end
|
11
|
+
|
7
12
|
def is_utc?
|
8
|
-
|
13
|
+
default_timezone == :utc
|
9
14
|
end
|
10
15
|
|
11
16
|
def default_timezone
|
12
|
-
ActiveRecord
|
17
|
+
@timezone || ActiveRecord.default_timezone
|
13
18
|
end
|
14
19
|
end
|
15
20
|
end
|
@@ -31,7 +31,7 @@ module ActiveRecord
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def inspect
|
34
|
-
Kernel.instance_method(:inspect).
|
34
|
+
Kernel.instance_method(:inspect).bind_call(self)
|
35
35
|
end
|
36
36
|
|
37
37
|
def changed_in_place?(raw_old_value, value)
|
@@ -55,6 +55,10 @@ module ActiveRecord
|
|
55
55
|
coder.respond_to?(:object_class) && value.is_a?(coder.object_class)
|
56
56
|
end
|
57
57
|
|
58
|
+
def serialized? # :nodoc:
|
59
|
+
true
|
60
|
+
end
|
61
|
+
|
58
62
|
private
|
59
63
|
def default_value?(value)
|
60
64
|
value == coder.load(nil)
|
@@ -63,11 +67,11 @@ module ActiveRecord
|
|
63
67
|
def encoded(value)
|
64
68
|
return if default_value?(value)
|
65
69
|
payload = coder.dump(value)
|
66
|
-
if payload && binary?
|
67
|
-
|
68
|
-
|
70
|
+
if payload && @subtype.binary?
|
71
|
+
ActiveModel::Type::Binary::Data.new(payload)
|
72
|
+
else
|
73
|
+
payload
|
69
74
|
end
|
70
|
-
payload
|
71
75
|
end
|
72
76
|
end
|
73
77
|
end
|
@@ -5,55 +5,52 @@ require "concurrent/map"
|
|
5
5
|
module ActiveRecord
|
6
6
|
module Type
|
7
7
|
class TypeMap # :nodoc:
|
8
|
-
def initialize
|
8
|
+
def initialize(parent = nil)
|
9
9
|
@mapping = {}
|
10
|
-
@
|
11
|
-
|
12
|
-
end
|
10
|
+
@parent = parent
|
11
|
+
@cache = Concurrent::Map.new
|
13
12
|
end
|
14
13
|
|
15
|
-
def lookup(lookup_key
|
16
|
-
fetch(lookup_key
|
14
|
+
def lookup(lookup_key)
|
15
|
+
fetch(lookup_key) { Type.default_value }
|
17
16
|
end
|
18
17
|
|
19
|
-
def fetch(lookup_key,
|
20
|
-
@cache
|
21
|
-
perform_fetch(lookup_key,
|
18
|
+
def fetch(lookup_key, &block)
|
19
|
+
@cache.fetch_or_store(lookup_key) do
|
20
|
+
perform_fetch(lookup_key, &block)
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
25
24
|
def register_type(key, value = nil, &block)
|
26
25
|
raise ::ArgumentError unless value || block
|
27
|
-
@cache.clear
|
28
26
|
|
29
27
|
if block
|
30
28
|
@mapping[key] = block
|
31
29
|
else
|
32
30
|
@mapping[key] = proc { value }
|
33
31
|
end
|
32
|
+
@cache.clear
|
34
33
|
end
|
35
34
|
|
36
35
|
def alias_type(key, target_key)
|
37
|
-
register_type(key) do |sql_type
|
36
|
+
register_type(key) do |sql_type|
|
38
37
|
metadata = sql_type[/\(.*\)/, 0]
|
39
|
-
lookup("#{target_key}#{metadata}"
|
38
|
+
lookup("#{target_key}#{metadata}")
|
40
39
|
end
|
41
40
|
end
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
def perform_fetch(lookup_key, *args)
|
42
|
+
protected
|
43
|
+
def perform_fetch(lookup_key, &block)
|
49
44
|
matching_pair = @mapping.reverse_each.detect do |key, _|
|
50
45
|
key === lookup_key
|
51
46
|
end
|
52
47
|
|
53
48
|
if matching_pair
|
54
|
-
matching_pair.last.call(lookup_key
|
49
|
+
matching_pair.last.call(lookup_key)
|
50
|
+
elsif @parent
|
51
|
+
@parent.perform_fetch(lookup_key, &block)
|
55
52
|
else
|
56
|
-
yield lookup_key
|
53
|
+
yield lookup_key
|
57
54
|
end
|
58
55
|
end
|
59
56
|
end
|
data/lib/active_record/type.rb
CHANGED
@@ -14,7 +14,7 @@ module ActiveRecord
|
|
14
14
|
module ClassMethods
|
15
15
|
# Validates that the specified attributes are not present (as defined by
|
16
16
|
# Object#present?). If the attribute is an association, the associated object
|
17
|
-
# is considered
|
17
|
+
# is also considered not present if it is marked for destruction.
|
18
18
|
#
|
19
19
|
# See ActiveModel::Validations::HelperMethods.validates_absence_of for more information.
|
20
20
|
def validates_absence_of(*attr_names)
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Validations
|
5
|
-
class AssociatedValidator < ActiveModel::EachValidator
|
5
|
+
class AssociatedValidator < ActiveModel::EachValidator # :nodoc:
|
6
6
|
def validate_each(record, attribute, value)
|
7
7
|
if Array(value).reject { |r| valid_object?(r) }.any?
|
8
8
|
record.errors.add(attribute, :invalid, **options.merge(value: value))
|
@@ -42,14 +42,14 @@ module ActiveRecord
|
|
42
42
|
# or an array of symbols. (e.g. <tt>on: :create</tt> or
|
43
43
|
# <tt>on: :custom_validation_context</tt> or
|
44
44
|
# <tt>on: [:create, :custom_validation_context]</tt>)
|
45
|
-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
45
|
+
# * <tt>:if</tt> - Specifies a method, proc, or string to call to determine
|
46
46
|
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
47
47
|
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
|
48
48
|
# proc or string should return or evaluate to a +true+ or +false+ value.
|
49
|
-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
49
|
+
# * <tt>:unless</tt> - Specifies a method, proc, or string to call to
|
50
50
|
# determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
51
51
|
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
52
|
-
# method, proc or string should return or evaluate to a +true+ or +false+
|
52
|
+
# method, proc, or string should return or evaluate to a +true+ or +false+
|
53
53
|
# value.
|
54
54
|
def validates_associated(*attr_names)
|
55
55
|
validates_with AssociatedValidator, _merge_attributes(attr_names)
|