activerecord 7.0.8 → 7.1.3.4
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 +1554 -1452
- data/MIT-LICENSE +1 -1
- data/README.rdoc +16 -16
- data/lib/active_record/aggregations.rb +16 -13
- data/lib/active_record/association_relation.rb +1 -1
- data/lib/active_record/associations/association.rb +20 -4
- data/lib/active_record/associations/association_scope.rb +16 -9
- data/lib/active_record/associations/belongs_to_association.rb +14 -6
- data/lib/active_record/associations/builder/association.rb +3 -3
- data/lib/active_record/associations/builder/belongs_to.rb +21 -8
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
- data/lib/active_record/associations/builder/singular_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +15 -9
- data/lib/active_record/associations/collection_proxy.rb +15 -10
- data/lib/active_record/associations/foreign_association.rb +10 -3
- data/lib/active_record/associations/has_many_association.rb +20 -13
- data/lib/active_record/associations/has_many_through_association.rb +10 -6
- data/lib/active_record/associations/has_one_association.rb +10 -3
- data/lib/active_record/associations/join_dependency.rb +10 -8
- data/lib/active_record/associations/preloader/association.rb +31 -7
- data/lib/active_record/associations/preloader.rb +13 -10
- data/lib/active_record/associations/singular_association.rb +1 -1
- data/lib/active_record/associations/through_association.rb +22 -11
- data/lib/active_record/associations.rb +313 -217
- data/lib/active_record/attribute_assignment.rb +0 -2
- data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
- data/lib/active_record/attribute_methods/dirty.rb +52 -34
- data/lib/active_record/attribute_methods/primary_key.rb +76 -24
- data/lib/active_record/attribute_methods/query.rb +28 -16
- data/lib/active_record/attribute_methods/read.rb +18 -5
- data/lib/active_record/attribute_methods/serialization.rb +150 -31
- data/lib/active_record/attribute_methods/write.rb +3 -3
- data/lib/active_record/attribute_methods.rb +105 -21
- data/lib/active_record/attributes.rb +3 -3
- data/lib/active_record/autosave_association.rb +55 -9
- data/lib/active_record/base.rb +7 -2
- data/lib/active_record/callbacks.rb +10 -24
- data/lib/active_record/coders/column_serializer.rb +61 -0
- data/lib/active_record/coders/json.rb +1 -1
- data/lib/active_record/coders/yaml_column.rb +70 -42
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +163 -88
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +3 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +74 -51
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +129 -31
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +62 -23
- data/lib/active_record/connection_adapters/abstract/quoting.rb +41 -6
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +289 -124
- data/lib/active_record/connection_adapters/abstract/transaction.rb +287 -58
- data/lib/active_record/connection_adapters/abstract_adapter.rb +511 -91
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +207 -108
- data/lib/active_record/connection_adapters/column.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +22 -143
- data/lib/active_record/connection_adapters/mysql/quoting.rb +16 -12
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +18 -13
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +98 -53
- data/lib/active_record/connection_adapters/pool_config.rb +14 -5
- data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/column.rb +14 -3
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +74 -40
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +10 -6
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +131 -2
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +361 -60
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +353 -192
- data/lib/active_record/connection_adapters/schema_cache.rb +287 -59
- data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +52 -39
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +4 -3
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +1 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +26 -7
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +209 -79
- 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 +262 -0
- data/lib/active_record/connection_adapters.rb +3 -1
- data/lib/active_record/connection_handling.rb +72 -95
- data/lib/active_record/core.rb +175 -153
- data/lib/active_record/counter_cache.rb +46 -25
- data/lib/active_record/database_configurations/database_config.rb +9 -3
- data/lib/active_record/database_configurations/hash_config.rb +22 -12
- data/lib/active_record/database_configurations/url_config.rb +17 -11
- data/lib/active_record/database_configurations.rb +86 -33
- data/lib/active_record/delegated_type.rb +9 -4
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +2 -0
- data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
- data/lib/active_record/encryption/config.rb +25 -1
- data/lib/active_record/encryption/configurable.rb +12 -19
- data/lib/active_record/encryption/context.rb +10 -3
- data/lib/active_record/encryption/contexts.rb +5 -1
- data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
- data/lib/active_record/encryption/encryptable_record.rb +42 -18
- data/lib/active_record/encryption/encrypted_attribute_type.rb +21 -6
- data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -69
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
- data/lib/active_record/encryption/key_generator.rb +12 -1
- data/lib/active_record/encryption/message_serializer.rb +2 -0
- data/lib/active_record/encryption/properties.rb +3 -3
- data/lib/active_record/encryption/scheme.rb +19 -22
- data/lib/active_record/encryption.rb +1 -0
- data/lib/active_record/enum.rb +112 -28
- data/lib/active_record/errors.rb +112 -18
- data/lib/active_record/explain.rb +23 -3
- data/lib/active_record/fixture_set/model_metadata.rb +14 -4
- data/lib/active_record/fixture_set/render_context.rb +2 -0
- data/lib/active_record/fixture_set/table_row.rb +29 -8
- data/lib/active_record/fixtures.rb +135 -71
- data/lib/active_record/future_result.rb +31 -5
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +30 -16
- data/lib/active_record/insert_all.rb +57 -10
- data/lib/active_record/integration.rb +8 -8
- data/lib/active_record/internal_metadata.rb +120 -30
- data/lib/active_record/locking/pessimistic.rb +5 -2
- data/lib/active_record/log_subscriber.rb +29 -12
- 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 +4 -0
- data/lib/active_record/middleware/database_selector.rb +6 -8
- data/lib/active_record/middleware/shard_selector.rb +3 -1
- data/lib/active_record/migration/command_recorder.rb +104 -5
- data/lib/active_record/migration/compatibility.rb +139 -5
- 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/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +219 -111
- data/lib/active_record/model_schema.rb +64 -44
- data/lib/active_record/nested_attributes.rb +24 -6
- data/lib/active_record/normalization.rb +167 -0
- data/lib/active_record/persistence.rb +188 -37
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +3 -21
- data/lib/active_record/query_logs.rb +77 -52
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +15 -2
- data/lib/active_record/railtie.rb +109 -47
- data/lib/active_record/railties/controller_runtime.rb +12 -6
- data/lib/active_record/railties/databases.rake +142 -148
- data/lib/active_record/railties/job_runtime.rb +23 -0
- data/lib/active_record/readonly_attributes.rb +32 -5
- data/lib/active_record/reflection.rb +174 -44
- data/lib/active_record/relation/batches/batch_enumerator.rb +5 -3
- data/lib/active_record/relation/batches.rb +190 -61
- data/lib/active_record/relation/calculations.rb +187 -63
- data/lib/active_record/relation/delegation.rb +23 -9
- data/lib/active_record/relation/finder_methods.rb +77 -16
- data/lib/active_record/relation/merger.rb +2 -0
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +11 -2
- 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 +26 -14
- data/lib/active_record/relation/query_attribute.rb +2 -1
- data/lib/active_record/relation/query_methods.rb +352 -63
- data/lib/active_record/relation/spawn_methods.rb +18 -1
- data/lib/active_record/relation.rb +91 -35
- data/lib/active_record/result.rb +19 -5
- data/lib/active_record/runtime_registry.rb +24 -1
- data/lib/active_record/sanitization.rb +51 -11
- data/lib/active_record/schema.rb +2 -3
- data/lib/active_record/schema_dumper.rb +46 -7
- data/lib/active_record/schema_migration.rb +68 -33
- data/lib/active_record/scoping/default.rb +15 -5
- data/lib/active_record/scoping/named.rb +2 -2
- data/lib/active_record/scoping.rb +2 -1
- data/lib/active_record/secure_password.rb +60 -0
- data/lib/active_record/secure_token.rb +21 -3
- data/lib/active_record/signed_id.rb +7 -5
- data/lib/active_record/store.rb +8 -8
- data/lib/active_record/suppressor.rb +3 -1
- data/lib/active_record/table_metadata.rb +10 -1
- data/lib/active_record/tasks/database_tasks.rb +127 -105
- data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
- data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
- data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
- data/lib/active_record/test_fixtures.rb +113 -96
- data/lib/active_record/timestamp.rb +27 -15
- data/lib/active_record/token_for.rb +113 -0
- data/lib/active_record/touch_later.rb +11 -6
- data/lib/active_record/transactions.rb +36 -10
- data/lib/active_record/type/adapter_specific_registry.rb +1 -8
- data/lib/active_record/type/internal/timezone.rb +7 -2
- data/lib/active_record/type/time.rb +4 -0
- data/lib/active_record/validations/absence.rb +1 -1
- 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 +47 -2
- data/lib/active_record/validations.rb +8 -4
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +121 -16
- data/lib/arel/errors.rb +10 -0
- data/lib/arel/factory_methods.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/cte.rb +36 -0
- data/lib/arel/nodes/fragments.rb +35 -0
- data/lib/arel/nodes/homogeneous_in.rb +1 -9
- data/lib/arel/nodes/leading_join.rb +8 -0
- data/lib/arel/nodes/node.rb +111 -2
- data/lib/arel/nodes/sql_literal.rb +6 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes.rb +4 -0
- data/lib/arel/predications.rb +2 -0
- data/lib/arel/table.rb +9 -5
- data/lib/arel/visitors/mysql.rb +8 -1
- data/lib/arel/visitors/to_sql.rb +81 -17
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +16 -2
- data/lib/rails/generators/active_record/application_record/USAGE +8 -0
- data/lib/rails/generators/active_record/migration.rb +3 -1
- data/lib/rails/generators/active_record/model/USAGE +113 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
- metadata +48 -12
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -63
@@ -6,6 +6,13 @@ module ActiveRecord
|
|
6
6
|
module ModelSchema
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
|
+
##
|
10
|
+
# :method: id_value
|
11
|
+
# :call-seq: id_value
|
12
|
+
#
|
13
|
+
# Returns the underlying column value for a column named "id". Useful when defining
|
14
|
+
# a composite primary key including an "id" column so that the value is readable.
|
15
|
+
|
9
16
|
##
|
10
17
|
# :singleton-method: primary_key_prefix_type
|
11
18
|
# :call-seq: primary_key_prefix_type
|
@@ -180,8 +187,9 @@ module ActiveRecord
|
|
180
187
|
# artists, records => artists_records
|
181
188
|
# records, artists => artists_records
|
182
189
|
# music_artists, music_records => music_artists_records
|
190
|
+
# music.artists, music.records => music.artists_records
|
183
191
|
def self.derive_join_table_name(first_table, second_table) # :nodoc:
|
184
|
-
[first_table.to_s, second_table.to_s].sort.join("\0").gsub(/^(.*_)(.+)\0\1(.+)/, '\1\2_\3').tr("\0", "_")
|
192
|
+
[first_table.to_s, second_table.to_s].sort.join("\0").gsub(/^(.*[_.])(.+)\0\1(.+)/, '\1\2_\3').tr("\0", "_")
|
185
193
|
end
|
186
194
|
|
187
195
|
module ClassMethods
|
@@ -276,8 +284,10 @@ module ActiveRecord
|
|
276
284
|
|
277
285
|
# Computes the table name, (re)sets it internally, and returns it.
|
278
286
|
def reset_table_name # :nodoc:
|
279
|
-
self.table_name = if
|
280
|
-
|
287
|
+
self.table_name = if self == Base
|
288
|
+
nil
|
289
|
+
elsif abstract_class?
|
290
|
+
superclass.table_name
|
281
291
|
elsif superclass.abstract_class?
|
282
292
|
superclass.table_name || compute_table_name
|
283
293
|
else
|
@@ -315,11 +325,7 @@ module ActiveRecord
|
|
315
325
|
# The list of columns names the model should ignore. Ignored columns won't have attribute
|
316
326
|
# accessors defined, and won't be referenced in SQL queries.
|
317
327
|
def ignored_columns
|
318
|
-
|
319
|
-
@ignored_columns
|
320
|
-
else
|
321
|
-
superclass.ignored_columns
|
322
|
-
end
|
328
|
+
@ignored_columns || superclass.ignored_columns
|
323
329
|
end
|
324
330
|
|
325
331
|
# Sets the columns names the model should ignore. Ignored columns won't have attribute
|
@@ -340,7 +346,7 @@ module ActiveRecord
|
|
340
346
|
# # name :string, limit: 255
|
341
347
|
# # category :string, limit: 255
|
342
348
|
#
|
343
|
-
# self.ignored_columns
|
349
|
+
# self.ignored_columns += [:category]
|
344
350
|
# end
|
345
351
|
#
|
346
352
|
# The schema still contains "category", but now the model omits it, so any meta-driven code or
|
@@ -425,6 +431,12 @@ module ActiveRecord
|
|
425
431
|
@columns ||= columns_hash.values.freeze
|
426
432
|
end
|
427
433
|
|
434
|
+
def _returning_columns_for_insert # :nodoc:
|
435
|
+
@_returning_columns_for_insert ||= columns.filter_map do |c|
|
436
|
+
c.name if connection.return_value_after_insert?(c)
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
428
440
|
def attribute_types # :nodoc:
|
429
441
|
load_schema
|
430
442
|
@attribute_types ||= Hash.new(Type.default_value)
|
@@ -457,7 +469,7 @@ module ActiveRecord
|
|
457
469
|
end
|
458
470
|
|
459
471
|
# Returns the column object for the named attribute.
|
460
|
-
# Returns an
|
472
|
+
# Returns an ActiveRecord::ConnectionAdapters::NullColumn if the
|
461
473
|
# named attribute does not exist.
|
462
474
|
#
|
463
475
|
# class Person < ActiveRecord::Base
|
@@ -515,7 +527,7 @@ module ActiveRecord
|
|
515
527
|
# when just after creating a table you want to populate it with some default
|
516
528
|
# values, e.g.:
|
517
529
|
#
|
518
|
-
# class CreateJobLevels < ActiveRecord::Migration[7.
|
530
|
+
# class CreateJobLevels < ActiveRecord::Migration[7.1]
|
519
531
|
# def up
|
520
532
|
# create_table :job_levels do |t|
|
521
533
|
# t.integer :id
|
@@ -543,35 +555,61 @@ module ActiveRecord
|
|
543
555
|
initialize_find_by_cache
|
544
556
|
end
|
545
557
|
|
558
|
+
def load_schema # :nodoc:
|
559
|
+
return if schema_loaded?
|
560
|
+
@load_schema_monitor.synchronize do
|
561
|
+
return if @columns_hash
|
562
|
+
|
563
|
+
load_schema!
|
564
|
+
|
565
|
+
@schema_loaded = true
|
566
|
+
rescue
|
567
|
+
reload_schema_from_cache # If the schema loading failed half way through, we must reset the state.
|
568
|
+
raise
|
569
|
+
end
|
570
|
+
end
|
571
|
+
|
546
572
|
protected
|
547
573
|
def initialize_load_schema_monitor
|
548
574
|
@load_schema_monitor = Monitor.new
|
549
575
|
end
|
550
576
|
|
577
|
+
def reload_schema_from_cache(recursive = true)
|
578
|
+
@_returning_columns_for_insert = nil
|
579
|
+
@arel_table = nil
|
580
|
+
@column_names = nil
|
581
|
+
@symbol_column_to_string_name_hash = nil
|
582
|
+
@attribute_types = nil
|
583
|
+
@content_columns = nil
|
584
|
+
@default_attributes = nil
|
585
|
+
@column_defaults = nil
|
586
|
+
@attributes_builder = nil
|
587
|
+
@columns = nil
|
588
|
+
@columns_hash = nil
|
589
|
+
@schema_loaded = false
|
590
|
+
@attribute_names = nil
|
591
|
+
@yaml_encoder = nil
|
592
|
+
if recursive
|
593
|
+
subclasses.each do |descendant|
|
594
|
+
descendant.send(:reload_schema_from_cache)
|
595
|
+
end
|
596
|
+
end
|
597
|
+
end
|
598
|
+
|
551
599
|
private
|
552
600
|
def inherited(child_class)
|
553
601
|
super
|
554
602
|
child_class.initialize_load_schema_monitor
|
603
|
+
child_class.reload_schema_from_cache(false)
|
604
|
+
child_class.class_eval do
|
605
|
+
@ignored_columns = nil
|
606
|
+
end
|
555
607
|
end
|
556
608
|
|
557
609
|
def schema_loaded?
|
558
610
|
defined?(@schema_loaded) && @schema_loaded
|
559
611
|
end
|
560
612
|
|
561
|
-
def load_schema
|
562
|
-
return if schema_loaded?
|
563
|
-
@load_schema_monitor.synchronize do
|
564
|
-
return if defined?(@columns_hash) && @columns_hash
|
565
|
-
|
566
|
-
load_schema!
|
567
|
-
|
568
|
-
@schema_loaded = true
|
569
|
-
rescue
|
570
|
-
reload_schema_from_cache # If the schema loading failed half way through, we must reset the state.
|
571
|
-
raise
|
572
|
-
end
|
573
|
-
end
|
574
|
-
|
575
613
|
def load_schema!
|
576
614
|
unless table_name
|
577
615
|
raise ActiveRecord::TableNotSpecified, "#{self} has no table configured. Set one with #{self}.table_name="
|
@@ -589,25 +627,7 @@ module ActiveRecord
|
|
589
627
|
default: column.default,
|
590
628
|
user_provided_default: false
|
591
629
|
)
|
592
|
-
|
593
|
-
end
|
594
|
-
|
595
|
-
def reload_schema_from_cache
|
596
|
-
@arel_table = nil
|
597
|
-
@column_names = nil
|
598
|
-
@symbol_column_to_string_name_hash = nil
|
599
|
-
@attribute_types = nil
|
600
|
-
@content_columns = nil
|
601
|
-
@default_attributes = nil
|
602
|
-
@column_defaults = nil
|
603
|
-
@attributes_builder = nil
|
604
|
-
@columns = nil
|
605
|
-
@columns_hash = nil
|
606
|
-
@schema_loaded = false
|
607
|
-
@attribute_names = nil
|
608
|
-
@yaml_encoder = nil
|
609
|
-
subclasses.each do |descendant|
|
610
|
-
descendant.send(:reload_schema_from_cache)
|
630
|
+
alias_attribute :id_value, :id if name == "id"
|
611
631
|
end
|
612
632
|
end
|
613
633
|
|
@@ -15,7 +15,7 @@ module ActiveRecord
|
|
15
15
|
class_attribute :nested_attributes_options, instance_writer: false, default: {}
|
16
16
|
end
|
17
17
|
|
18
|
-
# = Active Record Nested Attributes
|
18
|
+
# = Active Record Nested \Attributes
|
19
19
|
#
|
20
20
|
# Nested attributes allow you to save attributes on associated records
|
21
21
|
# through the parent. By default nested attribute updating is turned off
|
@@ -280,6 +280,24 @@ module ActiveRecord
|
|
280
280
|
# member = Member.new
|
281
281
|
# member.avatar_attributes = {icon: 'sad'}
|
282
282
|
# member.avatar.width # => 200
|
283
|
+
#
|
284
|
+
# === Creating forms with nested attributes
|
285
|
+
#
|
286
|
+
# Use ActionView::Helpers::FormHelper#fields_for to create form elements for
|
287
|
+
# nested attributes.
|
288
|
+
#
|
289
|
+
# Integration test params should reflect the structure of the form. For
|
290
|
+
# example:
|
291
|
+
#
|
292
|
+
# post members_path, params: {
|
293
|
+
# member: {
|
294
|
+
# name: 'joe',
|
295
|
+
# posts_attributes: {
|
296
|
+
# '0' => { title: 'Foo' },
|
297
|
+
# '1' => { title: 'Bar' }
|
298
|
+
# }
|
299
|
+
# }
|
300
|
+
# }
|
283
301
|
module ClassMethods
|
284
302
|
REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |key, value| key == "_destroy" || value.blank? } }
|
285
303
|
|
@@ -289,7 +307,7 @@ module ActiveRecord
|
|
289
307
|
# [:allow_destroy]
|
290
308
|
# If true, destroys any members from the attributes hash with a
|
291
309
|
# <tt>_destroy</tt> key and a value that evaluates to +true+
|
292
|
-
# (e.g. 1, '1', true, or 'true'). This option is
|
310
|
+
# (e.g. 1, '1', true, or 'true'). This option is false by default.
|
293
311
|
# [:reject_if]
|
294
312
|
# Allows you to specify a Proc or a Symbol pointing to a method
|
295
313
|
# that checks whether a record should be built for a certain attribute
|
@@ -314,11 +332,11 @@ module ActiveRecord
|
|
314
332
|
# nested attributes are going to be used when an associated record already
|
315
333
|
# exists. In general, an existing record may either be updated with the
|
316
334
|
# new set of attribute values or be replaced by a wholly new record
|
317
|
-
# containing those values. By default the +:update_only+ option is
|
335
|
+
# containing those values. By default the +:update_only+ option is false
|
318
336
|
# and the nested attributes are used to update the existing record only
|
319
337
|
# if they include the record's <tt>:id</tt> value. Otherwise a new
|
320
338
|
# record will be instantiated and used to replace the existing one.
|
321
|
-
# However if the +:update_only+ option is
|
339
|
+
# However if the +:update_only+ option is true, the nested attributes
|
322
340
|
# are used to update the record's attributes always, regardless of
|
323
341
|
# whether the <tt>:id</tt> is present. The option is ignored for collection
|
324
342
|
# associations.
|
@@ -375,11 +393,11 @@ module ActiveRecord
|
|
375
393
|
end
|
376
394
|
end
|
377
395
|
|
378
|
-
# Returns ActiveRecord::AutosaveAssociation
|
396
|
+
# Returns ActiveRecord::AutosaveAssociation#marked_for_destruction? It's
|
379
397
|
# used in conjunction with fields_for to build a form element for the
|
380
398
|
# destruction of this association.
|
381
399
|
#
|
382
|
-
# See ActionView::Helpers::FormHelper
|
400
|
+
# See ActionView::Helpers::FormHelper#fields_for for more info.
|
383
401
|
def _destroy
|
384
402
|
marked_for_destruction?
|
385
403
|
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord # :nodoc:
|
4
|
+
module Normalization
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :normalized_attributes, default: Set.new
|
9
|
+
|
10
|
+
before_validation :normalize_changed_in_place_attributes
|
11
|
+
end
|
12
|
+
|
13
|
+
# Normalizes a specified attribute using its declared normalizations.
|
14
|
+
#
|
15
|
+
# ==== Examples
|
16
|
+
#
|
17
|
+
# class User < ActiveRecord::Base
|
18
|
+
# normalizes :email, with: -> email { email.strip.downcase }
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# legacy_user = User.find(1)
|
22
|
+
# legacy_user.email # => " CRUISE-CONTROL@EXAMPLE.COM\n"
|
23
|
+
# legacy_user.normalize_attribute(:email)
|
24
|
+
# legacy_user.email # => "cruise-control@example.com"
|
25
|
+
# legacy_user.save
|
26
|
+
def normalize_attribute(name)
|
27
|
+
# Treat the value as a new, unnormalized value.
|
28
|
+
self[name] = self[name]
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
# Declares a normalization for one or more attributes. The normalization
|
33
|
+
# is applied when the attribute is assigned or updated, and the normalized
|
34
|
+
# value will be persisted to the database. The normalization is also
|
35
|
+
# applied to the corresponding keyword argument of query methods. This
|
36
|
+
# allows a record to be created and later queried using unnormalized
|
37
|
+
# values.
|
38
|
+
#
|
39
|
+
# However, to prevent confusion, the normalization will not be applied
|
40
|
+
# when the attribute is fetched from the database. This means that if a
|
41
|
+
# record was persisted before the normalization was declared, the record's
|
42
|
+
# attribute will not be normalized until either it is assigned a new
|
43
|
+
# value, or it is explicitly migrated via Normalization#normalize_attribute.
|
44
|
+
#
|
45
|
+
# Because the normalization may be applied multiple times, it should be
|
46
|
+
# _idempotent_. In other words, applying the normalization more than once
|
47
|
+
# should have the same result as applying it only once.
|
48
|
+
#
|
49
|
+
# By default, the normalization will not be applied to +nil+ values. This
|
50
|
+
# behavior can be changed with the +:apply_to_nil+ option.
|
51
|
+
#
|
52
|
+
# Be aware that if your app was created before Rails 7.1, and your app
|
53
|
+
# marshals instances of the targeted model (for example, when caching),
|
54
|
+
# then you should set ActiveRecord.marshalling_format_version to +7.1+ or
|
55
|
+
# higher via either <tt>config.load_defaults 7.1</tt> or
|
56
|
+
# <tt>config.active_record.marshalling_format_version = 7.1</tt>.
|
57
|
+
# Otherwise, +Marshal+ may attempt to serialize the normalization +Proc+
|
58
|
+
# and raise +TypeError+.
|
59
|
+
#
|
60
|
+
# ==== Options
|
61
|
+
#
|
62
|
+
# * +:with+ - Any callable object that accepts the attribute's value as
|
63
|
+
# its sole argument, and returns it normalized.
|
64
|
+
# * +:apply_to_nil+ - Whether to apply the normalization to +nil+ values.
|
65
|
+
# Defaults to +false+.
|
66
|
+
#
|
67
|
+
# ==== Examples
|
68
|
+
#
|
69
|
+
# class User < ActiveRecord::Base
|
70
|
+
# normalizes :email, with: -> email { email.strip.downcase }
|
71
|
+
# normalizes :phone, with: -> phone { phone.delete("^0-9").delete_prefix("1") }
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# user = User.create(email: " CRUISE-CONTROL@EXAMPLE.COM\n")
|
75
|
+
# user.email # => "cruise-control@example.com"
|
76
|
+
#
|
77
|
+
# user = User.find_by(email: "\tCRUISE-CONTROL@EXAMPLE.COM ")
|
78
|
+
# user.email # => "cruise-control@example.com"
|
79
|
+
# user.email_before_type_cast # => "cruise-control@example.com"
|
80
|
+
#
|
81
|
+
# User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1
|
82
|
+
# User.where(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]).count # => 0
|
83
|
+
#
|
84
|
+
# User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true
|
85
|
+
# User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false
|
86
|
+
#
|
87
|
+
# User.normalize_value_for(:phone, "+1 (555) 867-5309") # => "5558675309"
|
88
|
+
def normalizes(*names, with:, apply_to_nil: false)
|
89
|
+
names.each do |name|
|
90
|
+
attribute(name) do |cast_type|
|
91
|
+
NormalizedValueType.new(cast_type: cast_type, normalizer: with, normalize_nil: apply_to_nil)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
self.normalized_attributes += names.map(&:to_sym)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Normalizes a given +value+ using normalizations declared for +name+.
|
99
|
+
#
|
100
|
+
# ==== Examples
|
101
|
+
#
|
102
|
+
# class User < ActiveRecord::Base
|
103
|
+
# normalizes :email, with: -> email { email.strip.downcase }
|
104
|
+
# end
|
105
|
+
#
|
106
|
+
# User.normalize_value_for(:email, " CRUISE-CONTROL@EXAMPLE.COM\n")
|
107
|
+
# # => "cruise-control@example.com"
|
108
|
+
def normalize_value_for(name, value)
|
109
|
+
type_for_attribute(name).cast(value)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
def normalize_changed_in_place_attributes
|
115
|
+
self.class.normalized_attributes.each do |name|
|
116
|
+
normalize_attribute(name) if attribute_changed_in_place?(name)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class NormalizedValueType < DelegateClass(ActiveModel::Type::Value) # :nodoc:
|
121
|
+
include ActiveModel::Type::SerializeCastValue
|
122
|
+
|
123
|
+
attr_reader :cast_type, :normalizer, :normalize_nil
|
124
|
+
alias :normalize_nil? :normalize_nil
|
125
|
+
|
126
|
+
def initialize(cast_type:, normalizer:, normalize_nil:)
|
127
|
+
@cast_type = cast_type
|
128
|
+
@normalizer = normalizer
|
129
|
+
@normalize_nil = normalize_nil
|
130
|
+
super(cast_type)
|
131
|
+
end
|
132
|
+
|
133
|
+
def cast(value)
|
134
|
+
normalize(super(value))
|
135
|
+
end
|
136
|
+
|
137
|
+
def serialize(value)
|
138
|
+
serialize_cast_value(cast(value))
|
139
|
+
end
|
140
|
+
|
141
|
+
def serialize_cast_value(value)
|
142
|
+
ActiveModel::Type::SerializeCastValue.serialize(cast_type, value)
|
143
|
+
end
|
144
|
+
|
145
|
+
def ==(other)
|
146
|
+
self.class == other.class &&
|
147
|
+
normalize_nil? == other.normalize_nil? &&
|
148
|
+
normalizer == other.normalizer &&
|
149
|
+
cast_type == other.cast_type
|
150
|
+
end
|
151
|
+
alias eql? ==
|
152
|
+
|
153
|
+
def hash
|
154
|
+
[self.class, cast_type, normalizer, normalize_nil?].hash
|
155
|
+
end
|
156
|
+
|
157
|
+
def inspect
|
158
|
+
Kernel.instance_method(:inspect).bind_call(self)
|
159
|
+
end
|
160
|
+
|
161
|
+
private
|
162
|
+
def normalize(value)
|
163
|
+
normalizer.call(value) unless value.nil? && !normalize_nil?
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|