activerecord 3.2.6 → 6.0.0
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 +611 -6417
- data/MIT-LICENSE +4 -2
- data/README.rdoc +44 -47
- data/examples/performance.rb +79 -71
- data/examples/simple.rb +6 -5
- data/lib/active_record/aggregations.rb +268 -238
- data/lib/active_record/association_relation.rb +40 -0
- data/lib/active_record/associations/alias_tracker.rb +47 -42
- data/lib/active_record/associations/association.rb +173 -81
- data/lib/active_record/associations/association_scope.rb +124 -92
- data/lib/active_record/associations/belongs_to_association.rb +83 -38
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +11 -9
- data/lib/active_record/associations/builder/association.rb +113 -32
- data/lib/active_record/associations/builder/belongs_to.rb +105 -60
- data/lib/active_record/associations/builder/collection_association.rb +53 -56
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +98 -41
- data/lib/active_record/associations/builder/has_many.rb +11 -63
- data/lib/active_record/associations/builder/has_one.rb +47 -45
- data/lib/active_record/associations/builder/singular_association.rb +30 -18
- data/lib/active_record/associations/collection_association.rb +217 -295
- data/lib/active_record/associations/collection_proxy.rb +1074 -77
- data/lib/active_record/associations/foreign_association.rb +20 -0
- data/lib/active_record/associations/has_many_association.rb +78 -50
- data/lib/active_record/associations/has_many_through_association.rb +99 -61
- data/lib/active_record/associations/has_one_association.rb +75 -30
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +45 -119
- data/lib/active_record/associations/join_dependency/join_base.rb +11 -12
- data/lib/active_record/associations/join_dependency/join_part.rb +35 -42
- data/lib/active_record/associations/join_dependency.rb +208 -164
- data/lib/active_record/associations/preloader/association.rb +93 -87
- data/lib/active_record/associations/preloader/through_association.rb +87 -38
- data/lib/active_record/associations/preloader.rb +134 -110
- data/lib/active_record/associations/singular_association.rb +19 -24
- data/lib/active_record/associations/through_association.rb +61 -27
- data/lib/active_record/associations.rb +1766 -1505
- data/lib/active_record/attribute_assignment.rb +57 -193
- data/lib/active_record/attribute_decorators.rb +90 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +58 -8
- data/lib/active_record/attribute_methods/dirty.rb +187 -67
- data/lib/active_record/attribute_methods/primary_key.rb +100 -78
- data/lib/active_record/attribute_methods/query.rb +10 -8
- data/lib/active_record/attribute_methods/read.rb +29 -118
- data/lib/active_record/attribute_methods/serialization.rb +60 -72
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -42
- data/lib/active_record/attribute_methods/write.rb +36 -44
- data/lib/active_record/attribute_methods.rb +306 -161
- data/lib/active_record/attributes.rb +279 -0
- data/lib/active_record/autosave_association.rb +324 -238
- data/lib/active_record/base.rb +114 -507
- data/lib/active_record/callbacks.rb +147 -83
- data/lib/active_record/coders/json.rb +15 -0
- data/lib/active_record/coders/yaml_column.rb +32 -23
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +962 -279
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +32 -5
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +331 -209
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -23
- data/lib/active_record/connection_adapters/abstract/quoting.rb +201 -65
- 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 +510 -289
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +93 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1182 -313
- data/lib/active_record/connection_adapters/abstract/transaction.rb +323 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +585 -120
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +610 -463
- data/lib/active_record/connection_adapters/column.rb +58 -233
- 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 +200 -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 +75 -207
- data/lib/active_record/connection_adapters/postgresql/column.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +182 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -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/oid.rb +34 -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 +695 -1052
- data/lib/active_record/connection_adapters/schema_cache.rb +115 -24
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +118 -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 +528 -26
- data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
- data/lib/active_record/connection_handling.rb +267 -0
- data/lib/active_record/core.rb +599 -0
- data/lib/active_record/counter_cache.rb +177 -103
- 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/database_configurations.rb +233 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +107 -64
- data/lib/active_record/enum.rb +274 -0
- data/lib/active_record/errors.rb +254 -61
- data/lib/active_record/explain.rb +35 -70
- data/lib/active_record/explain_registry.rb +32 -0
- data/lib/active_record/explain_subscriber.rb +18 -8
- 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 +291 -475
- data/lib/active_record/gem_version.rb +17 -0
- data/lib/active_record/inheritance.rb +219 -100
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +175 -17
- 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 +9 -1
- data/lib/active_record/locking/optimistic.rb +106 -92
- data/lib/active_record/locking/pessimistic.rb +23 -11
- data/lib/active_record/log_subscriber.rb +80 -30
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/migration/command_recorder.rb +235 -56
- data/lib/active_record/migration/compatibility.rb +244 -0
- data/lib/active_record/migration/join_table.rb +17 -0
- data/lib/active_record/migration.rb +917 -301
- data/lib/active_record/model_schema.rb +351 -175
- data/lib/active_record/nested_attributes.rb +366 -235
- data/lib/active_record/no_touching.rb +65 -0
- data/lib/active_record/null_relation.rb +68 -0
- data/lib/active_record/persistence.rb +761 -166
- data/lib/active_record/query_cache.rb +22 -44
- data/lib/active_record/querying.rb +55 -31
- data/lib/active_record/railtie.rb +185 -47
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/console_sandbox.rb +5 -4
- data/lib/active_record/railties/controller_runtime.rb +35 -33
- data/lib/active_record/railties/databases.rake +366 -463
- data/lib/active_record/readonly_attributes.rb +4 -6
- data/lib/active_record/reflection.rb +736 -228
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/batches.rb +252 -52
- data/lib/active_record/relation/calculations.rb +340 -270
- data/lib/active_record/relation/delegation.rb +117 -36
- data/lib/active_record/relation/finder_methods.rb +439 -286
- 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/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/predicate_builder.rb +131 -39
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +1163 -221
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +49 -120
- 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/relation.rb +671 -349
- data/lib/active_record/result.rb +149 -15
- data/lib/active_record/runtime_registry.rb +24 -0
- data/lib/active_record/sanitization.rb +153 -133
- data/lib/active_record/schema.rb +22 -19
- data/lib/active_record/schema_dumper.rb +178 -112
- data/lib/active_record/schema_migration.rb +60 -0
- data/lib/active_record/scoping/default.rb +107 -98
- data/lib/active_record/scoping/named.rb +130 -115
- data/lib/active_record/scoping.rb +77 -123
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +10 -6
- data/lib/active_record/statement_cache.rb +148 -0
- data/lib/active_record/store.rb +256 -16
- 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 +93 -39
- data/lib/active_record/touch_later.rb +66 -0
- data/lib/active_record/transactions.rb +260 -129
- data/lib/active_record/translation.rb +3 -1
- 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.rb +78 -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/type_caster.rb +9 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +35 -18
- 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 +123 -77
- data/lib/active_record/validations.rb +54 -43
- data/lib/active_record/version.rb +7 -7
- data/lib/active_record.rb +97 -49
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes/attribute.rb +37 -0
- data/lib/arel/attributes.rb +22 -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/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/nodes.rb +68 -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/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/visitors.rb +20 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +51 -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/migration_generator.rb +59 -9
- 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/migration.rb +41 -8
- data/lib/rails/generators/active_record/model/model_generator.rb +24 -22
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +1 -1
- data/lib/rails/generators/active_record.rb +10 -16
- metadata +285 -149
- data/examples/associations.png +0 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
- data/lib/active_record/associations/join_helper.rb +0 -55
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -24
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
- data/lib/active_record/associations/preloader/has_one.rb +0 -23
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -21
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -188
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -426
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -579
- data/lib/active_record/dynamic_finder_match.rb +0 -68
- data/lib/active_record/dynamic_scope_match.rb +0 -23
- data/lib/active_record/fixtures/file.rb +0 -65
- data/lib/active_record/identity_map.rb +0 -162
- data/lib/active_record/observer.rb +0 -121
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -203
- data/lib/active_record/session_store.rb +0 -358
- data/lib/active_record/test_case.rb +0 -73
- data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
- data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,13 +1,13 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
|
-
# = Active Record Callbacks
|
4
|
+
# = Active Record \Callbacks
|
5
5
|
#
|
6
|
-
# Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic
|
6
|
+
# \Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic
|
7
7
|
# before or after an alteration of the object state. This can be used to make sure that associated and
|
8
|
-
# dependent objects are deleted when
|
9
|
-
# before they're validated (by overwriting +before_validation+).
|
10
|
-
# the
|
8
|
+
# dependent objects are deleted when {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] is called (by overwriting +before_destroy+) or
|
9
|
+
# to massage attributes before they're validated (by overwriting +before_validation+).
|
10
|
+
# As an example of the callbacks initiated, consider the {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] call for a new record:
|
11
11
|
#
|
12
12
|
# * (-) <tt>save</tt>
|
13
13
|
# * (-) <tt>valid</tt>
|
@@ -22,22 +22,25 @@ module ActiveRecord
|
|
22
22
|
# * (7) <tt>after_commit</tt>
|
23
23
|
#
|
24
24
|
# Also, an <tt>after_rollback</tt> callback can be configured to be triggered whenever a rollback is issued.
|
25
|
-
# Check out
|
25
|
+
# Check out ActiveRecord::Transactions for more details about <tt>after_commit</tt> and
|
26
26
|
# <tt>after_rollback</tt>.
|
27
27
|
#
|
28
|
-
#
|
28
|
+
# Additionally, an <tt>after_touch</tt> callback is triggered whenever an
|
29
|
+
# object is touched.
|
30
|
+
#
|
31
|
+
# Lastly an <tt>after_find</tt> and <tt>after_initialize</tt> callback is triggered for each object that
|
29
32
|
# is found and instantiated by a finder, with <tt>after_initialize</tt> being triggered after new objects
|
30
33
|
# are instantiated as well.
|
31
34
|
#
|
32
|
-
#
|
33
|
-
# Active Record life cycle. The sequence for calling
|
35
|
+
# There are nineteen callbacks in total, which give you immense power to react and prepare for each state in the
|
36
|
+
# Active Record life cycle. The sequence for calling {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] for an existing record is similar,
|
34
37
|
# except that each <tt>_create</tt> callback is replaced by the corresponding <tt>_update</tt> callback.
|
35
38
|
#
|
36
39
|
# Examples:
|
37
40
|
# class CreditCard < ActiveRecord::Base
|
38
41
|
# # Strip everything but digits, so the user can specify "555 234 34" or
|
39
|
-
# # "5552-3434"
|
40
|
-
# before_validation(:
|
42
|
+
# # "5552-3434" and both will mean "55523434"
|
43
|
+
# before_validation(on: :create) do
|
41
44
|
# self.number = number.gsub(/[^0-9]/, "") if attribute_present?("number")
|
42
45
|
# end
|
43
46
|
# end
|
@@ -52,9 +55,9 @@ module ActiveRecord
|
|
52
55
|
# end
|
53
56
|
#
|
54
57
|
# class Firm < ActiveRecord::Base
|
55
|
-
# #
|
56
|
-
# before_destroy { |record| Person.
|
57
|
-
# before_destroy { |record| Client.
|
58
|
+
# # Disables access to the system, for associated clients and people when the firm is destroyed
|
59
|
+
# before_destroy { |record| Person.where(firm_id: record.id).update_all(access: 'disabled') }
|
60
|
+
# before_destroy { |record| Client.where(client_of: record.id).update_all(access: 'disabled') }
|
58
61
|
# end
|
59
62
|
#
|
60
63
|
# == Inheritable callback queues
|
@@ -72,21 +75,7 @@ module ActiveRecord
|
|
72
75
|
# end
|
73
76
|
#
|
74
77
|
# Now, when <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is
|
75
|
-
# run, both +destroy_author+ and +destroy_readers+ are called.
|
76
|
-
# where the +before_destroy+ method is overridden:
|
77
|
-
#
|
78
|
-
# class Topic < ActiveRecord::Base
|
79
|
-
# def before_destroy() destroy_author end
|
80
|
-
# end
|
81
|
-
#
|
82
|
-
# class Reply < Topic
|
83
|
-
# def before_destroy() destroy_readers end
|
84
|
-
# end
|
85
|
-
#
|
86
|
-
# In that case, <tt>Reply#destroy</tt> would only run +destroy_readers+ and _not_ +destroy_author+.
|
87
|
-
# So, use the callback macros when you want to ensure that a certain callback is called for the entire
|
88
|
-
# hierarchy, and use the regular overwriteable methods when you want to leave it up to each descendant
|
89
|
-
# to decide whether they want to call +super+ and trigger the inherited callbacks.
|
78
|
+
# run, both +destroy_author+ and +destroy_readers+ are called.
|
90
79
|
#
|
91
80
|
# *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the
|
92
81
|
# callbacks before specifying the associations. Otherwise, you might trigger the loading of a
|
@@ -95,9 +84,9 @@ module ActiveRecord
|
|
95
84
|
# == Types of callbacks
|
96
85
|
#
|
97
86
|
# There are four types of callbacks accepted by the callback macros: Method references (symbol), callback objects,
|
98
|
-
# inline methods (using a proc)
|
87
|
+
# inline methods (using a proc). Method references and callback objects
|
99
88
|
# are the recommended approaches, inline methods using a proc are sometimes appropriate (such as for
|
100
|
-
# creating mix-ins)
|
89
|
+
# creating mix-ins).
|
101
90
|
#
|
102
91
|
# The method reference callbacks work by specifying a protected or private method available in the object, like this:
|
103
92
|
#
|
@@ -106,7 +95,7 @@ module ActiveRecord
|
|
106
95
|
#
|
107
96
|
# private
|
108
97
|
# def delete_parents
|
109
|
-
# self.class.
|
98
|
+
# self.class.delete_by(parent_id: id)
|
110
99
|
# end
|
111
100
|
# end
|
112
101
|
#
|
@@ -127,7 +116,7 @@ module ActiveRecord
|
|
127
116
|
# record.credit_card_number = decrypt(record.credit_card_number)
|
128
117
|
# end
|
129
118
|
#
|
130
|
-
# alias_method :
|
119
|
+
# alias_method :after_initialize, :after_save
|
131
120
|
#
|
132
121
|
# private
|
133
122
|
# def encrypt(value)
|
@@ -139,7 +128,7 @@ module ActiveRecord
|
|
139
128
|
# end
|
140
129
|
# end
|
141
130
|
#
|
142
|
-
# So you specify the object you want messaged on a given callback. When that callback is triggered, the object has
|
131
|
+
# So you specify the object you want to be messaged on a given callback. When that callback is triggered, the object has
|
143
132
|
# a method by the name of the callback messaged. You can make these callbacks more flexible by passing in other
|
144
133
|
# initialization data such as the name of the attribute to work with:
|
145
134
|
#
|
@@ -162,7 +151,7 @@ module ActiveRecord
|
|
162
151
|
# record.send("#{@attribute}=", decrypt(record.send("#{@attribute}")))
|
163
152
|
# end
|
164
153
|
#
|
165
|
-
# alias_method :
|
154
|
+
# alias_method :after_initialize, :after_save
|
166
155
|
#
|
167
156
|
# private
|
168
157
|
# def encrypt(value)
|
@@ -174,64 +163,135 @@ module ActiveRecord
|
|
174
163
|
# end
|
175
164
|
# end
|
176
165
|
#
|
177
|
-
#
|
178
|
-
#
|
166
|
+
# == <tt>before_validation*</tt> returning statements
|
167
|
+
#
|
168
|
+
# If the +before_validation+ callback throws +:abort+, the process will be
|
169
|
+
# aborted and {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] will return +false+.
|
170
|
+
# If {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] is called it will raise an ActiveRecord::RecordInvalid exception.
|
171
|
+
# Nothing will be appended to the errors object.
|
172
|
+
#
|
173
|
+
# == Canceling callbacks
|
174
|
+
#
|
175
|
+
# If a <tt>before_*</tt> callback throws +:abort+, all the later callbacks and
|
176
|
+
# the associated action are cancelled.
|
177
|
+
# Callbacks are generally run in the order they are defined, with the exception of callbacks defined as
|
178
|
+
# methods on the model, which are called last.
|
179
|
+
#
|
180
|
+
# == Ordering callbacks
|
181
|
+
#
|
182
|
+
# Sometimes the code needs that the callbacks execute in a specific order. For example, a +before_destroy+
|
183
|
+
# callback (+log_children+ in this case) should be executed before the children get destroyed by the
|
184
|
+
# <tt>dependent: :destroy</tt> option.
|
185
|
+
#
|
186
|
+
# Let's look at the code below:
|
179
187
|
#
|
180
188
|
# class Topic < ActiveRecord::Base
|
181
|
-
#
|
189
|
+
# has_many :children, dependent: :destroy
|
190
|
+
#
|
191
|
+
# before_destroy :log_children
|
192
|
+
#
|
193
|
+
# private
|
194
|
+
# def log_children
|
195
|
+
# # Child processing
|
196
|
+
# end
|
182
197
|
# end
|
183
198
|
#
|
184
|
-
#
|
185
|
-
#
|
199
|
+
# In this case, the problem is that when the +before_destroy+ callback is executed, the children are not available
|
200
|
+
# because the {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] callback gets executed first.
|
201
|
+
# You can use the +prepend+ option on the +before_destroy+ callback to avoid this.
|
186
202
|
#
|
187
203
|
# class Topic < ActiveRecord::Base
|
188
|
-
#
|
189
|
-
#
|
204
|
+
# has_many :children, dependent: :destroy
|
205
|
+
#
|
206
|
+
# before_destroy :log_children, prepend: true
|
207
|
+
#
|
208
|
+
# private
|
209
|
+
# def log_children
|
210
|
+
# # Child processing
|
211
|
+
# end
|
190
212
|
# end
|
191
213
|
#
|
192
|
-
#
|
214
|
+
# This way, the +before_destroy+ gets executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
|
193
215
|
#
|
194
|
-
#
|
195
|
-
#
|
196
|
-
# ActiveRecord::RecordInvalid exception. Nothing will be appended to the errors object.
|
216
|
+
# Also, there are cases when you want several callbacks of the same type to
|
217
|
+
# be executed in order.
|
197
218
|
#
|
198
|
-
#
|
219
|
+
# For example:
|
199
220
|
#
|
200
|
-
#
|
201
|
-
#
|
202
|
-
#
|
203
|
-
#
|
221
|
+
# class Topic < ActiveRecord::Base
|
222
|
+
# has_many :children
|
223
|
+
#
|
224
|
+
# after_save :log_children
|
225
|
+
# after_save :do_something_else
|
226
|
+
#
|
227
|
+
# private
|
228
|
+
#
|
229
|
+
# def log_children
|
230
|
+
# # Child processing
|
231
|
+
# end
|
204
232
|
#
|
205
|
-
#
|
233
|
+
# def do_something_else
|
234
|
+
# # Something else
|
235
|
+
# end
|
236
|
+
# end
|
206
237
|
#
|
207
|
-
#
|
208
|
-
#
|
209
|
-
#
|
238
|
+
# In this case the +log_children+ gets executed before +do_something_else+.
|
239
|
+
# The same applies to all non-transactional callbacks.
|
240
|
+
#
|
241
|
+
# In case there are multiple transactional callbacks as seen below, the order
|
242
|
+
# is reversed.
|
243
|
+
#
|
244
|
+
# For example:
|
245
|
+
#
|
246
|
+
# class Topic < ActiveRecord::Base
|
247
|
+
# has_many :children
|
248
|
+
#
|
249
|
+
# after_commit :log_children
|
250
|
+
# after_commit :do_something_else
|
251
|
+
#
|
252
|
+
# private
|
253
|
+
#
|
254
|
+
# def log_children
|
255
|
+
# # Child processing
|
256
|
+
# end
|
257
|
+
#
|
258
|
+
# def do_something_else
|
259
|
+
# # Something else
|
260
|
+
# end
|
261
|
+
# end
|
262
|
+
#
|
263
|
+
# In this case the +do_something_else+ gets executed before +log_children+.
|
264
|
+
#
|
265
|
+
# == \Transactions
|
266
|
+
#
|
267
|
+
# The entire callback chain of a {#save}[rdoc-ref:Persistence#save], {#save!}[rdoc-ref:Persistence#save!],
|
268
|
+
# or {#destroy}[rdoc-ref:Persistence#destroy] call runs within a transaction. That includes <tt>after_*</tt> hooks.
|
269
|
+
# If everything goes fine a COMMIT is executed once the chain has been completed.
|
210
270
|
#
|
211
271
|
# If a <tt>before_*</tt> callback cancels the action a ROLLBACK is issued. You
|
212
272
|
# can also trigger a ROLLBACK raising an exception in any of the callbacks,
|
213
273
|
# including <tt>after_*</tt> hooks. Note, however, that in that case the client
|
214
|
-
# needs to be aware of it because an ordinary
|
274
|
+
# needs to be aware of it because an ordinary {#save}[rdoc-ref:Persistence#save] will raise such exception
|
215
275
|
# instead of quietly returning +false+.
|
216
276
|
#
|
217
277
|
# == Debugging callbacks
|
218
|
-
#
|
219
|
-
# The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object.
|
278
|
+
#
|
279
|
+
# The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object. Active Model \Callbacks support
|
220
280
|
# <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt> property
|
221
281
|
# defines what part of the chain the callback runs in.
|
222
|
-
#
|
223
|
-
# To find all callbacks in the before_save callback chain:
|
224
|
-
#
|
282
|
+
#
|
283
|
+
# To find all callbacks in the before_save callback chain:
|
284
|
+
#
|
225
285
|
# Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }
|
226
|
-
#
|
286
|
+
#
|
227
287
|
# Returns an array of callback objects that form the before_save chain.
|
228
|
-
#
|
288
|
+
#
|
229
289
|
# To further check if the before_save chain contains a proc defined as <tt>rest_when_dead</tt> use the <tt>filter</tt> property of the callback object:
|
230
|
-
#
|
290
|
+
#
|
231
291
|
# Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).include?(:rest_when_dead)
|
232
|
-
#
|
292
|
+
#
|
233
293
|
# Returns true or false depending on whether the proc is contained in the before_save callback chain on a Topic model.
|
234
|
-
#
|
294
|
+
#
|
235
295
|
module Callbacks
|
236
296
|
extend ActiveSupport::Concern
|
237
297
|
|
@@ -242,34 +302,38 @@ module ActiveRecord
|
|
242
302
|
:before_destroy, :around_destroy, :after_destroy, :after_commit, :after_rollback
|
243
303
|
]
|
244
304
|
|
245
|
-
included do
|
246
|
-
extend ActiveModel::Callbacks
|
247
|
-
include ActiveModel::Validations::Callbacks
|
248
|
-
|
249
|
-
define_model_callbacks :initialize, :find, :touch, :only => :after
|
250
|
-
define_model_callbacks :save, :create, :update, :destroy
|
251
|
-
end
|
252
|
-
|
253
305
|
def destroy #:nodoc:
|
254
|
-
|
306
|
+
@_destroy_callback_already_called ||= false
|
307
|
+
return if @_destroy_callback_already_called
|
308
|
+
@_destroy_callback_already_called = true
|
309
|
+
_run_destroy_callbacks { super }
|
310
|
+
rescue RecordNotDestroyed => e
|
311
|
+
@_association_destroy_exception = e
|
312
|
+
false
|
313
|
+
ensure
|
314
|
+
@_destroy_callback_already_called = false
|
255
315
|
end
|
256
316
|
|
257
317
|
def touch(*) #:nodoc:
|
258
|
-
|
318
|
+
_run_touch_callbacks { super }
|
319
|
+
end
|
320
|
+
|
321
|
+
def increment!(attribute, by = 1, touch: nil) # :nodoc:
|
322
|
+
touch ? _run_touch_callbacks { super } : super
|
259
323
|
end
|
260
324
|
|
261
325
|
private
|
262
326
|
|
263
|
-
def create_or_update
|
264
|
-
|
327
|
+
def create_or_update(**)
|
328
|
+
_run_save_callbacks { super }
|
265
329
|
end
|
266
330
|
|
267
|
-
def
|
268
|
-
|
331
|
+
def _create_record
|
332
|
+
_run_create_callbacks { super }
|
269
333
|
end
|
270
334
|
|
271
|
-
def
|
272
|
-
|
335
|
+
def _update_record
|
336
|
+
_run_update_callbacks { super }
|
273
337
|
end
|
274
338
|
end
|
275
339
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Coders # :nodoc:
|
5
|
+
class JSON # :nodoc:
|
6
|
+
def self.dump(obj)
|
7
|
+
ActiveSupport::JSON.encode(obj)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.load(json)
|
11
|
+
ActiveSupport::JSON.decode(json) unless json.blank?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,41 +1,50 @@
|
|
1
|
-
|
2
|
-
# :stopdoc:
|
3
|
-
module Coders
|
4
|
-
class YAMLColumn
|
5
|
-
RESCUE_ERRORS = [ ArgumentError ]
|
1
|
+
# frozen_string_literal: true
|
6
2
|
|
7
|
-
|
8
|
-
RESCUE_ERRORS << Psych::SyntaxError
|
9
|
-
end
|
3
|
+
require "yaml"
|
10
4
|
|
5
|
+
module ActiveRecord
|
6
|
+
module Coders # :nodoc:
|
7
|
+
class YAMLColumn # :nodoc:
|
11
8
|
attr_accessor :object_class
|
12
9
|
|
13
|
-
def initialize(object_class = Object)
|
10
|
+
def initialize(attr_name, object_class = Object)
|
11
|
+
@attr_name = attr_name
|
14
12
|
@object_class = object_class
|
13
|
+
check_arity_of_constructor
|
15
14
|
end
|
16
15
|
|
17
16
|
def dump(obj)
|
17
|
+
return if obj.nil?
|
18
|
+
|
19
|
+
assert_valid_value(obj, action: "dump")
|
18
20
|
YAML.dump obj
|
19
21
|
end
|
20
22
|
|
21
23
|
def load(yaml)
|
22
24
|
return object_class.new if object_class != Object && yaml.nil?
|
23
|
-
return yaml unless yaml.is_a?(String) && yaml
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
return yaml unless yaml.is_a?(String) && /^---/.match?(yaml)
|
26
|
+
obj = YAML.load(yaml)
|
27
|
+
|
28
|
+
assert_valid_value(obj, action: "load")
|
29
|
+
obj ||= object_class.new if object_class != Object
|
30
|
+
|
31
|
+
obj
|
32
|
+
end
|
33
|
+
|
34
|
+
def assert_valid_value(obj, action:)
|
35
|
+
unless obj.nil? || obj.is_a?(object_class)
|
36
|
+
raise SerializationTypeMismatch,
|
37
|
+
"can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
|
36
38
|
end
|
37
39
|
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def check_arity_of_constructor
|
44
|
+
load(nil)
|
45
|
+
rescue ArgumentError
|
46
|
+
raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
|
47
|
+
end
|
38
48
|
end
|
39
49
|
end
|
40
|
-
# :startdoc
|
41
50
|
end
|