activerecord 6.1.7 → 7.2.0
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 +520 -1385
- data/MIT-LICENSE +1 -1
- data/README.rdoc +31 -31
- data/examples/performance.rb +2 -2
- data/lib/active_record/aggregations.rb +17 -14
- data/lib/active_record/association_relation.rb +2 -12
- data/lib/active_record/associations/alias_tracker.rb +25 -19
- data/lib/active_record/associations/association.rb +60 -21
- data/lib/active_record/associations/association_scope.rb +17 -12
- data/lib/active_record/associations/belongs_to_association.rb +37 -11
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +13 -4
- data/lib/active_record/associations/builder/association.rb +11 -5
- data/lib/active_record/associations/builder/belongs_to.rb +41 -14
- data/lib/active_record/associations/builder/collection_association.rb +10 -3
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -7
- data/lib/active_record/associations/builder/has_many.rb +4 -4
- data/lib/active_record/associations/builder/has_one.rb +4 -4
- data/lib/active_record/associations/builder/singular_association.rb +6 -2
- data/lib/active_record/associations/collection_association.rb +46 -36
- data/lib/active_record/associations/collection_proxy.rb +44 -16
- data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
- 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 +29 -19
- 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/join_association.rb +27 -25
- data/lib/active_record/associations/join_dependency.rb +23 -15
- data/lib/active_record/associations/nested_error.rb +47 -0
- data/lib/active_record/associations/preloader/association.rb +212 -53
- data/lib/active_record/associations/preloader/batch.rb +48 -0
- data/lib/active_record/associations/preloader/branch.rb +153 -0
- data/lib/active_record/associations/preloader/through_association.rb +50 -16
- data/lib/active_record/associations/preloader.rb +50 -121
- data/lib/active_record/associations/singular_association.rb +15 -3
- data/lib/active_record/associations/through_association.rb +25 -14
- data/lib/active_record/associations.rb +404 -509
- data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
- data/lib/active_record/attribute_assignment.rb +2 -14
- data/lib/active_record/attribute_methods/before_type_cast.rb +24 -2
- data/lib/active_record/attribute_methods/composite_primary_key.rb +84 -0
- data/lib/active_record/attribute_methods/dirty.rb +73 -22
- data/lib/active_record/attribute_methods/primary_key.rb +47 -27
- data/lib/active_record/attribute_methods/query.rb +31 -19
- data/lib/active_record/attribute_methods/read.rb +14 -11
- data/lib/active_record/attribute_methods/serialization.rb +174 -37
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +11 -9
- data/lib/active_record/attribute_methods/write.rb +12 -15
- data/lib/active_record/attribute_methods.rb +164 -52
- data/lib/active_record/attributes.rb +51 -49
- data/lib/active_record/autosave_association.rb +74 -57
- data/lib/active_record/base.rb +27 -5
- 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 -46
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +284 -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 +79 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +327 -612
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -17
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +199 -60
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +201 -64
- data/lib/active_record/connection_adapters/abstract/quoting.rb +119 -131
- 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 +377 -142
- data/lib/active_record/connection_adapters/abstract/transaction.rb +361 -76
- data/lib/active_record/connection_adapters/abstract_adapter.rb +624 -163
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +345 -166
- 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 +29 -130
- data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -55
- 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 +45 -14
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +152 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +107 -68
- data/lib/active_record/connection_adapters/pool_config.rb +26 -16
- 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 +114 -54
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -0
- 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/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 +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/uuid.rb +14 -4
- data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +137 -104
- 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 +173 -3
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +78 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +401 -77
- data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +518 -251
- data/lib/active_record/connection_adapters/schema_cache.rb +326 -102
- data/lib/active_record/connection_adapters/sqlite3/column.rb +62 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +78 -55
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +68 -54
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +20 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +16 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +66 -22
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +372 -130
- 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 +130 -6
- data/lib/active_record/connection_handling.rb +132 -146
- data/lib/active_record/core.rb +276 -251
- data/lib/active_record/counter_cache.rb +68 -34
- data/lib/active_record/database_configurations/connection_url_resolver.rb +9 -3
- data/lib/active_record/database_configurations/database_config.rb +34 -10
- data/lib/active_record/database_configurations/hash_config.rb +107 -31
- data/lib/active_record/database_configurations/url_config.rb +38 -13
- data/lib/active_record/database_configurations.rb +96 -60
- data/lib/active_record/delegated_type.rb +90 -20
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +4 -2
- data/lib/active_record/disable_joins_association_relation.rb +39 -0
- data/lib/active_record/dynamic_matchers.rb +3 -3
- 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 +230 -0
- data/lib/active_record/encryption/encrypted_attribute_type.rb +175 -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 +170 -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_pack_message_serializer.rb +76 -0
- data/lib/active_record/encryption/message_serializer.rb +96 -0
- data/lib/active_record/encryption/null_encryptor.rb +25 -0
- data/lib/active_record/encryption/properties.rb +76 -0
- data/lib/active_record/encryption/read_only_null_encryptor.rb +28 -0
- data/lib/active_record/encryption/scheme.rb +100 -0
- data/lib/active_record/encryption.rb +56 -0
- data/lib/active_record/enum.rb +163 -63
- data/lib/active_record/errors.rb +210 -27
- data/lib/active_record/explain.rb +21 -12
- 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 +179 -112
- data/lib/active_record/future_result.rb +178 -0
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +85 -31
- data/lib/active_record/insert_all.rb +148 -32
- data/lib/active_record/integration.rb +14 -10
- data/lib/active_record/internal_metadata.rb +123 -23
- data/lib/active_record/legacy_yaml_adapter.rb +2 -39
- data/lib/active_record/locking/optimistic.rb +43 -27
- data/lib/active_record/locking/pessimistic.rb +15 -6
- data/lib/active_record/log_subscriber.rb +41 -29
- 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 +113 -16
- data/lib/active_record/migration/compatibility.rb +235 -46
- 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/join_table.rb +1 -1
- data/lib/active_record/migration/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +374 -177
- data/lib/active_record/model_schema.rb +143 -159
- data/lib/active_record/nested_attributes.rb +48 -21
- data/lib/active_record/no_touching.rb +3 -3
- data/lib/active_record/normalization.rb +163 -0
- data/lib/active_record/persistence.rb +282 -283
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +19 -25
- data/lib/active_record/query_logs.rb +189 -0
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +44 -9
- data/lib/active_record/railtie.rb +234 -71
- data/lib/active_record/railties/controller_runtime.rb +25 -11
- data/lib/active_record/railties/databases.rake +189 -256
- 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 +325 -103
- data/lib/active_record/relation/batches/batch_enumerator.rb +38 -9
- data/lib/active_record/relation/batches.rb +198 -63
- data/lib/active_record/relation/calculations.rb +300 -111
- data/lib/active_record/relation/delegation.rb +33 -22
- data/lib/active_record/relation/finder_methods.rb +123 -52
- data/lib/active_record/relation/merger.rb +26 -19
- data/lib/active_record/relation/predicate_builder/array_handler.rb +2 -2
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +38 -4
- 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 +29 -22
- data/lib/active_record/relation/query_attribute.rb +30 -12
- data/lib/active_record/relation/query_methods.rb +842 -150
- data/lib/active_record/relation/record_fetch_warning.rb +10 -9
- data/lib/active_record/relation/spawn_methods.rb +7 -6
- data/lib/active_record/relation/where_clause.rb +15 -36
- data/lib/active_record/relation.rb +736 -145
- data/lib/active_record/result.rb +67 -54
- data/lib/active_record/runtime_registry.rb +71 -13
- data/lib/active_record/sanitization.rb +84 -34
- data/lib/active_record/schema.rb +39 -23
- data/lib/active_record/schema_dumper.rb +90 -31
- data/lib/active_record/schema_migration.rb +74 -23
- 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 +30 -9
- data/lib/active_record/statement_cache.rb +7 -7
- data/lib/active_record/store.rb +10 -10
- data/lib/active_record/suppressor.rb +13 -15
- data/lib/active_record/table_metadata.rb +7 -3
- data/lib/active_record/tasks/database_tasks.rb +277 -149
- 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 +16 -7
- data/lib/active_record/test_databases.rb +1 -1
- data/lib/active_record/test_fixtures.rb +173 -155
- data/lib/active_record/testing/query_assertions.rb +121 -0
- data/lib/active_record/timestamp.rb +32 -19
- 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 +118 -41
- data/lib/active_record/translation.rb +3 -5
- 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 -7
- 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/type_caster/connection.rb +4 -4
- data/lib/active_record/validations/absence.rb +1 -1
- data/lib/active_record/validations/associated.rb +13 -7
- 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 +64 -15
- data/lib/active_record/validations.rb +12 -5
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +444 -32
- data/lib/arel/alias_predication.rb +1 -1
- data/lib/arel/attributes/attribute.rb +0 -8
- 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/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/binary.rb +6 -7
- data/lib/arel/nodes/bound_sql_literal.rb +65 -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/{and.rb → nary.rb} +9 -2
- data/lib/arel/nodes/node.rb +115 -5
- 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 +13 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes/update_statement.rb +8 -3
- data/lib/arel/nodes.rb +7 -2
- data/lib/arel/predications.rb +14 -4
- data/lib/arel/select_manager.rb +11 -5
- data/lib/arel/table.rb +9 -6
- data/lib/arel/tree_manager.rb +8 -15
- data/lib/arel/update_manager.rb +20 -5
- data/lib/arel/visitors/dot.rb +81 -90
- data/lib/arel/visitors/mysql.rb +23 -5
- data/lib/arel/visitors/postgresql.rb +1 -22
- data/lib/arel/visitors/to_sql.rb +170 -36
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +23 -4
- 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/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
- 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 +100 -14
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -67
@@ -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
|
@@ -30,9 +30,7 @@ module ActiveRecord
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
|
-
Kernel.instance_method(:inspect).bind(self).call
|
35
|
-
end
|
33
|
+
define_method(:inspect, Kernel.instance_method(:inspect))
|
36
34
|
|
37
35
|
def changed_in_place?(raw_old_value, value)
|
38
36
|
return false if value.nil?
|
@@ -55,6 +53,10 @@ module ActiveRecord
|
|
55
53
|
coder.respond_to?(:object_class) && value.is_a?(coder.object_class)
|
56
54
|
end
|
57
55
|
|
56
|
+
def serialized? # :nodoc:
|
57
|
+
true
|
58
|
+
end
|
59
|
+
|
58
60
|
private
|
59
61
|
def default_value?(value)
|
60
62
|
value == coder.load(nil)
|
@@ -63,11 +65,11 @@ module ActiveRecord
|
|
63
65
|
def encoded(value)
|
64
66
|
return if default_value?(value)
|
65
67
|
payload = coder.dump(value)
|
66
|
-
if payload && binary?
|
67
|
-
|
68
|
-
|
68
|
+
if payload && @subtype.binary?
|
69
|
+
ActiveModel::Type::Binary::Data.new(payload)
|
70
|
+
else
|
71
|
+
payload
|
69
72
|
end
|
70
|
-
payload
|
71
73
|
end
|
72
74
|
end
|
73
75
|
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,18 +14,18 @@ module ActiveRecord
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def type_for_attribute(attr_name)
|
17
|
-
schema_cache =
|
17
|
+
schema_cache = @klass.schema_cache
|
18
18
|
|
19
19
|
if schema_cache.data_source_exists?(table_name)
|
20
20
|
column = schema_cache.columns_hash(table_name)[attr_name.to_s]
|
21
|
-
|
21
|
+
if column
|
22
|
+
type = @klass.with_connection { |connection| connection.lookup_cast_type_from_column(column) }
|
23
|
+
end
|
22
24
|
end
|
23
25
|
|
24
26
|
type || Type.default_value
|
25
27
|
end
|
26
28
|
|
27
|
-
delegate :connection, to: :@klass, private: true
|
28
|
-
|
29
29
|
private
|
30
30
|
attr_reader :table_name
|
31
31
|
end
|
@@ -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,16 +2,22 @@
|
|
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
|
+
context = record_validation_context_for_association(record)
|
8
|
+
|
9
|
+
if Array(value).reject { |association| valid_object?(association, context) }.any?
|
8
10
|
record.errors.add(attribute, :invalid, **options.merge(value: value))
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
12
14
|
private
|
13
|
-
def valid_object?(record)
|
14
|
-
(record.respond_to?(:marked_for_destruction?) && record.marked_for_destruction?) || record.valid?
|
15
|
+
def valid_object?(record, context)
|
16
|
+
(record.respond_to?(:marked_for_destruction?) && record.marked_for_destruction?) || record.valid?(context)
|
17
|
+
end
|
18
|
+
|
19
|
+
def record_validation_context_for_association(record)
|
20
|
+
record.custom_validation_context? ? record.validation_context : nil
|
15
21
|
end
|
16
22
|
end
|
17
23
|
|
@@ -42,14 +48,14 @@ module ActiveRecord
|
|
42
48
|
# or an array of symbols. (e.g. <tt>on: :create</tt> or
|
43
49
|
# <tt>on: :custom_validation_context</tt> or
|
44
50
|
# <tt>on: [:create, :custom_validation_context]</tt>)
|
45
|
-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
51
|
+
# * <tt>:if</tt> - Specifies a method, proc, or string to call to determine
|
46
52
|
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
47
53
|
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
|
48
54
|
# 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
|
55
|
+
# * <tt>:unless</tt> - Specifies a method, proc, or string to call to
|
50
56
|
# determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
51
57
|
# 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+
|
58
|
+
# method, proc, or string should return or evaluate to a +true+ or +false+
|
53
59
|
# value.
|
54
60
|
def validates_associated(*attr_names)
|
55
61
|
validates_with AssociatedValidator, _merge_attributes(attr_names)
|
@@ -21,10 +21,11 @@ module ActiveRecord
|
|
21
21
|
|
22
22
|
module ClassMethods
|
23
23
|
# Validates whether the value of the specified attribute is numeric by
|
24
|
-
# trying to convert it to a float with Kernel.Float (if
|
25
|
-
# is +false+) or applying it to the regular
|
26
|
-
# (if <tt>only_integer</tt> is set to
|
27
|
-
# defaults to the column's precision
|
24
|
+
# trying to convert it to a float with +Kernel.Float+ (if
|
25
|
+
# <tt>only_integer</tt> is +false+) or applying it to the regular
|
26
|
+
# expression <tt>/\A[\+\-]?\d+\z/</tt> (if <tt>only_integer</tt> is set to
|
27
|
+
# +true+). +Kernel.Float+ precision defaults to the column's precision
|
28
|
+
# value or 15.
|
28
29
|
#
|
29
30
|
# See ActiveModel::Validations::HelperMethods.validates_numericality_of for more information.
|
30
31
|
def validates_numericality_of(*attr_names)
|
@@ -13,9 +13,8 @@ module ActiveRecord
|
|
13
13
|
|
14
14
|
module ClassMethods
|
15
15
|
# Validates that the specified attributes are not blank (as defined by
|
16
|
-
# Object#blank?)
|
17
|
-
#
|
18
|
-
# on save.
|
16
|
+
# Object#blank?). If the attribute is an association, the associated object
|
17
|
+
# is also considered blank if it is marked for destruction.
|
19
18
|
#
|
20
19
|
# class Person < ActiveRecord::Base
|
21
20
|
# has_one :face
|
@@ -25,41 +24,19 @@ module ActiveRecord
|
|
25
24
|
# The face attribute must be in the object and it cannot be blank or marked
|
26
25
|
# for destruction.
|
27
26
|
#
|
28
|
-
# If you want to validate the presence of a boolean field (where the real values
|
29
|
-
# are true and false), you will want to use
|
30
|
-
# <tt>validates_inclusion_of :field_name, in: [true, false]</tt>.
|
31
|
-
#
|
32
|
-
# This is due to the way Object#blank? handles boolean values:
|
33
|
-
# <tt>false.blank? # => true</tt>.
|
34
|
-
#
|
35
27
|
# This validator defers to the Active Model validation for presence, adding the
|
36
28
|
# check to see that an associated object is not marked for destruction. This
|
37
29
|
# prevents the parent object from validating successfully and saving, which then
|
38
30
|
# deletes the associated object, thus putting the parent object into an invalid
|
39
31
|
# state.
|
40
32
|
#
|
33
|
+
# See ActiveModel::Validations::HelperMethods.validates_presence_of for
|
34
|
+
# more information.
|
35
|
+
#
|
41
36
|
# NOTE: This validation will not fail while using it with an association
|
42
37
|
# if the latter was assigned but not valid. If you want to ensure that
|
43
38
|
# it is both present and valid, you also need to use
|
44
39
|
# {validates_associated}[rdoc-ref:Validations::ClassMethods#validates_associated].
|
45
|
-
#
|
46
|
-
# Configuration options:
|
47
|
-
# * <tt>:message</tt> - A custom error message (default is: "can't be blank").
|
48
|
-
# * <tt>:on</tt> - Specifies the contexts where this validation is active.
|
49
|
-
# Runs in all validation contexts by default +nil+. You can pass a symbol
|
50
|
-
# or an array of symbols. (e.g. <tt>on: :create</tt> or
|
51
|
-
# <tt>on: :custom_validation_context</tt> or
|
52
|
-
# <tt>on: [:create, :custom_validation_context]</tt>)
|
53
|
-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
|
54
|
-
# the validation should occur (e.g. <tt>if: :allow_validation</tt>, or
|
55
|
-
# <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
|
56
|
-
# or string should return or evaluate to a +true+ or +false+ value.
|
57
|
-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
|
58
|
-
# if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
59
|
-
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
|
60
|
-
# proc or string should return or evaluate to a +true+ or +false+ value.
|
61
|
-
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
62
|
-
# See ActiveModel::Validations#validates! for more information.
|
63
40
|
def validates_presence_of(*attr_names)
|
64
41
|
validates_with PresenceValidator, _merge_attributes(attr_names)
|
65
42
|
end
|
@@ -20,10 +20,12 @@ module ActiveRecord
|
|
20
20
|
finder_class = find_finder_class_for(record)
|
21
21
|
value = map_enum_attribute(finder_class, attribute, value)
|
22
22
|
|
23
|
+
return if record.persisted? && !validation_needed?(finder_class, record, attribute)
|
24
|
+
|
23
25
|
relation = build_relation(finder_class, attribute, value)
|
24
26
|
if record.persisted?
|
25
27
|
if finder_class.primary_key
|
26
|
-
relation = relation.where.not(finder_class.primary_key => record.id_in_database)
|
28
|
+
relation = relation.where.not(finder_class.primary_key => [record.id_in_database])
|
27
29
|
else
|
28
30
|
raise UnknownPrimaryKey.new(finder_class, "Cannot validate uniqueness for persisted record without primary key.")
|
29
31
|
end
|
@@ -64,18 +66,64 @@ module ActiveRecord
|
|
64
66
|
class_hierarchy.detect { |klass| !klass.abstract_class? }
|
65
67
|
end
|
66
68
|
|
69
|
+
def validation_needed?(klass, record, attribute)
|
70
|
+
return true if options[:conditions] || options.key?(:case_sensitive)
|
71
|
+
|
72
|
+
scope = Array(options[:scope])
|
73
|
+
attributes = scope + [attribute]
|
74
|
+
attributes = resolve_attributes(record, attributes)
|
75
|
+
|
76
|
+
return true if attributes.any? { |attr| record.attribute_changed?(attr) ||
|
77
|
+
record.read_attribute(attr).nil? }
|
78
|
+
|
79
|
+
!covered_by_unique_index?(klass, record, attribute, scope)
|
80
|
+
end
|
81
|
+
|
82
|
+
def covered_by_unique_index?(klass, record, attribute, scope)
|
83
|
+
@covered ||= self.attributes.map(&:to_s).select do |attr|
|
84
|
+
attributes = scope + [attr]
|
85
|
+
attributes = resolve_attributes(record, attributes)
|
86
|
+
|
87
|
+
klass.schema_cache.indexes(klass.table_name).any? do |index|
|
88
|
+
index.unique &&
|
89
|
+
index.where.nil? &&
|
90
|
+
(Array(index.columns) - attributes).empty?
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
@covered.include?(attribute.to_s)
|
95
|
+
end
|
96
|
+
|
97
|
+
def resolve_attributes(record, attributes)
|
98
|
+
attributes.flat_map do |attribute|
|
99
|
+
reflection = record.class._reflect_on_association(attribute)
|
100
|
+
|
101
|
+
if reflection.nil?
|
102
|
+
attribute.to_s
|
103
|
+
elsif reflection.polymorphic?
|
104
|
+
[reflection.foreign_key, reflection.foreign_type]
|
105
|
+
else
|
106
|
+
reflection.foreign_key
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
67
111
|
def build_relation(klass, attribute, value)
|
68
112
|
relation = klass.unscoped
|
69
|
-
|
70
|
-
|
113
|
+
# TODO: Add case-sensitive / case-insensitive operators to Arel
|
114
|
+
# to no longer need to checkout a connection here.
|
115
|
+
comparison = klass.with_connection do |connection|
|
116
|
+
relation.bind_attribute(attribute, value) do |attr, bind|
|
117
|
+
return relation.none! if bind.unboundable?
|
71
118
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
119
|
+
if !options.key?(:case_sensitive) || bind.nil?
|
120
|
+
connection.default_uniqueness_comparison(attr, bind)
|
121
|
+
elsif options[:case_sensitive]
|
122
|
+
connection.case_sensitive_comparison(attr, bind)
|
123
|
+
else
|
124
|
+
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
|
125
|
+
connection.case_insensitive_comparison(attr, bind)
|
126
|
+
end
|
79
127
|
end
|
80
128
|
end
|
81
129
|
|
@@ -161,19 +209,19 @@ module ActiveRecord
|
|
161
209
|
# <tt>WHERE</tt> SQL fragment to limit the uniqueness constraint lookup
|
162
210
|
# (e.g. <tt>conditions: -> { where(status: 'active') }</tt>).
|
163
211
|
# * <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by
|
164
|
-
# non-text columns
|
212
|
+
# non-text columns. The default behavior respects the default database collation.
|
165
213
|
# * <tt>:allow_nil</tt> - If set to +true+, skips this validation if the
|
166
214
|
# attribute is +nil+ (default is +false+).
|
167
215
|
# * <tt>:allow_blank</tt> - If set to +true+, skips this validation if the
|
168
216
|
# attribute is blank (default is +false+).
|
169
|
-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
217
|
+
# * <tt>:if</tt> - Specifies a method, proc, or string to call to determine
|
170
218
|
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
171
219
|
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
|
172
220
|
# proc or string should return or evaluate to a +true+ or +false+ value.
|
173
|
-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
221
|
+
# * <tt>:unless</tt> - Specifies a method, proc, or string to call to
|
174
222
|
# determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
175
223
|
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
176
|
-
# method, proc or string should return or evaluate to a +true+ or +false+
|
224
|
+
# method, proc, or string should return or evaluate to a +true+ or +false+
|
177
225
|
# value.
|
178
226
|
#
|
179
227
|
# === Concurrency and integrity
|
@@ -221,7 +269,7 @@ module ActiveRecord
|
|
221
269
|
# When the database catches such a duplicate insertion,
|
222
270
|
# {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] will raise an ActiveRecord::StatementInvalid
|
223
271
|
# exception. You can either choose to let this error propagate (which
|
224
|
-
# will result in the default Rails exception page being shown), or you
|
272
|
+
# will result in the default \Rails exception page being shown), or you
|
225
273
|
# can catch it and restart the transaction (e.g. by telling the user
|
226
274
|
# that the title already exists, and asking them to re-enter the title).
|
227
275
|
# This technique is also known as
|
@@ -236,6 +284,7 @@ module ActiveRecord
|
|
236
284
|
# The following bundled adapters throw the ActiveRecord::RecordNotUnique exception:
|
237
285
|
#
|
238
286
|
# * ActiveRecord::ConnectionAdapters::Mysql2Adapter.
|
287
|
+
# * ActiveRecord::ConnectionAdapters::TrilogyAdapter.
|
239
288
|
# * ActiveRecord::ConnectionAdapters::SQLite3Adapter.
|
240
289
|
# * ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.
|
241
290
|
def validates_uniqueness_of(*attr_names)
|
@@ -30,14 +30,15 @@ module ActiveRecord
|
|
30
30
|
|
31
31
|
# = Active Record \Validations
|
32
32
|
#
|
33
|
-
# Active Record includes the majority of its validations from ActiveModel::Validations
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
33
|
+
# Active Record includes the majority of its validations from ActiveModel::Validations.
|
34
|
+
#
|
35
|
+
# In Active Record, all validations are performed on save by default.
|
36
|
+
# Validations accept the <tt>:on</tt> argument to define the context where
|
37
|
+
# the validations are active. Active Record will pass either the context of
|
38
|
+
# <tt>:create</tt> or <tt>:update</tt> depending on whether the model is a
|
37
39
|
# {new_record?}[rdoc-ref:Persistence#new_record?].
|
38
40
|
module Validations
|
39
41
|
extend ActiveSupport::Concern
|
40
|
-
include ActiveModel::Validations
|
41
42
|
|
42
43
|
# The validation process on save can be skipped by passing <tt>validate: false</tt>.
|
43
44
|
# The validation context can be changed by passing <tt>context: context</tt>.
|
@@ -60,6 +61,8 @@ module ActiveRecord
|
|
60
61
|
#
|
61
62
|
# If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
|
62
63
|
# {new_record?}[rdoc-ref:Persistence#new_record?] is +true+, and to <tt>:update</tt> if it is not.
|
64
|
+
# If the argument is an array of contexts, <tt>post.valid?([:create, :update])</tt>, the validations are
|
65
|
+
# run within multiple contexts.
|
63
66
|
#
|
64
67
|
# \Validations with no <tt>:on</tt> option will run no matter the context. \Validations with
|
65
68
|
# some <tt>:on</tt> option will only run in the specified context.
|
@@ -71,6 +74,10 @@ module ActiveRecord
|
|
71
74
|
|
72
75
|
alias_method :validate, :valid?
|
73
76
|
|
77
|
+
def custom_validation_context? # :nodoc:
|
78
|
+
validation_context && [:create, :update].exclude?(validation_context)
|
79
|
+
end
|
80
|
+
|
74
81
|
private
|
75
82
|
def default_validation_context
|
76
83
|
new_record? ? :create : :update
|