activerecord 6.0.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1086 -0
- data/MIT-LICENSE +22 -0
- data/README.rdoc +219 -0
- data/examples/performance.rb +185 -0
- data/examples/simple.rb +15 -0
- data/lib/active_record.rb +195 -0
- data/lib/active_record/aggregations.rb +285 -0
- data/lib/active_record/association_relation.rb +49 -0
- data/lib/active_record/associations.rb +1865 -0
- data/lib/active_record/associations/alias_tracker.rb +81 -0
- data/lib/active_record/associations/association.rb +340 -0
- data/lib/active_record/associations/association_scope.rb +166 -0
- data/lib/active_record/associations/belongs_to_association.rb +124 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +36 -0
- data/lib/active_record/associations/builder/association.rb +136 -0
- data/lib/active_record/associations/builder/belongs_to.rb +130 -0
- data/lib/active_record/associations/builder/collection_association.rb +72 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +114 -0
- data/lib/active_record/associations/builder/has_many.rb +19 -0
- data/lib/active_record/associations/builder/has_one.rb +64 -0
- data/lib/active_record/associations/builder/singular_association.rb +44 -0
- data/lib/active_record/associations/collection_association.rb +498 -0
- data/lib/active_record/associations/collection_proxy.rb +1128 -0
- data/lib/active_record/associations/foreign_association.rb +20 -0
- data/lib/active_record/associations/has_many_association.rb +136 -0
- data/lib/active_record/associations/has_many_through_association.rb +220 -0
- data/lib/active_record/associations/has_one_association.rb +118 -0
- data/lib/active_record/associations/has_one_through_association.rb +45 -0
- data/lib/active_record/associations/join_dependency.rb +262 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +80 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +23 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +71 -0
- data/lib/active_record/associations/preloader.rb +201 -0
- data/lib/active_record/associations/preloader/association.rb +133 -0
- data/lib/active_record/associations/preloader/through_association.rb +116 -0
- data/lib/active_record/associations/singular_association.rb +59 -0
- data/lib/active_record/associations/through_association.rb +121 -0
- data/lib/active_record/attribute_assignment.rb +85 -0
- data/lib/active_record/attribute_decorators.rb +90 -0
- data/lib/active_record/attribute_methods.rb +420 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +81 -0
- data/lib/active_record/attribute_methods/dirty.rb +221 -0
- data/lib/active_record/attribute_methods/primary_key.rb +136 -0
- data/lib/active_record/attribute_methods/query.rb +41 -0
- data/lib/active_record/attribute_methods/read.rb +47 -0
- data/lib/active_record/attribute_methods/serialization.rb +90 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +91 -0
- data/lib/active_record/attribute_methods/write.rb +61 -0
- data/lib/active_record/attributes.rb +279 -0
- data/lib/active_record/autosave_association.rb +512 -0
- data/lib/active_record/base.rb +328 -0
- data/lib/active_record/callbacks.rb +339 -0
- data/lib/active_record/coders/json.rb +15 -0
- data/lib/active_record/coders/yaml_column.rb +50 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +1175 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +85 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +516 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +155 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +251 -0
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +713 -0
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +93 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1475 -0
- data/lib/active_record/connection_adapters/abstract/transaction.rb +323 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +772 -0
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +830 -0
- data/lib/active_record/connection_adapters/column.rb +95 -0
- data/lib/active_record/connection_adapters/connection_specification.rb +297 -0
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +29 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +202 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +95 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +88 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +146 -0
- data/lib/active_record/connection_adapters/postgresql/column.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +184 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +113 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +26 -0
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +205 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +222 -0
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +776 -0
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +953 -0
- data/lib/active_record/connection_adapters/schema_cache.rb +141 -0
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +103 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +137 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +561 -0
- data/lib/active_record/connection_adapters/statement_pool.rb +61 -0
- data/lib/active_record/connection_handling.rb +274 -0
- data/lib/active_record/core.rb +603 -0
- data/lib/active_record/counter_cache.rb +193 -0
- data/lib/active_record/database_configurations.rb +233 -0
- data/lib/active_record/database_configurations/database_config.rb +37 -0
- data/lib/active_record/database_configurations/hash_config.rb +50 -0
- data/lib/active_record/database_configurations/url_config.rb +79 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +122 -0
- data/lib/active_record/enum.rb +274 -0
- data/lib/active_record/errors.rb +388 -0
- data/lib/active_record/explain.rb +50 -0
- data/lib/active_record/explain_registry.rb +32 -0
- data/lib/active_record/explain_subscriber.rb +34 -0
- data/lib/active_record/fixture_set/file.rb +82 -0
- data/lib/active_record/fixture_set/model_metadata.rb +33 -0
- data/lib/active_record/fixture_set/render_context.rb +17 -0
- data/lib/active_record/fixture_set/table_row.rb +153 -0
- data/lib/active_record/fixture_set/table_rows.rb +47 -0
- data/lib/active_record/fixtures.rb +738 -0
- data/lib/active_record/gem_version.rb +17 -0
- data/lib/active_record/inheritance.rb +293 -0
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +207 -0
- data/lib/active_record/internal_metadata.rb +53 -0
- data/lib/active_record/legacy_yaml_adapter.rb +48 -0
- data/lib/active_record/locale/en.yml +48 -0
- data/lib/active_record/locking/optimistic.rb +197 -0
- data/lib/active_record/locking/pessimistic.rb +89 -0
- data/lib/active_record/log_subscriber.rb +118 -0
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/migration.rb +1397 -0
- data/lib/active_record/migration/command_recorder.rb +284 -0
- data/lib/active_record/migration/compatibility.rb +244 -0
- data/lib/active_record/migration/join_table.rb +17 -0
- data/lib/active_record/model_schema.rb +545 -0
- data/lib/active_record/nested_attributes.rb +600 -0
- data/lib/active_record/no_touching.rb +65 -0
- data/lib/active_record/null_relation.rb +68 -0
- data/lib/active_record/persistence.rb +967 -0
- data/lib/active_record/query_cache.rb +52 -0
- data/lib/active_record/querying.rb +82 -0
- data/lib/active_record/railtie.rb +263 -0
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/console_sandbox.rb +7 -0
- data/lib/active_record/railties/controller_runtime.rb +51 -0
- data/lib/active_record/railties/databases.rake +527 -0
- data/lib/active_record/readonly_attributes.rb +24 -0
- data/lib/active_record/reflection.rb +1042 -0
- data/lib/active_record/relation.rb +860 -0
- data/lib/active_record/relation/batches.rb +290 -0
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/calculations.rb +424 -0
- data/lib/active_record/relation/delegation.rb +130 -0
- data/lib/active_record/relation/finder_methods.rb +561 -0
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +184 -0
- data/lib/active_record/relation/predicate_builder.rb +150 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +49 -0
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +18 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +1371 -0
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +77 -0
- data/lib/active_record/relation/where_clause.rb +190 -0
- data/lib/active_record/relation/where_clause_factory.rb +33 -0
- data/lib/active_record/result.rb +168 -0
- data/lib/active_record/runtime_registry.rb +24 -0
- data/lib/active_record/sanitization.rb +214 -0
- data/lib/active_record/schema.rb +61 -0
- data/lib/active_record/schema_dumper.rb +270 -0
- data/lib/active_record/schema_migration.rb +60 -0
- data/lib/active_record/scoping.rb +106 -0
- data/lib/active_record/scoping/default.rb +151 -0
- data/lib/active_record/scoping/named.rb +217 -0
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +22 -0
- data/lib/active_record/statement_cache.rb +148 -0
- data/lib/active_record/store.rb +290 -0
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +75 -0
- data/lib/active_record/tasks/database_tasks.rb +506 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +141 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +77 -0
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +224 -0
- data/lib/active_record/timestamp.rb +167 -0
- data/lib/active_record/touch_later.rb +66 -0
- data/lib/active_record/transactions.rb +493 -0
- data/lib/active_record/translation.rb +24 -0
- data/lib/active_record/type.rb +78 -0
- data/lib/active_record/type/adapter_specific_registry.rb +129 -0
- data/lib/active_record/type/date.rb +9 -0
- data/lib/active_record/type/date_time.rb +9 -0
- data/lib/active_record/type/decimal_without_scale.rb +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
- data/lib/active_record/type/internal/timezone.rb +17 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +71 -0
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +21 -0
- data/lib/active_record/type/type_map.rb +62 -0
- data/lib/active_record/type/unsigned_integer.rb +17 -0
- data/lib/active_record/type_caster.rb +9 -0
- data/lib/active_record/type_caster/connection.rb +34 -0
- data/lib/active_record/type_caster/map.rb +20 -0
- data/lib/active_record/validations.rb +94 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +60 -0
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +68 -0
- data/lib/active_record/validations/uniqueness.rb +226 -0
- data/lib/active_record/version.rb +10 -0
- data/lib/arel.rb +58 -0
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes.rb +22 -0
- data/lib/arel/attributes/attribute.rb +37 -0
- data/lib/arel/collectors/bind.rb +24 -0
- data/lib/arel/collectors/composite.rb +31 -0
- data/lib/arel/collectors/plain_string.rb +20 -0
- data/lib/arel/collectors/sql_string.rb +20 -0
- data/lib/arel/collectors/substitute_binds.rb +28 -0
- data/lib/arel/crud.rb +42 -0
- data/lib/arel/delete_manager.rb +18 -0
- data/lib/arel/errors.rb +9 -0
- data/lib/arel/expressions.rb +29 -0
- data/lib/arel/factory_methods.rb +49 -0
- data/lib/arel/insert_manager.rb +49 -0
- data/lib/arel/math.rb +45 -0
- data/lib/arel/nodes.rb +68 -0
- data/lib/arel/nodes/and.rb +32 -0
- data/lib/arel/nodes/ascending.rb +23 -0
- data/lib/arel/nodes/binary.rb +52 -0
- data/lib/arel/nodes/bind_param.rb +36 -0
- data/lib/arel/nodes/case.rb +55 -0
- data/lib/arel/nodes/casted.rb +50 -0
- data/lib/arel/nodes/comment.rb +29 -0
- data/lib/arel/nodes/count.rb +12 -0
- data/lib/arel/nodes/delete_statement.rb +45 -0
- data/lib/arel/nodes/descending.rb +23 -0
- data/lib/arel/nodes/equality.rb +18 -0
- data/lib/arel/nodes/extract.rb +24 -0
- data/lib/arel/nodes/false.rb +16 -0
- data/lib/arel/nodes/full_outer_join.rb +8 -0
- data/lib/arel/nodes/function.rb +44 -0
- data/lib/arel/nodes/grouping.rb +8 -0
- data/lib/arel/nodes/in.rb +8 -0
- data/lib/arel/nodes/infix_operation.rb +80 -0
- data/lib/arel/nodes/inner_join.rb +8 -0
- data/lib/arel/nodes/insert_statement.rb +37 -0
- data/lib/arel/nodes/join_source.rb +20 -0
- data/lib/arel/nodes/matches.rb +18 -0
- data/lib/arel/nodes/named_function.rb +23 -0
- data/lib/arel/nodes/node.rb +50 -0
- data/lib/arel/nodes/node_expression.rb +13 -0
- data/lib/arel/nodes/outer_join.rb +8 -0
- data/lib/arel/nodes/over.rb +15 -0
- data/lib/arel/nodes/regexp.rb +16 -0
- data/lib/arel/nodes/right_outer_join.rb +8 -0
- data/lib/arel/nodes/select_core.rb +67 -0
- data/lib/arel/nodes/select_statement.rb +41 -0
- data/lib/arel/nodes/sql_literal.rb +16 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/table_alias.rb +27 -0
- data/lib/arel/nodes/terminal.rb +16 -0
- data/lib/arel/nodes/true.rb +16 -0
- data/lib/arel/nodes/unary.rb +45 -0
- data/lib/arel/nodes/unary_operation.rb +20 -0
- data/lib/arel/nodes/unqualified_column.rb +22 -0
- data/lib/arel/nodes/update_statement.rb +41 -0
- data/lib/arel/nodes/values_list.rb +9 -0
- data/lib/arel/nodes/window.rb +126 -0
- data/lib/arel/nodes/with.rb +11 -0
- data/lib/arel/order_predications.rb +13 -0
- data/lib/arel/predications.rb +257 -0
- data/lib/arel/select_manager.rb +271 -0
- data/lib/arel/table.rb +110 -0
- data/lib/arel/tree_manager.rb +72 -0
- data/lib/arel/update_manager.rb +34 -0
- data/lib/arel/visitors.rb +20 -0
- data/lib/arel/visitors/depth_first.rb +204 -0
- data/lib/arel/visitors/dot.rb +297 -0
- data/lib/arel/visitors/ibm_db.rb +34 -0
- data/lib/arel/visitors/informix.rb +62 -0
- data/lib/arel/visitors/mssql.rb +157 -0
- data/lib/arel/visitors/mysql.rb +83 -0
- data/lib/arel/visitors/oracle.rb +159 -0
- data/lib/arel/visitors/oracle12.rb +66 -0
- data/lib/arel/visitors/postgresql.rb +110 -0
- data/lib/arel/visitors/sqlite.rb +39 -0
- data/lib/arel/visitors/to_sql.rb +889 -0
- data/lib/arel/visitors/visitor.rb +46 -0
- data/lib/arel/visitors/where_sql.rb +23 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/rails/generators/active_record.rb +19 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
- data/lib/rails/generators/active_record/migration.rb +48 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +75 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +48 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +49 -0
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
- data/lib/rails/generators/active_record/model/templates/module.rb.tt +7 -0
- metadata +418 -0
@@ -0,0 +1,512 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
# = Active Record Autosave Association
|
5
|
+
#
|
6
|
+
# AutosaveAssociation is a module that takes care of automatically saving
|
7
|
+
# associated records when their parent is saved. In addition to saving, it
|
8
|
+
# also destroys any associated records that were marked for destruction.
|
9
|
+
# (See #mark_for_destruction and #marked_for_destruction?).
|
10
|
+
#
|
11
|
+
# Saving of the parent, its associations, and the destruction of marked
|
12
|
+
# associations, all happen inside a transaction. This should never leave the
|
13
|
+
# database in an inconsistent state.
|
14
|
+
#
|
15
|
+
# If validations for any of the associations fail, their error messages will
|
16
|
+
# be applied to the parent.
|
17
|
+
#
|
18
|
+
# Note that it also means that associations marked for destruction won't
|
19
|
+
# be destroyed directly. They will however still be marked for destruction.
|
20
|
+
#
|
21
|
+
# Note that <tt>autosave: false</tt> is not same as not declaring <tt>:autosave</tt>.
|
22
|
+
# When the <tt>:autosave</tt> option is not present then new association records are
|
23
|
+
# saved but the updated association records are not saved.
|
24
|
+
#
|
25
|
+
# == Validation
|
26
|
+
#
|
27
|
+
# Child records are validated unless <tt>:validate</tt> is +false+.
|
28
|
+
#
|
29
|
+
# == Callbacks
|
30
|
+
#
|
31
|
+
# Association with autosave option defines several callbacks on your
|
32
|
+
# model (before_save, after_create, after_update). Please note that
|
33
|
+
# callbacks are executed in the order they were defined in
|
34
|
+
# model. You should avoid modifying the association content, before
|
35
|
+
# autosave callbacks are executed. Placing your callbacks after
|
36
|
+
# associations is usually a good practice.
|
37
|
+
#
|
38
|
+
# === One-to-one Example
|
39
|
+
#
|
40
|
+
# class Post < ActiveRecord::Base
|
41
|
+
# has_one :author, autosave: true
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# Saving changes to the parent and its associated model can now be performed
|
45
|
+
# automatically _and_ atomically:
|
46
|
+
#
|
47
|
+
# post = Post.find(1)
|
48
|
+
# post.title # => "The current global position of migrating ducks"
|
49
|
+
# post.author.name # => "alloy"
|
50
|
+
#
|
51
|
+
# post.title = "On the migration of ducks"
|
52
|
+
# post.author.name = "Eloy Duran"
|
53
|
+
#
|
54
|
+
# post.save
|
55
|
+
# post.reload
|
56
|
+
# post.title # => "On the migration of ducks"
|
57
|
+
# post.author.name # => "Eloy Duran"
|
58
|
+
#
|
59
|
+
# Destroying an associated model, as part of the parent's save action, is as
|
60
|
+
# simple as marking it for destruction:
|
61
|
+
#
|
62
|
+
# post.author.mark_for_destruction
|
63
|
+
# post.author.marked_for_destruction? # => true
|
64
|
+
#
|
65
|
+
# Note that the model is _not_ yet removed from the database:
|
66
|
+
#
|
67
|
+
# id = post.author.id
|
68
|
+
# Author.find_by(id: id).nil? # => false
|
69
|
+
#
|
70
|
+
# post.save
|
71
|
+
# post.reload.author # => nil
|
72
|
+
#
|
73
|
+
# Now it _is_ removed from the database:
|
74
|
+
#
|
75
|
+
# Author.find_by(id: id).nil? # => true
|
76
|
+
#
|
77
|
+
# === One-to-many Example
|
78
|
+
#
|
79
|
+
# When <tt>:autosave</tt> is not declared new children are saved when their parent is saved:
|
80
|
+
#
|
81
|
+
# class Post < ActiveRecord::Base
|
82
|
+
# has_many :comments # :autosave option is not declared
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# post = Post.new(title: 'ruby rocks')
|
86
|
+
# post.comments.build(body: 'hello world')
|
87
|
+
# post.save # => saves both post and comment
|
88
|
+
#
|
89
|
+
# post = Post.create(title: 'ruby rocks')
|
90
|
+
# post.comments.build(body: 'hello world')
|
91
|
+
# post.save # => saves both post and comment
|
92
|
+
#
|
93
|
+
# post = Post.create(title: 'ruby rocks')
|
94
|
+
# post.comments.create(body: 'hello world')
|
95
|
+
# post.save # => saves both post and comment
|
96
|
+
#
|
97
|
+
# When <tt>:autosave</tt> is true all children are saved, no matter whether they
|
98
|
+
# are new records or not:
|
99
|
+
#
|
100
|
+
# class Post < ActiveRecord::Base
|
101
|
+
# has_many :comments, autosave: true
|
102
|
+
# end
|
103
|
+
#
|
104
|
+
# post = Post.create(title: 'ruby rocks')
|
105
|
+
# post.comments.create(body: 'hello world')
|
106
|
+
# post.comments[0].body = 'hi everyone'
|
107
|
+
# post.comments.build(body: "good morning.")
|
108
|
+
# post.title += "!"
|
109
|
+
# post.save # => saves both post and comments.
|
110
|
+
#
|
111
|
+
# Destroying one of the associated models as part of the parent's save action
|
112
|
+
# is as simple as marking it for destruction:
|
113
|
+
#
|
114
|
+
# post.comments # => [#<Comment id: 1, ...>, #<Comment id: 2, ...]>
|
115
|
+
# post.comments[1].mark_for_destruction
|
116
|
+
# post.comments[1].marked_for_destruction? # => true
|
117
|
+
# post.comments.length # => 2
|
118
|
+
#
|
119
|
+
# Note that the model is _not_ yet removed from the database:
|
120
|
+
#
|
121
|
+
# id = post.comments.last.id
|
122
|
+
# Comment.find_by(id: id).nil? # => false
|
123
|
+
#
|
124
|
+
# post.save
|
125
|
+
# post.reload.comments.length # => 1
|
126
|
+
#
|
127
|
+
# Now it _is_ removed from the database:
|
128
|
+
#
|
129
|
+
# Comment.find_by(id: id).nil? # => true
|
130
|
+
module AutosaveAssociation
|
131
|
+
extend ActiveSupport::Concern
|
132
|
+
|
133
|
+
module AssociationBuilderExtension #:nodoc:
|
134
|
+
def self.build(model, reflection)
|
135
|
+
model.send(:add_autosave_association_callbacks, reflection)
|
136
|
+
end
|
137
|
+
|
138
|
+
def self.valid_options
|
139
|
+
[ :autosave ]
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
included do
|
144
|
+
Associations::Builder::Association.extensions << AssociationBuilderExtension
|
145
|
+
mattr_accessor :index_nested_attribute_errors, instance_writer: false, default: false
|
146
|
+
end
|
147
|
+
|
148
|
+
module ClassMethods # :nodoc:
|
149
|
+
private
|
150
|
+
|
151
|
+
def define_non_cyclic_method(name, &block)
|
152
|
+
return if instance_methods(false).include?(name)
|
153
|
+
define_method(name) do |*args|
|
154
|
+
result = true; @_already_called ||= {}
|
155
|
+
# Loop prevention for validation of associations
|
156
|
+
unless @_already_called[name]
|
157
|
+
begin
|
158
|
+
@_already_called[name] = true
|
159
|
+
result = instance_eval(&block)
|
160
|
+
ensure
|
161
|
+
@_already_called[name] = false
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
result
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Adds validation and save callbacks for the association as specified by
|
170
|
+
# the +reflection+.
|
171
|
+
#
|
172
|
+
# For performance reasons, we don't check whether to validate at runtime.
|
173
|
+
# However the validation and callback methods are lazy and those methods
|
174
|
+
# get created when they are invoked for the very first time. However,
|
175
|
+
# this can change, for instance, when using nested attributes, which is
|
176
|
+
# called _after_ the association has been defined. Since we don't want
|
177
|
+
# the callbacks to get defined multiple times, there are guards that
|
178
|
+
# check if the save or validation methods have already been defined
|
179
|
+
# before actually defining them.
|
180
|
+
def add_autosave_association_callbacks(reflection)
|
181
|
+
save_method = :"autosave_associated_records_for_#{reflection.name}"
|
182
|
+
|
183
|
+
if reflection.collection?
|
184
|
+
before_save :before_save_collection_association
|
185
|
+
after_save :after_save_collection_association
|
186
|
+
|
187
|
+
define_non_cyclic_method(save_method) { save_collection_association(reflection) }
|
188
|
+
# Doesn't use after_save as that would save associations added in after_create/after_update twice
|
189
|
+
after_create save_method
|
190
|
+
after_update save_method
|
191
|
+
elsif reflection.has_one?
|
192
|
+
define_method(save_method) { save_has_one_association(reflection) } unless method_defined?(save_method)
|
193
|
+
# Configures two callbacks instead of a single after_save so that
|
194
|
+
# the model may rely on their execution order relative to its
|
195
|
+
# own callbacks.
|
196
|
+
#
|
197
|
+
# For example, given that after_creates run before after_saves, if
|
198
|
+
# we configured instead an after_save there would be no way to fire
|
199
|
+
# a custom after_create callback after the child association gets
|
200
|
+
# created.
|
201
|
+
after_create save_method
|
202
|
+
after_update save_method
|
203
|
+
else
|
204
|
+
define_non_cyclic_method(save_method) { throw(:abort) if save_belongs_to_association(reflection) == false }
|
205
|
+
before_save save_method
|
206
|
+
end
|
207
|
+
|
208
|
+
define_autosave_validation_callbacks(reflection)
|
209
|
+
end
|
210
|
+
|
211
|
+
def define_autosave_validation_callbacks(reflection)
|
212
|
+
validation_method = :"validate_associated_records_for_#{reflection.name}"
|
213
|
+
if reflection.validate? && !method_defined?(validation_method)
|
214
|
+
if reflection.collection?
|
215
|
+
method = :validate_collection_association
|
216
|
+
else
|
217
|
+
method = :validate_single_association
|
218
|
+
end
|
219
|
+
|
220
|
+
define_non_cyclic_method(validation_method) { send(method, reflection) }
|
221
|
+
validate validation_method
|
222
|
+
after_validation :_ensure_no_duplicate_errors
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
# Reloads the attributes of the object as usual and clears <tt>marked_for_destruction</tt> flag.
|
228
|
+
def reload(options = nil)
|
229
|
+
@marked_for_destruction = false
|
230
|
+
@destroyed_by_association = nil
|
231
|
+
super
|
232
|
+
end
|
233
|
+
|
234
|
+
# Marks this record to be destroyed as part of the parent's save transaction.
|
235
|
+
# This does _not_ actually destroy the record instantly, rather child record will be destroyed
|
236
|
+
# when <tt>parent.save</tt> is called.
|
237
|
+
#
|
238
|
+
# Only useful if the <tt>:autosave</tt> option on the parent is enabled for this associated model.
|
239
|
+
def mark_for_destruction
|
240
|
+
@marked_for_destruction = true
|
241
|
+
end
|
242
|
+
|
243
|
+
# Returns whether or not this record will be destroyed as part of the parent's save transaction.
|
244
|
+
#
|
245
|
+
# Only useful if the <tt>:autosave</tt> option on the parent is enabled for this associated model.
|
246
|
+
def marked_for_destruction?
|
247
|
+
@marked_for_destruction
|
248
|
+
end
|
249
|
+
|
250
|
+
# Records the association that is being destroyed and destroying this
|
251
|
+
# record in the process.
|
252
|
+
def destroyed_by_association=(reflection)
|
253
|
+
@destroyed_by_association = reflection
|
254
|
+
end
|
255
|
+
|
256
|
+
# Returns the association for the parent being destroyed.
|
257
|
+
#
|
258
|
+
# Used to avoid updating the counter cache unnecessarily.
|
259
|
+
def destroyed_by_association
|
260
|
+
@destroyed_by_association
|
261
|
+
end
|
262
|
+
|
263
|
+
# Returns whether or not this record has been changed in any way (including whether
|
264
|
+
# any of its nested autosave associations are likewise changed)
|
265
|
+
def changed_for_autosave?
|
266
|
+
new_record? || has_changes_to_save? || marked_for_destruction? || nested_records_changed_for_autosave?
|
267
|
+
end
|
268
|
+
|
269
|
+
private
|
270
|
+
|
271
|
+
# Returns the record for an association collection that should be validated
|
272
|
+
# or saved. If +autosave+ is +false+ only new records will be returned,
|
273
|
+
# unless the parent is/was a new record itself.
|
274
|
+
def associated_records_to_validate_or_save(association, new_record, autosave)
|
275
|
+
if new_record || custom_validation_context?
|
276
|
+
association && association.target
|
277
|
+
elsif autosave
|
278
|
+
association.target.find_all(&:changed_for_autosave?)
|
279
|
+
else
|
280
|
+
association.target.find_all(&:new_record?)
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
# go through nested autosave associations that are loaded in memory (without loading
|
285
|
+
# any new ones), and return true if is changed for autosave
|
286
|
+
def nested_records_changed_for_autosave?
|
287
|
+
@_nested_records_changed_for_autosave_already_called ||= false
|
288
|
+
return false if @_nested_records_changed_for_autosave_already_called
|
289
|
+
begin
|
290
|
+
@_nested_records_changed_for_autosave_already_called = true
|
291
|
+
self.class._reflections.values.any? do |reflection|
|
292
|
+
if reflection.options[:autosave]
|
293
|
+
association = association_instance_get(reflection.name)
|
294
|
+
association && Array.wrap(association.target).any?(&:changed_for_autosave?)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
ensure
|
298
|
+
@_nested_records_changed_for_autosave_already_called = false
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
# Validate the association if <tt>:validate</tt> or <tt>:autosave</tt> is
|
303
|
+
# turned on for the association.
|
304
|
+
def validate_single_association(reflection)
|
305
|
+
association = association_instance_get(reflection.name)
|
306
|
+
record = association && association.reader
|
307
|
+
association_valid?(reflection, record) if record && (record.changed_for_autosave? || custom_validation_context?)
|
308
|
+
end
|
309
|
+
|
310
|
+
# Validate the associated records if <tt>:validate</tt> or
|
311
|
+
# <tt>:autosave</tt> is turned on for the association specified by
|
312
|
+
# +reflection+.
|
313
|
+
def validate_collection_association(reflection)
|
314
|
+
if association = association_instance_get(reflection.name)
|
315
|
+
if records = associated_records_to_validate_or_save(association, new_record?, reflection.options[:autosave])
|
316
|
+
records.each_with_index { |record, index| association_valid?(reflection, record, index) }
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
# Returns whether or not the association is valid and applies any errors to
|
322
|
+
# the parent, <tt>self</tt>, if it wasn't. Skips any <tt>:autosave</tt>
|
323
|
+
# enabled records if they're marked_for_destruction? or destroyed.
|
324
|
+
def association_valid?(reflection, record, index = nil)
|
325
|
+
return true if record.destroyed? || (reflection.options[:autosave] && record.marked_for_destruction?)
|
326
|
+
|
327
|
+
context = validation_context if custom_validation_context?
|
328
|
+
|
329
|
+
unless valid = record.valid?(context)
|
330
|
+
if reflection.options[:autosave]
|
331
|
+
indexed_attribute = !index.nil? && (reflection.options[:index_errors] || ActiveRecord::Base.index_nested_attribute_errors)
|
332
|
+
|
333
|
+
record.errors.each do |attribute, message|
|
334
|
+
attribute = normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)
|
335
|
+
errors[attribute] << message
|
336
|
+
errors[attribute].uniq!
|
337
|
+
end
|
338
|
+
|
339
|
+
record.errors.details.each_key do |attribute|
|
340
|
+
reflection_attribute =
|
341
|
+
normalize_reflection_attribute(indexed_attribute, reflection, index, attribute).to_sym
|
342
|
+
|
343
|
+
record.errors.details[attribute].each do |error|
|
344
|
+
errors.details[reflection_attribute] << error
|
345
|
+
errors.details[reflection_attribute].uniq!
|
346
|
+
end
|
347
|
+
end
|
348
|
+
else
|
349
|
+
errors.add(reflection.name)
|
350
|
+
end
|
351
|
+
end
|
352
|
+
valid
|
353
|
+
end
|
354
|
+
|
355
|
+
def normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)
|
356
|
+
if indexed_attribute
|
357
|
+
"#{reflection.name}[#{index}].#{attribute}"
|
358
|
+
else
|
359
|
+
"#{reflection.name}.#{attribute}"
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
# Is used as a before_save callback to check while saving a collection
|
364
|
+
# association whether or not the parent was a new record before saving.
|
365
|
+
def before_save_collection_association
|
366
|
+
@new_record_before_save = new_record?
|
367
|
+
end
|
368
|
+
|
369
|
+
def after_save_collection_association
|
370
|
+
@new_record_before_save = false
|
371
|
+
end
|
372
|
+
|
373
|
+
# Saves any new associated records, or all loaded autosave associations if
|
374
|
+
# <tt>:autosave</tt> is enabled on the association.
|
375
|
+
#
|
376
|
+
# In addition, it destroys all children that were marked for destruction
|
377
|
+
# with #mark_for_destruction.
|
378
|
+
#
|
379
|
+
# This all happens inside a transaction, _if_ the Transactions module is included into
|
380
|
+
# ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
|
381
|
+
def save_collection_association(reflection)
|
382
|
+
if association = association_instance_get(reflection.name)
|
383
|
+
autosave = reflection.options[:autosave]
|
384
|
+
|
385
|
+
# By saving the instance variable in a local variable,
|
386
|
+
# we make the whole callback re-entrant.
|
387
|
+
new_record_before_save = @new_record_before_save
|
388
|
+
|
389
|
+
# reconstruct the scope now that we know the owner's id
|
390
|
+
association.reset_scope
|
391
|
+
|
392
|
+
if records = associated_records_to_validate_or_save(association, new_record_before_save, autosave)
|
393
|
+
if autosave
|
394
|
+
records_to_destroy = records.select(&:marked_for_destruction?)
|
395
|
+
records_to_destroy.each { |record| association.destroy(record) }
|
396
|
+
records -= records_to_destroy
|
397
|
+
end
|
398
|
+
|
399
|
+
records.each do |record|
|
400
|
+
next if record.destroyed?
|
401
|
+
|
402
|
+
saved = true
|
403
|
+
|
404
|
+
if autosave != false && (new_record_before_save || record.new_record?)
|
405
|
+
if autosave
|
406
|
+
saved = association.insert_record(record, false)
|
407
|
+
elsif !reflection.nested?
|
408
|
+
association_saved = association.insert_record(record)
|
409
|
+
|
410
|
+
if reflection.validate?
|
411
|
+
errors.add(reflection.name) unless association_saved
|
412
|
+
saved = association_saved
|
413
|
+
end
|
414
|
+
end
|
415
|
+
elsif autosave
|
416
|
+
saved = record.save(validate: false)
|
417
|
+
end
|
418
|
+
|
419
|
+
raise(RecordInvalid.new(association.owner)) unless saved
|
420
|
+
end
|
421
|
+
end
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
# Saves the associated record if it's new or <tt>:autosave</tt> is enabled
|
426
|
+
# on the association.
|
427
|
+
#
|
428
|
+
# In addition, it will destroy the association if it was marked for
|
429
|
+
# destruction with #mark_for_destruction.
|
430
|
+
#
|
431
|
+
# This all happens inside a transaction, _if_ the Transactions module is included into
|
432
|
+
# ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
|
433
|
+
def save_has_one_association(reflection)
|
434
|
+
association = association_instance_get(reflection.name)
|
435
|
+
record = association && association.load_target
|
436
|
+
|
437
|
+
if record && !record.destroyed?
|
438
|
+
autosave = reflection.options[:autosave]
|
439
|
+
|
440
|
+
if autosave && record.marked_for_destruction?
|
441
|
+
record.destroy
|
442
|
+
elsif autosave != false
|
443
|
+
key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id
|
444
|
+
|
445
|
+
if (autosave && record.changed_for_autosave?) || new_record? || record_changed?(reflection, record, key)
|
446
|
+
unless reflection.through_reflection
|
447
|
+
record[reflection.foreign_key] = key
|
448
|
+
if inverse_reflection = reflection.inverse_of
|
449
|
+
record.association(inverse_reflection.name).loaded!
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
saved = record.save(validate: !autosave)
|
454
|
+
raise ActiveRecord::Rollback if !saved && autosave
|
455
|
+
saved
|
456
|
+
end
|
457
|
+
end
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
# If the record is new or it has changed, returns true.
|
462
|
+
def record_changed?(reflection, record, key)
|
463
|
+
record.new_record? ||
|
464
|
+
association_foreign_key_changed?(reflection, record, key) ||
|
465
|
+
record.will_save_change_to_attribute?(reflection.foreign_key)
|
466
|
+
end
|
467
|
+
|
468
|
+
def association_foreign_key_changed?(reflection, record, key)
|
469
|
+
return false if reflection.through_reflection?
|
470
|
+
|
471
|
+
record.has_attribute?(reflection.foreign_key) && record[reflection.foreign_key] != key
|
472
|
+
end
|
473
|
+
|
474
|
+
# Saves the associated record if it's new or <tt>:autosave</tt> is enabled.
|
475
|
+
#
|
476
|
+
# In addition, it will destroy the association if it was marked for destruction.
|
477
|
+
def save_belongs_to_association(reflection)
|
478
|
+
association = association_instance_get(reflection.name)
|
479
|
+
return unless association && association.loaded? && !association.stale_target?
|
480
|
+
|
481
|
+
record = association.load_target
|
482
|
+
if record && !record.destroyed?
|
483
|
+
autosave = reflection.options[:autosave]
|
484
|
+
|
485
|
+
if autosave && record.marked_for_destruction?
|
486
|
+
self[reflection.foreign_key] = nil
|
487
|
+
record.destroy
|
488
|
+
elsif autosave != false
|
489
|
+
saved = record.save(validate: !autosave) if record.new_record? || (autosave && record.changed_for_autosave?)
|
490
|
+
|
491
|
+
if association.updated?
|
492
|
+
association_id = record.send(reflection.options[:primary_key] || :id)
|
493
|
+
self[reflection.foreign_key] = association_id
|
494
|
+
association.loaded!
|
495
|
+
end
|
496
|
+
|
497
|
+
saved if autosave
|
498
|
+
end
|
499
|
+
end
|
500
|
+
end
|
501
|
+
|
502
|
+
def custom_validation_context?
|
503
|
+
validation_context && [:create, :update].exclude?(validation_context)
|
504
|
+
end
|
505
|
+
|
506
|
+
def _ensure_no_duplicate_errors
|
507
|
+
errors.messages.each_key do |attribute|
|
508
|
+
errors[attribute].uniq!
|
509
|
+
end
|
510
|
+
end
|
511
|
+
end
|
512
|
+
end
|