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
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class AsynchronousQueriesTracker # :nodoc:
|
5
|
+
module NullSession # :nodoc:
|
6
|
+
class << self
|
7
|
+
def active?
|
8
|
+
true
|
9
|
+
end
|
10
|
+
|
11
|
+
def finalize
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Session # :nodoc:
|
17
|
+
def initialize
|
18
|
+
@active = true
|
19
|
+
end
|
20
|
+
|
21
|
+
def active?
|
22
|
+
@active
|
23
|
+
end
|
24
|
+
|
25
|
+
def finalize
|
26
|
+
@active = false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class << self
|
31
|
+
def install_executor_hooks(executor = ActiveSupport::Executor)
|
32
|
+
executor.register_hook(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
def run
|
36
|
+
ActiveRecord::Base.asynchronous_queries_tracker.start_session
|
37
|
+
end
|
38
|
+
|
39
|
+
def complete(asynchronous_queries_tracker)
|
40
|
+
asynchronous_queries_tracker.finalize_session
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
attr_reader :current_session
|
45
|
+
|
46
|
+
def initialize
|
47
|
+
@current_session = NullSession
|
48
|
+
end
|
49
|
+
|
50
|
+
def start_session
|
51
|
+
@current_session = Session.new
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def finalize_session
|
56
|
+
@current_session.finalize
|
57
|
+
@current_session = NullSession
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -1,36 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "active_model/forbidden_attributes_protection"
|
4
|
-
|
5
3
|
module ActiveRecord
|
6
4
|
module AttributeAssignment
|
7
|
-
include ActiveModel::AttributeAssignment
|
8
|
-
|
9
5
|
private
|
10
6
|
def _assign_attributes(attributes)
|
11
|
-
multi_parameter_attributes =
|
7
|
+
multi_parameter_attributes = nil
|
12
8
|
|
13
9
|
attributes.each do |k, v|
|
14
10
|
key = k.to_s
|
15
11
|
|
16
12
|
if key.include?("(")
|
17
13
|
(multi_parameter_attributes ||= {})[key] = v
|
18
|
-
elsif v.is_a?(Hash)
|
19
|
-
(nested_parameter_attributes ||= {})[key] = v
|
20
14
|
else
|
21
15
|
_assign_attribute(key, v)
|
22
16
|
end
|
23
17
|
end
|
24
18
|
|
25
|
-
assign_nested_parameter_attributes(nested_parameter_attributes) if nested_parameter_attributes
|
26
19
|
assign_multiparameter_attributes(multi_parameter_attributes) if multi_parameter_attributes
|
27
20
|
end
|
28
21
|
|
29
|
-
# Assign any deferred nested attributes after the base attributes have been set.
|
30
|
-
def assign_nested_parameter_attributes(pairs)
|
31
|
-
pairs.each { |k, v| _assign_attribute(k, v) }
|
32
|
-
end
|
33
|
-
|
34
22
|
# Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done
|
35
23
|
# by calling new on the column type or aggregation type (through composed_of) object with these parameters.
|
36
24
|
# So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate
|
@@ -46,7 +34,7 @@ module ActiveRecord
|
|
46
34
|
def execute_callstack_for_multiparameter_attributes(callstack)
|
47
35
|
errors = []
|
48
36
|
callstack.each do |name, values_with_empty_parameters|
|
49
|
-
if values_with_empty_parameters.each_value.all?(
|
37
|
+
if values_with_empty_parameters.each_value.all?(NilClass)
|
50
38
|
values = nil
|
51
39
|
else
|
52
40
|
values = values_with_empty_parameters
|
@@ -29,8 +29,8 @@ module ActiveRecord
|
|
29
29
|
extend ActiveSupport::Concern
|
30
30
|
|
31
31
|
included do
|
32
|
-
attribute_method_suffix "_before_type_cast", "_for_database"
|
33
|
-
attribute_method_suffix "_came_from_user?"
|
32
|
+
attribute_method_suffix "_before_type_cast", "_for_database", parameters: false
|
33
|
+
attribute_method_suffix "_came_from_user?", parameters: false
|
34
34
|
end
|
35
35
|
|
36
36
|
# Returns the value of the attribute identified by +attr_name+ before
|
@@ -52,6 +52,23 @@ module ActiveRecord
|
|
52
52
|
attribute_before_type_cast(name)
|
53
53
|
end
|
54
54
|
|
55
|
+
# Returns the value of the attribute identified by +attr_name+ after
|
56
|
+
# serialization.
|
57
|
+
#
|
58
|
+
# class Book < ActiveRecord::Base
|
59
|
+
# enum :status, { draft: 1, published: 2 }
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# book = Book.new(status: "published")
|
63
|
+
# book.read_attribute(:status) # => "published"
|
64
|
+
# book.read_attribute_for_database(:status) # => 2
|
65
|
+
def read_attribute_for_database(attr_name)
|
66
|
+
name = attr_name.to_s
|
67
|
+
name = self.class.attribute_aliases[name] || name
|
68
|
+
|
69
|
+
attribute_for_database(name)
|
70
|
+
end
|
71
|
+
|
55
72
|
# Returns a hash of attributes before typecasting and deserialization.
|
56
73
|
#
|
57
74
|
# class Task < ActiveRecord::Base
|
@@ -66,6 +83,11 @@ module ActiveRecord
|
|
66
83
|
@attributes.values_before_type_cast
|
67
84
|
end
|
68
85
|
|
86
|
+
# Returns a hash of attributes for assignment to the database.
|
87
|
+
def attributes_for_database
|
88
|
+
@attributes.values_for_database
|
89
|
+
end
|
90
|
+
|
69
91
|
private
|
70
92
|
# Dispatch target for <tt>*_before_type_cast</tt> attribute methods.
|
71
93
|
def attribute_before_type_cast(attr_name)
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module AttributeMethods
|
5
|
+
module CompositePrimaryKey # :nodoc:
|
6
|
+
# Returns the primary key column's value. If the primary key is composite,
|
7
|
+
# returns an array of the primary key column values.
|
8
|
+
def id
|
9
|
+
if self.class.composite_primary_key?
|
10
|
+
@primary_key.map { |pk| _read_attribute(pk) }
|
11
|
+
else
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def primary_key_values_present? # :nodoc:
|
17
|
+
if self.class.composite_primary_key?
|
18
|
+
id.all?
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Sets the primary key column's value. If the primary key is composite,
|
25
|
+
# raises TypeError when the set value not enumerable.
|
26
|
+
def id=(value)
|
27
|
+
if self.class.composite_primary_key?
|
28
|
+
raise TypeError, "Expected value matching #{self.class.primary_key.inspect}, got #{value.inspect}." unless value.is_a?(Enumerable)
|
29
|
+
@primary_key.zip(value) { |attr, value| _write_attribute(attr, value) }
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Queries the primary key column's value. If the primary key is composite,
|
36
|
+
# all primary key column values must be queryable.
|
37
|
+
def id?
|
38
|
+
if self.class.composite_primary_key?
|
39
|
+
@primary_key.all? { |col| _query_attribute(col) }
|
40
|
+
else
|
41
|
+
super
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns the primary key column's value before type cast. If the primary key is composite,
|
46
|
+
# returns an array of primary key column values before type cast.
|
47
|
+
def id_before_type_cast
|
48
|
+
if self.class.composite_primary_key?
|
49
|
+
@primary_key.map { |col| attribute_before_type_cast(col) }
|
50
|
+
else
|
51
|
+
super
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns the primary key column's previous value. If the primary key is composite,
|
56
|
+
# returns an array of primary key column previous values.
|
57
|
+
def id_was
|
58
|
+
if self.class.composite_primary_key?
|
59
|
+
@primary_key.map { |col| attribute_was(col) }
|
60
|
+
else
|
61
|
+
super
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Returns the primary key column's value from the database. If the primary key is composite,
|
66
|
+
# returns an array of primary key column values from database.
|
67
|
+
def id_in_database
|
68
|
+
if self.class.composite_primary_key?
|
69
|
+
@primary_key.map { |col| attribute_in_database(col) }
|
70
|
+
else
|
71
|
+
super
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def id_for_database # :nodoc:
|
76
|
+
if self.class.composite_primary_key?
|
77
|
+
@primary_key.map { |col| @attributes[col].value_for_database }
|
78
|
+
else
|
79
|
+
super
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -4,6 +4,38 @@ require "active_support/core_ext/module/attribute_accessors"
|
|
4
4
|
|
5
5
|
module ActiveRecord
|
6
6
|
module AttributeMethods
|
7
|
+
# = Active Record Attribute Methods \Dirty
|
8
|
+
#
|
9
|
+
# Provides a way to track changes in your Active Record models. It adds all
|
10
|
+
# methods from ActiveModel::Dirty and adds database-specific methods.
|
11
|
+
#
|
12
|
+
# A newly created +Person+ object is unchanged:
|
13
|
+
#
|
14
|
+
# class Person < ActiveRecord::Base
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# person = Person.create(name: "Allison")
|
18
|
+
# person.changed? # => false
|
19
|
+
#
|
20
|
+
# Change the name:
|
21
|
+
#
|
22
|
+
# person.name = 'Alice'
|
23
|
+
# person.name_in_database # => "Allison"
|
24
|
+
# person.will_save_change_to_name? # => true
|
25
|
+
# person.name_change_to_be_saved # => ["Allison", "Alice"]
|
26
|
+
# person.changes_to_save # => {"name"=>["Allison", "Alice"]}
|
27
|
+
#
|
28
|
+
# Save the changes:
|
29
|
+
#
|
30
|
+
# person.save
|
31
|
+
# person.name_in_database # => "Alice"
|
32
|
+
# person.saved_change_to_name? # => true
|
33
|
+
# person.saved_change_to_name # => ["Allison", "Alice"]
|
34
|
+
# person.name_before_last_save # => "Allison"
|
35
|
+
#
|
36
|
+
# Similar to ActiveModel::Dirty, methods can be invoked as
|
37
|
+
# +saved_change_to_name?+ or by passing an argument to the generic method
|
38
|
+
# <tt>saved_change_to_attribute?("name")</tt>.
|
7
39
|
module Dirty
|
8
40
|
extend ActiveSupport::Concern
|
9
41
|
|
@@ -14,16 +46,17 @@ module ActiveRecord
|
|
14
46
|
raise "You cannot include Dirty after Timestamp"
|
15
47
|
end
|
16
48
|
|
17
|
-
class_attribute :
|
49
|
+
class_attribute :partial_updates, instance_writer: false, default: true
|
50
|
+
class_attribute :partial_inserts, instance_writer: false, default: true
|
18
51
|
|
19
52
|
# Attribute methods for "changed in last call to save?"
|
20
|
-
attribute_method_affix(prefix: "saved_change_to_", suffix: "?")
|
21
|
-
attribute_method_prefix("saved_change_to_")
|
22
|
-
attribute_method_suffix("_before_last_save")
|
53
|
+
attribute_method_affix(prefix: "saved_change_to_", suffix: "?", parameters: "**options")
|
54
|
+
attribute_method_prefix("saved_change_to_", parameters: false)
|
55
|
+
attribute_method_suffix("_before_last_save", parameters: false)
|
23
56
|
|
24
57
|
# Attribute methods for "will change if I call save?"
|
25
|
-
attribute_method_affix(prefix: "will_save_change_to_", suffix: "?")
|
26
|
-
attribute_method_suffix("_change_to_be_saved", "_in_database")
|
58
|
+
attribute_method_affix(prefix: "will_save_change_to_", suffix: "?", parameters: "**options")
|
59
|
+
attribute_method_suffix("_change_to_be_saved", "_in_database", parameters: false)
|
27
60
|
end
|
28
61
|
|
29
62
|
# <tt>reload</tt> the record and clears changed attributes.
|
@@ -43,11 +76,13 @@ module ActiveRecord
|
|
43
76
|
#
|
44
77
|
# ==== Options
|
45
78
|
#
|
46
|
-
# +from+
|
47
|
-
#
|
79
|
+
# [+from+]
|
80
|
+
# When specified, this method will return false unless the original
|
81
|
+
# value is equal to the given value.
|
48
82
|
#
|
49
|
-
# +to+
|
50
|
-
#
|
83
|
+
# [+to+]
|
84
|
+
# When specified, this method will return false unless the value will be
|
85
|
+
# changed to the given value.
|
51
86
|
def saved_change_to_attribute?(attr_name, **options)
|
52
87
|
mutations_before_last_save.changed?(attr_name.to_s, **options)
|
53
88
|
end
|
@@ -93,11 +128,13 @@ module ActiveRecord
|
|
93
128
|
#
|
94
129
|
# ==== Options
|
95
130
|
#
|
96
|
-
# +from+
|
97
|
-
#
|
131
|
+
# [+from+]
|
132
|
+
# When specified, this method will return false unless the original
|
133
|
+
# value is equal to the given value.
|
98
134
|
#
|
99
|
-
# +to+
|
100
|
-
#
|
135
|
+
# [+to+]
|
136
|
+
# When specified, this method will return false unless the value will be
|
137
|
+
# changed to the given value.
|
101
138
|
def will_save_change_to_attribute?(attr_name, **options)
|
102
139
|
mutations_from_database.changed?(attr_name.to_s, **options)
|
103
140
|
end
|
@@ -156,10 +193,12 @@ module ActiveRecord
|
|
156
193
|
end
|
157
194
|
|
158
195
|
private
|
159
|
-
def
|
160
|
-
|
161
|
-
|
162
|
-
|
196
|
+
def init_internals
|
197
|
+
super
|
198
|
+
@mutations_before_last_save = nil
|
199
|
+
@mutations_from_database = nil
|
200
|
+
@_touch_attr_names = nil
|
201
|
+
@_skip_dirty_tracking = nil
|
163
202
|
end
|
164
203
|
|
165
204
|
def _touch_row(attribute_names, time)
|
@@ -191,20 +230,32 @@ module ActiveRecord
|
|
191
230
|
@_touch_attr_names, @_skip_dirty_tracking = nil, nil
|
192
231
|
end
|
193
232
|
|
194
|
-
def _update_record(attribute_names =
|
233
|
+
def _update_record(attribute_names = attribute_names_for_partial_updates)
|
195
234
|
affected_rows = super
|
196
235
|
changes_applied
|
197
236
|
affected_rows
|
198
237
|
end
|
199
238
|
|
200
|
-
def _create_record(attribute_names =
|
239
|
+
def _create_record(attribute_names = attribute_names_for_partial_inserts)
|
201
240
|
id = super
|
202
241
|
changes_applied
|
203
242
|
id
|
204
243
|
end
|
205
244
|
|
206
|
-
def
|
207
|
-
|
245
|
+
def attribute_names_for_partial_updates
|
246
|
+
partial_updates? ? changed_attribute_names_to_save : attribute_names
|
247
|
+
end
|
248
|
+
|
249
|
+
def attribute_names_for_partial_inserts
|
250
|
+
if partial_inserts?
|
251
|
+
changed_attribute_names_to_save
|
252
|
+
else
|
253
|
+
attribute_names.reject do |attr_name|
|
254
|
+
if column_for_attribute(attr_name).auto_populated?
|
255
|
+
!attribute_changed?(attr_name)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
208
259
|
end
|
209
260
|
end
|
210
261
|
end
|
@@ -4,6 +4,7 @@ require "set"
|
|
4
4
|
|
5
5
|
module ActiveRecord
|
6
6
|
module AttributeMethods
|
7
|
+
# = Active Record Attribute Methods Primary Key
|
7
8
|
module PrimaryKey
|
8
9
|
extend ActiveSupport::Concern
|
9
10
|
|
@@ -11,35 +12,45 @@ module ActiveRecord
|
|
11
12
|
# available.
|
12
13
|
def to_key
|
13
14
|
key = id
|
14
|
-
|
15
|
+
Array(key) if key
|
15
16
|
end
|
16
17
|
|
17
|
-
# Returns the primary key column's value.
|
18
|
+
# Returns the primary key column's value. If the primary key is composite,
|
19
|
+
# returns an array of the primary key column values.
|
18
20
|
def id
|
19
21
|
_read_attribute(@primary_key)
|
20
22
|
end
|
21
23
|
|
22
|
-
|
24
|
+
def primary_key_values_present? # :nodoc:
|
25
|
+
!!id
|
26
|
+
end
|
27
|
+
|
28
|
+
# Sets the primary key column's value. If the primary key is composite,
|
29
|
+
# raises TypeError when the set value not enumerable.
|
23
30
|
def id=(value)
|
24
31
|
_write_attribute(@primary_key, value)
|
25
32
|
end
|
26
33
|
|
27
|
-
# Queries the primary key column's value.
|
34
|
+
# Queries the primary key column's value. If the primary key is composite,
|
35
|
+
# all primary key column values must be queryable.
|
28
36
|
def id?
|
29
|
-
|
37
|
+
_query_attribute(@primary_key)
|
30
38
|
end
|
31
39
|
|
32
|
-
# Returns the primary key column's value before type cast.
|
40
|
+
# Returns the primary key column's value before type cast. If the primary key is composite,
|
41
|
+
# returns an array of primary key column values before type cast.
|
33
42
|
def id_before_type_cast
|
34
43
|
attribute_before_type_cast(@primary_key)
|
35
44
|
end
|
36
45
|
|
37
|
-
# Returns the primary key column's previous value.
|
46
|
+
# Returns the primary key column's previous value. If the primary key is composite,
|
47
|
+
# returns an array of primary key column previous values.
|
38
48
|
def id_was
|
39
49
|
attribute_was(@primary_key)
|
40
50
|
end
|
41
51
|
|
42
|
-
# Returns the primary key column's value from the database.
|
52
|
+
# Returns the primary key column's value from the database. If the primary key is composite,
|
53
|
+
# returns an array of primary key column values from database.
|
43
54
|
def id_in_database
|
44
55
|
attribute_in_database(@primary_key)
|
45
56
|
end
|
@@ -55,6 +66,7 @@ module ActiveRecord
|
|
55
66
|
|
56
67
|
module ClassMethods
|
57
68
|
ID_ATTRIBUTE_METHODS = %w(id id= id? id_before_type_cast id_was id_in_database id_for_database).to_set
|
69
|
+
PRIMARY_KEY_NOT_SET = BasicObject.new
|
58
70
|
|
59
71
|
def instance_method_already_implemented?(method_name)
|
60
72
|
super || primary_key && ID_ATTRIBUTE_METHODS.include?(method_name)
|
@@ -68,17 +80,22 @@ module ActiveRecord
|
|
68
80
|
# Overwriting will negate any effect of the +primary_key_prefix_type+
|
69
81
|
# setting, though.
|
70
82
|
def primary_key
|
71
|
-
|
83
|
+
reset_primary_key if PRIMARY_KEY_NOT_SET.equal?(@primary_key)
|
72
84
|
@primary_key
|
73
85
|
end
|
74
86
|
|
87
|
+
def composite_primary_key? # :nodoc:
|
88
|
+
reset_primary_key if PRIMARY_KEY_NOT_SET.equal?(@primary_key)
|
89
|
+
@composite_primary_key
|
90
|
+
end
|
91
|
+
|
75
92
|
# Returns a quoted version of the primary key name, used to construct
|
76
93
|
# SQL statements.
|
77
94
|
def quoted_primary_key
|
78
|
-
@quoted_primary_key ||=
|
95
|
+
@quoted_primary_key ||= adapter_class.quote_column_name(primary_key)
|
79
96
|
end
|
80
97
|
|
81
|
-
def reset_primary_key
|
98
|
+
def reset_primary_key # :nodoc:
|
82
99
|
if base_class?
|
83
100
|
self.primary_key = get_primary_key(base_class.name)
|
84
101
|
else
|
@@ -86,18 +103,15 @@ module ActiveRecord
|
|
86
103
|
end
|
87
104
|
end
|
88
105
|
|
89
|
-
def get_primary_key(base_name)
|
106
|
+
def get_primary_key(base_name) # :nodoc:
|
90
107
|
if base_name && primary_key_prefix_type == :table_name
|
91
108
|
base_name.foreign_key(false)
|
92
109
|
elsif base_name && primary_key_prefix_type == :table_name_with_underscore
|
93
110
|
base_name.foreign_key
|
111
|
+
elsif ActiveRecord::Base != self && table_exists?
|
112
|
+
schema_cache.primary_keys(table_name)
|
94
113
|
else
|
95
|
-
|
96
|
-
pk = connection.schema_cache.primary_keys(table_name)
|
97
|
-
suppress_composite_primary_key(pk)
|
98
|
-
else
|
99
|
-
"id"
|
100
|
-
end
|
114
|
+
"id"
|
101
115
|
end
|
102
116
|
end
|
103
117
|
|
@@ -117,20 +131,26 @@ module ActiveRecord
|
|
117
131
|
#
|
118
132
|
# Project.primary_key # => "foo_id"
|
119
133
|
def primary_key=(value)
|
120
|
-
@primary_key
|
134
|
+
@primary_key = if value.is_a?(Array)
|
135
|
+
@composite_primary_key = true
|
136
|
+
include CompositePrimaryKey
|
137
|
+
@primary_key = value.map { |v| -v.to_s }.freeze
|
138
|
+
elsif value
|
139
|
+
-value.to_s
|
140
|
+
end
|
121
141
|
@quoted_primary_key = nil
|
122
142
|
@attributes_builder = nil
|
123
143
|
end
|
124
144
|
|
125
145
|
private
|
126
|
-
def
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
146
|
+
def inherited(base)
|
147
|
+
super
|
148
|
+
base.class_eval do
|
149
|
+
@primary_key = PRIMARY_KEY_NOT_SET
|
150
|
+
@composite_primary_key = false
|
151
|
+
@quoted_primary_key = nil
|
152
|
+
@attributes_builder = nil
|
153
|
+
end
|
134
154
|
end
|
135
155
|
end
|
136
156
|
end
|
@@ -2,37 +2,49 @@
|
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
module AttributeMethods
|
5
|
+
# = Active Record Attribute Methods \Query
|
5
6
|
module Query
|
6
7
|
extend ActiveSupport::Concern
|
7
8
|
|
8
9
|
included do
|
9
|
-
attribute_method_suffix "?"
|
10
|
+
attribute_method_suffix "?", parameters: false
|
10
11
|
end
|
11
12
|
|
12
13
|
def query_attribute(attr_name)
|
13
|
-
value = self
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
value = self.public_send(attr_name)
|
15
|
+
|
16
|
+
query_cast_attribute(attr_name, value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def _query_attribute(attr_name) # :nodoc:
|
20
|
+
value = self._read_attribute(attr_name.to_s)
|
21
|
+
|
22
|
+
query_cast_attribute(attr_name, value)
|
23
|
+
end
|
24
|
+
|
25
|
+
alias :attribute? :query_attribute
|
26
|
+
private :attribute?
|
27
|
+
|
28
|
+
private
|
29
|
+
def query_cast_attribute(attr_name, value)
|
30
|
+
case value
|
31
|
+
when true then true
|
32
|
+
when false, nil then false
|
33
|
+
else
|
34
|
+
if !type_for_attribute(attr_name) { false }
|
35
|
+
if Numeric === value || !value.match?(/[^0-9]/)
|
36
|
+
!value.to_i.zero?
|
37
|
+
else
|
38
|
+
return false if ActiveModel::Type::Boolean::FALSE_VALUES.include?(value)
|
39
|
+
!value.blank?
|
40
|
+
end
|
41
|
+
elsif value.respond_to?(:zero?)
|
42
|
+
!value.zero?
|
22
43
|
else
|
23
|
-
return false if ActiveModel::Type::Boolean::FALSE_VALUES.include?(value)
|
24
44
|
!value.blank?
|
25
45
|
end
|
26
|
-
elsif value.respond_to?(:zero?)
|
27
|
-
!value.zero?
|
28
|
-
else
|
29
|
-
!value.blank?
|
30
46
|
end
|
31
47
|
end
|
32
|
-
end
|
33
|
-
|
34
|
-
alias :attribute? :query_attribute
|
35
|
-
private :attribute?
|
36
48
|
end
|
37
49
|
end
|
38
50
|
end
|
@@ -2,37 +2,40 @@
|
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
module AttributeMethods
|
5
|
+
# = Active Record Attribute Methods \Read
|
5
6
|
module Read
|
6
7
|
extend ActiveSupport::Concern
|
7
8
|
|
8
9
|
module ClassMethods # :nodoc:
|
9
10
|
private
|
10
|
-
def define_method_attribute(
|
11
|
+
def define_method_attribute(canonical_name, owner:, as: canonical_name)
|
11
12
|
ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
|
12
|
-
owner,
|
13
|
+
owner, canonical_name
|
13
14
|
) do |temp_method_name, attr_name_expr|
|
14
|
-
owner
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
owner.define_cached_method(temp_method_name, as: as, namespace: :active_record) do |batch|
|
16
|
+
batch <<
|
17
|
+
"def #{temp_method_name}" <<
|
18
|
+
" _read_attribute(#{attr_name_expr}) { |n| missing_attribute(n, caller) }" <<
|
19
|
+
"end"
|
20
|
+
end
|
18
21
|
end
|
19
22
|
end
|
20
23
|
end
|
21
24
|
|
22
|
-
# Returns the value of the attribute identified by
|
23
|
-
#
|
24
|
-
# to
|
25
|
+
# Returns the value of the attribute identified by +attr_name+ after it
|
26
|
+
# has been type cast. For example, a date attribute will cast "2004-12-12"
|
27
|
+
# to <tt>Date.new(2004, 12, 12)</tt>. (For information about specific type
|
28
|
+
# casting behavior, see the types under ActiveModel::Type.)
|
25
29
|
def read_attribute(attr_name, &block)
|
26
30
|
name = attr_name.to_s
|
27
31
|
name = self.class.attribute_aliases[name] || name
|
28
32
|
|
29
|
-
name = @primary_key if name == "id" && @primary_key
|
30
33
|
@attributes.fetch_value(name, &block)
|
31
34
|
end
|
32
35
|
|
33
36
|
# This method exists to avoid the expensive primary_key check internally, without
|
34
37
|
# breaking compatibility with the read_attribute API
|
35
|
-
def _read_attribute(attr_name, &block) # :nodoc
|
38
|
+
def _read_attribute(attr_name, &block) # :nodoc:
|
36
39
|
@attributes.fetch_value(attr_name, &block)
|
37
40
|
end
|
38
41
|
|