activerecord 4.2.9 → 6.1.4.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 +5 -5
- data/CHANGELOG.md +964 -1382
- data/MIT-LICENSE +4 -2
- data/README.rdoc +15 -14
- data/examples/performance.rb +33 -32
- data/examples/simple.rb +5 -4
- data/lib/active_record/aggregations.rb +266 -251
- data/lib/active_record/association_relation.rb +40 -15
- data/lib/active_record/associations/alias_tracker.rb +40 -43
- data/lib/active_record/associations/association.rb +162 -69
- data/lib/active_record/associations/association_scope.rb +105 -130
- data/lib/active_record/associations/belongs_to_association.rb +83 -65
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +13 -12
- data/lib/active_record/associations/builder/association.rb +57 -43
- data/lib/active_record/associations/builder/belongs_to.rb +74 -57
- data/lib/active_record/associations/builder/collection_association.rb +15 -37
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +49 -66
- data/lib/active_record/associations/builder/has_many.rb +13 -5
- data/lib/active_record/associations/builder/has_one.rb +44 -6
- data/lib/active_record/associations/builder/singular_association.rb +16 -10
- data/lib/active_record/associations/collection_association.rb +148 -287
- data/lib/active_record/associations/collection_proxy.rb +252 -150
- data/lib/active_record/associations/foreign_association.rb +23 -1
- data/lib/active_record/associations/has_many_association.rb +56 -98
- data/lib/active_record/associations/has_many_through_association.rb +68 -89
- data/lib/active_record/associations/has_one_association.rb +73 -47
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +54 -81
- data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +14 -14
- data/lib/active_record/associations/join_dependency.rb +174 -169
- data/lib/active_record/associations/preloader/association.rb +108 -115
- data/lib/active_record/associations/preloader/through_association.rb +85 -65
- data/lib/active_record/associations/preloader.rb +97 -94
- data/lib/active_record/associations/singular_association.rb +18 -39
- data/lib/active_record/associations/through_association.rb +39 -19
- data/lib/active_record/associations.rb +1845 -1598
- data/lib/active_record/attribute_assignment.rb +59 -185
- data/lib/active_record/attribute_methods/before_type_cast.rb +18 -10
- data/lib/active_record/attribute_methods/dirty.rb +168 -148
- data/lib/active_record/attribute_methods/primary_key.rb +93 -83
- data/lib/active_record/attribute_methods/query.rb +8 -10
- data/lib/active_record/attribute_methods/read.rb +19 -79
- data/lib/active_record/attribute_methods/serialization.rb +49 -24
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +55 -36
- data/lib/active_record/attribute_methods/write.rb +24 -55
- data/lib/active_record/attribute_methods.rb +149 -154
- data/lib/active_record/attributes.rb +234 -78
- data/lib/active_record/autosave_association.rb +133 -60
- data/lib/active_record/base.rb +46 -46
- data/lib/active_record/callbacks.rb +234 -79
- data/lib/active_record/coders/json.rb +3 -1
- data/lib/active_record/coders/yaml_column.rb +34 -13
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +887 -323
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +17 -41
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +292 -124
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +78 -24
- data/lib/active_record/connection_adapters/abstract/quoting.rb +177 -60
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +8 -6
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +157 -93
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +473 -255
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +79 -36
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +869 -286
- data/lib/active_record/connection_adapters/abstract/transaction.rb +257 -91
- data/lib/active_record/connection_adapters/abstract_adapter.rb +483 -230
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +557 -640
- data/lib/active_record/connection_adapters/column.rb +67 -40
- data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +194 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +71 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +96 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +97 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +103 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +91 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +268 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +40 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +80 -192
- data/lib/active_record/connection_adapters/pool_config.rb +73 -0
- data/lib/active_record/connection_adapters/pool_manager.rb +47 -0
- data/lib/active_record/connection_adapters/postgresql/column.rb +44 -11
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +75 -160
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +49 -58
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +9 -8
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +8 -6
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +13 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +14 -19
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +5 -4
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +31 -20
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -9
- data/lib/active_record/connection_adapters/postgresql/oid/{infinity.rb → oid.rb} +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +32 -11
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +70 -34
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -5
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +58 -54
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +18 -4
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid.rb +25 -25
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +145 -48
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +27 -14
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +80 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +178 -108
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +49 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +496 -298
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +11 -8
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +588 -375
- data/lib/active_record/connection_adapters/schema_cache.rb +167 -29
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +45 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +144 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +102 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +21 -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 +170 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +322 -373
- data/lib/active_record/connection_adapters/statement_pool.rb +33 -13
- data/lib/active_record/connection_adapters.rb +52 -0
- data/lib/active_record/connection_handling.rb +314 -41
- data/lib/active_record/core.rb +458 -241
- data/lib/active_record/counter_cache.rb +70 -49
- data/lib/active_record/database_configurations/connection_url_resolver.rb +98 -0
- data/lib/active_record/database_configurations/database_config.rb +80 -0
- data/lib/active_record/database_configurations/hash_config.rb +96 -0
- data/lib/active_record/database_configurations/url_config.rb +53 -0
- data/lib/active_record/database_configurations.rb +272 -0
- data/lib/active_record/delegated_type.rb +209 -0
- data/lib/active_record/destroy_association_async_job.rb +36 -0
- data/lib/active_record/dynamic_matchers.rb +87 -106
- data/lib/active_record/enum.rb +211 -92
- data/lib/active_record/errors.rb +224 -54
- data/lib/active_record/explain.rb +27 -11
- data/lib/active_record/explain_registry.rb +4 -2
- data/lib/active_record/explain_subscriber.rb +10 -5
- data/lib/active_record/fixture_set/file.rb +33 -14
- data/lib/active_record/fixture_set/model_metadata.rb +32 -0
- data/lib/active_record/fixture_set/render_context.rb +17 -0
- data/lib/active_record/fixture_set/table_row.rb +152 -0
- data/lib/active_record/fixture_set/table_rows.rb +46 -0
- data/lib/active_record/fixtures.rb +275 -500
- data/lib/active_record/gem_version.rb +6 -4
- data/lib/active_record/inheritance.rb +175 -110
- data/lib/active_record/insert_all.rb +212 -0
- data/lib/active_record/integration.rb +121 -29
- data/lib/active_record/internal_metadata.rb +62 -0
- data/lib/active_record/legacy_yaml_adapter.rb +27 -5
- data/lib/active_record/locale/en.yml +3 -2
- data/lib/active_record/locking/optimistic.rb +98 -92
- data/lib/active_record/locking/pessimistic.rb +22 -6
- data/lib/active_record/log_subscriber.rb +93 -31
- data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector.rb +77 -0
- data/lib/active_record/migration/command_recorder.rb +185 -90
- data/lib/active_record/migration/compatibility.rb +295 -0
- data/lib/active_record/migration/join_table.rb +8 -7
- data/lib/active_record/migration.rb +673 -325
- data/lib/active_record/model_schema.rb +418 -113
- data/lib/active_record/nested_attributes.rb +263 -224
- data/lib/active_record/no_touching.rb +15 -2
- data/lib/active_record/null_relation.rb +24 -38
- data/lib/active_record/persistence.rb +572 -136
- data/lib/active_record/query_cache.rb +29 -23
- data/lib/active_record/querying.rb +50 -31
- data/lib/active_record/railtie.rb +170 -51
- data/lib/active_record/railties/console_sandbox.rb +3 -3
- data/lib/active_record/railties/controller_runtime.rb +34 -33
- data/lib/active_record/railties/databases.rake +523 -199
- data/lib/active_record/readonly_attributes.rb +9 -4
- data/lib/active_record/reflection.rb +454 -291
- data/lib/active_record/relation/batches/batch_enumerator.rb +85 -0
- data/lib/active_record/relation/batches.rb +217 -59
- data/lib/active_record/relation/calculations.rb +324 -249
- data/lib/active_record/relation/delegation.rb +76 -84
- data/lib/active_record/relation/finder_methods.rb +316 -242
- data/lib/active_record/relation/from_clause.rb +30 -0
- data/lib/active_record/relation/merger.rb +95 -103
- data/lib/active_record/relation/predicate_builder/array_handler.rb +26 -26
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +42 -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 +57 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +7 -1
- data/lib/active_record/relation/predicate_builder.rb +136 -122
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +757 -413
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +18 -20
- data/lib/active_record/relation/where_clause.rb +239 -0
- data/lib/active_record/relation.rb +554 -343
- data/lib/active_record/result.rb +91 -47
- data/lib/active_record/runtime_registry.rb +6 -4
- data/lib/active_record/sanitization.rb +134 -122
- data/lib/active_record/schema.rb +21 -24
- data/lib/active_record/schema_dumper.rb +141 -92
- data/lib/active_record/schema_migration.rb +24 -23
- data/lib/active_record/scoping/default.rb +96 -83
- data/lib/active_record/scoping/named.rb +78 -36
- data/lib/active_record/scoping.rb +45 -27
- data/lib/active_record/secure_token.rb +48 -0
- data/lib/active_record/serialization.rb +8 -6
- data/lib/active_record/signed_id.rb +116 -0
- data/lib/active_record/statement_cache.rb +89 -36
- data/lib/active_record/store.rb +128 -43
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +81 -0
- data/lib/active_record/tasks/database_tasks.rb +364 -130
- data/lib/active_record/tasks/mysql_database_tasks.rb +67 -113
- data/lib/active_record/tasks/postgresql_database_tasks.rb +86 -49
- data/lib/active_record/tasks/sqlite_database_tasks.rb +44 -19
- data/lib/active_record/test_databases.rb +24 -0
- data/lib/active_record/test_fixtures.rb +287 -0
- data/lib/active_record/timestamp.rb +86 -43
- data/lib/active_record/touch_later.rb +65 -0
- data/lib/active_record/transactions.rb +182 -163
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type/adapter_specific_registry.rb +126 -0
- data/lib/active_record/type/date.rb +4 -45
- data/lib/active_record/type/date_time.rb +4 -49
- data/lib/active_record/type/decimal_without_scale.rb +6 -2
- data/lib/active_record/type/hash_lookup_type_map.rb +5 -4
- 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 +27 -15
- data/lib/active_record/type/text.rb +2 -2
- data/lib/active_record/type/time.rb +21 -16
- data/lib/active_record/type/type_map.rb +16 -19
- data/lib/active_record/type/unsigned_integer.rb +9 -8
- data/lib/active_record/type.rb +84 -23
- data/lib/active_record/type_caster/connection.rb +33 -0
- data/lib/active_record/type_caster/map.rb +23 -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 +12 -4
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/numericality.rb +35 -0
- data/lib/active_record/validations/presence.rb +14 -13
- data/lib/active_record/validations/uniqueness.rb +63 -56
- data/lib/active_record/validations.rb +39 -35
- data/lib/active_record/version.rb +3 -1
- data/lib/active_record.rb +42 -29
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes/attribute.rb +41 -0
- data/lib/arel/collectors/bind.rb +29 -0
- data/lib/arel/collectors/composite.rb +39 -0
- data/lib/arel/collectors/plain_string.rb +20 -0
- data/lib/arel/collectors/sql_string.rb +27 -0
- data/lib/arel/collectors/substitute_binds.rb +35 -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 +126 -0
- data/lib/arel/nodes/bind_param.rb +44 -0
- data/lib/arel/nodes/case.rb +55 -0
- data/lib/arel/nodes/casted.rb +62 -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 +15 -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 +11 -0
- data/lib/arel/nodes/homogeneous_in.rb +76 -0
- data/lib/arel/nodes/in.rb +15 -0
- data/lib/arel/nodes/infix_operation.rb +92 -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 +51 -0
- data/lib/arel/nodes/node_expression.rb +13 -0
- data/lib/arel/nodes/ordering.rb +27 -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 +19 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/table_alias.rb +31 -0
- data/lib/arel/nodes/terminal.rb +16 -0
- data/lib/arel/nodes/true.rb +16 -0
- data/lib/arel/nodes/unary.rb +44 -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 +70 -0
- data/lib/arel/order_predications.rb +13 -0
- data/lib/arel/predications.rb +250 -0
- data/lib/arel/select_manager.rb +270 -0
- data/lib/arel/table.rb +118 -0
- data/lib/arel/tree_manager.rb +72 -0
- data/lib/arel/update_manager.rb +34 -0
- data/lib/arel/visitors/dot.rb +308 -0
- data/lib/arel/visitors/mysql.rb +93 -0
- data/lib/arel/visitors/postgresql.rb +120 -0
- data/lib/arel/visitors/sqlite.rb +38 -0
- data/lib/arel/visitors/to_sql.rb +899 -0
- data/lib/arel/visitors/visitor.rb +45 -0
- data/lib/arel/visitors.rb +13 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +54 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +26 -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 +43 -37
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +26 -0
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +13 -4
- data/lib/rails/generators/active_record/migration.rb +35 -1
- data/lib/rails/generators/active_record/model/model_generator.rb +55 -22
- data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
- 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} +0 -0
- data/lib/rails/generators/active_record.rb +7 -5
- metadata +172 -65
- 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_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- 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.rb +0 -163
- data/lib/active_record/attribute_decorators.rb +0 -66
- data/lib/active_record/attribute_set/builder.rb +0 -106
- data/lib/active_record/attribute_set.rb +0 -81
- data/lib/active_record/connection_adapters/connection_specification.rb +0 -275
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -491
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
- data/lib/active_record/connection_adapters/postgresql/oid/float.rb +0 -21
- data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +0 -11
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -35
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -193
- data/lib/active_record/type/big_integer.rb +0 -13
- data/lib/active_record/type/binary.rb +0 -50
- data/lib/active_record/type/boolean.rb +0 -31
- data/lib/active_record/type/decimal.rb +0 -64
- data/lib/active_record/type/decorator.rb +0 -14
- data/lib/active_record/type/float.rb +0 -19
- data/lib/active_record/type/integer.rb +0 -59
- data/lib/active_record/type/mutable.rb +0 -16
- data/lib/active_record/type/numeric.rb +0 -36
- data/lib/active_record/type/string.rb +0 -40
- data/lib/active_record/type/time_value.rb +0 -38
- data/lib/active_record/type/value.rb +0 -110
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +0 -19
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -10
@@ -1,11 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
|
-
# = Active Record Callbacks
|
4
|
+
# = Active Record \Callbacks
|
3
5
|
#
|
4
|
-
# Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic
|
5
|
-
# before or after
|
6
|
-
# dependent objects are deleted when
|
7
|
-
# before they're validated (by overwriting +before_validation+).
|
8
|
-
# the
|
6
|
+
# \Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic
|
7
|
+
# before or after a change in the object state. This can be used to make sure that associated and
|
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:
|
9
11
|
#
|
10
12
|
# * (-) <tt>save</tt>
|
11
13
|
# * (-) <tt>valid</tt>
|
@@ -20,7 +22,7 @@ module ActiveRecord
|
|
20
22
|
# * (7) <tt>after_commit</tt>
|
21
23
|
#
|
22
24
|
# Also, an <tt>after_rollback</tt> callback can be configured to be triggered whenever a rollback is issued.
|
23
|
-
# Check out
|
25
|
+
# Check out ActiveRecord::Transactions for more details about <tt>after_commit</tt> and
|
24
26
|
# <tt>after_rollback</tt>.
|
25
27
|
#
|
26
28
|
# Additionally, an <tt>after_touch</tt> callback is triggered whenever an
|
@@ -30,8 +32,8 @@ module ActiveRecord
|
|
30
32
|
# is found and instantiated by a finder, with <tt>after_initialize</tt> being triggered after new objects
|
31
33
|
# are instantiated as well.
|
32
34
|
#
|
33
|
-
# There are nineteen callbacks in total, which give
|
34
|
-
# Active Record life cycle. The sequence for calling
|
35
|
+
# There are nineteen callbacks in total, which give a lot of control over how 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,
|
35
37
|
# except that each <tt>_create</tt> callback is replaced by the corresponding <tt>_update</tt> callback.
|
36
38
|
#
|
37
39
|
# Examples:
|
@@ -53,16 +55,16 @@ module ActiveRecord
|
|
53
55
|
# end
|
54
56
|
#
|
55
57
|
# class Firm < ActiveRecord::Base
|
56
|
-
# #
|
57
|
-
# before_destroy { |record| Person.
|
58
|
-
# 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') }
|
59
61
|
# end
|
60
62
|
#
|
61
63
|
# == Inheritable callback queues
|
62
64
|
#
|
63
65
|
# Besides the overwritable callback methods, it's also possible to register callbacks through the
|
64
66
|
# use of the callback macros. Their main advantage is that the macros add behavior into a callback
|
65
|
-
# queue that is kept intact
|
67
|
+
# queue that is kept intact through an inheritance hierarchy.
|
66
68
|
#
|
67
69
|
# class Topic < ActiveRecord::Base
|
68
70
|
# before_destroy :destroy_author
|
@@ -72,22 +74,8 @@ module ActiveRecord
|
|
72
74
|
# before_destroy :destroy_readers
|
73
75
|
# end
|
74
76
|
#
|
75
|
-
#
|
76
|
-
# run, both +destroy_author+ and +destroy_readers+ are called.
|
77
|
-
# where the +before_destroy+ method is overridden:
|
78
|
-
#
|
79
|
-
# class Topic < ActiveRecord::Base
|
80
|
-
# def before_destroy() destroy_author end
|
81
|
-
# end
|
82
|
-
#
|
83
|
-
# class Reply < Topic
|
84
|
-
# def before_destroy() destroy_readers end
|
85
|
-
# end
|
86
|
-
#
|
87
|
-
# In that case, <tt>Reply#destroy</tt> would only run +destroy_readers+ and _not_ +destroy_author+.
|
88
|
-
# So, use the callback macros when you want to ensure that a certain callback is called for the entire
|
89
|
-
# hierarchy, and use the regular overwritable methods when you want to leave it up to each descendant
|
90
|
-
# to decide whether they want to call +super+ and trigger the inherited callbacks.
|
77
|
+
# When <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is
|
78
|
+
# run, both +destroy_author+ and +destroy_readers+ are called.
|
91
79
|
#
|
92
80
|
# *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the
|
93
81
|
# callbacks before specifying the associations. Otherwise, you might trigger the loading of a
|
@@ -95,10 +83,9 @@ module ActiveRecord
|
|
95
83
|
#
|
96
84
|
# == Types of callbacks
|
97
85
|
#
|
98
|
-
# There are
|
99
|
-
# inline methods (using a proc)
|
100
|
-
#
|
101
|
-
# creating mix-ins), and inline eval methods are deprecated.
|
86
|
+
# There are three types of callbacks accepted by the callback macros: method references (symbol), callback objects,
|
87
|
+
# inline methods (using a proc). Method references and callback objects are the recommended approaches,
|
88
|
+
# inline methods using a proc are sometimes appropriate (such as for creating mix-ins).
|
102
89
|
#
|
103
90
|
# The method reference callbacks work by specifying a protected or private method available in the object, like this:
|
104
91
|
#
|
@@ -107,7 +94,7 @@ module ActiveRecord
|
|
107
94
|
#
|
108
95
|
# private
|
109
96
|
# def delete_parents
|
110
|
-
# self.class.
|
97
|
+
# self.class.delete_by(parent_id: id)
|
111
98
|
# end
|
112
99
|
# end
|
113
100
|
#
|
@@ -140,7 +127,7 @@ module ActiveRecord
|
|
140
127
|
# end
|
141
128
|
# end
|
142
129
|
#
|
143
|
-
# So you specify the object you want messaged on a given callback. When that callback is triggered, the object has
|
130
|
+
# So you specify the object you want to be messaged on a given callback. When that callback is triggered, the object has
|
144
131
|
# a method by the name of the callback messaged. You can make these callbacks more flexible by passing in other
|
145
132
|
# initialization data such as the name of the attribute to work with:
|
146
133
|
#
|
@@ -175,43 +162,30 @@ module ActiveRecord
|
|
175
162
|
# end
|
176
163
|
# end
|
177
164
|
#
|
178
|
-
# The callback macros usually accept a symbol for the method they're supposed to run, but you can also
|
179
|
-
# pass a "method string", which will then be evaluated within the binding of the callback. Example:
|
180
|
-
#
|
181
|
-
# class Topic < ActiveRecord::Base
|
182
|
-
# before_destroy 'self.class.delete_all "parent_id = #{id}"'
|
183
|
-
# end
|
184
|
-
#
|
185
|
-
# Notice that single quotes (') are used so the <tt>#{id}</tt> part isn't evaluated until the callback
|
186
|
-
# is triggered. Also note that these inline callbacks can be stacked just like the regular ones:
|
187
|
-
#
|
188
|
-
# class Topic < ActiveRecord::Base
|
189
|
-
# before_destroy 'self.class.delete_all "parent_id = #{id}"',
|
190
|
-
# 'puts "Evaluated after parents are destroyed"'
|
191
|
-
# end
|
192
|
-
#
|
193
165
|
# == <tt>before_validation*</tt> returning statements
|
194
166
|
#
|
195
|
-
# If the
|
196
|
-
# aborted and
|
197
|
-
# ActiveRecord::
|
167
|
+
# If the +before_validation+ callback throws +:abort+, the process will be
|
168
|
+
# aborted and {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] will return +false+.
|
169
|
+
# If {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] is called it will raise an ActiveRecord::RecordInvalid exception.
|
170
|
+
# Nothing will be appended to the errors object.
|
198
171
|
#
|
199
172
|
# == Canceling callbacks
|
200
173
|
#
|
201
|
-
# If a <tt>before_*</tt> callback
|
202
|
-
# cancelled.
|
174
|
+
# If a <tt>before_*</tt> callback throws +:abort+, all the later callbacks and
|
175
|
+
# the associated action are cancelled.
|
203
176
|
# Callbacks are generally run in the order they are defined, with the exception of callbacks defined as
|
204
177
|
# methods on the model, which are called last.
|
205
178
|
#
|
206
179
|
# == Ordering callbacks
|
207
180
|
#
|
208
|
-
# Sometimes
|
209
|
-
# callback (+log_children+ in this case) should be executed before the children
|
181
|
+
# Sometimes application code requires that callbacks execute in a specific order. For example, a +before_destroy+
|
182
|
+
# callback (+log_children+ in this case) should be executed before records in the +children+ association are destroyed by the
|
183
|
+
# <tt>dependent: :destroy</tt> option.
|
210
184
|
#
|
211
185
|
# Let's look at the code below:
|
212
186
|
#
|
213
187
|
# class Topic < ActiveRecord::Base
|
214
|
-
# has_many :children, dependent: destroy
|
188
|
+
# has_many :children, dependent: :destroy
|
215
189
|
#
|
216
190
|
# before_destroy :log_children
|
217
191
|
#
|
@@ -221,11 +195,12 @@ module ActiveRecord
|
|
221
195
|
# end
|
222
196
|
# end
|
223
197
|
#
|
224
|
-
# In this case, the problem is that when the +before_destroy+ callback is executed, the children
|
225
|
-
# because the
|
198
|
+
# In this case, the problem is that when the +before_destroy+ callback is executed, records in the +children+ association no
|
199
|
+
# longer exist because the {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] callback was executed first.
|
200
|
+
# You can use the +prepend+ option on the +before_destroy+ callback to avoid this.
|
226
201
|
#
|
227
202
|
# class Topic < ActiveRecord::Base
|
228
|
-
# has_many :children, dependent: destroy
|
203
|
+
# has_many :children, dependent: :destroy
|
229
204
|
#
|
230
205
|
# before_destroy :log_children, prepend: true
|
231
206
|
#
|
@@ -235,37 +210,86 @@ module ActiveRecord
|
|
235
210
|
# end
|
236
211
|
# end
|
237
212
|
#
|
238
|
-
# This way, the +before_destroy+
|
213
|
+
# This way, the +before_destroy+ is executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
|
214
|
+
#
|
215
|
+
# Also, there are cases when you want several callbacks of the same type to
|
216
|
+
# be executed in order.
|
217
|
+
#
|
218
|
+
# For example:
|
219
|
+
#
|
220
|
+
# class Topic < ActiveRecord::Base
|
221
|
+
# has_many :children
|
222
|
+
#
|
223
|
+
# after_save :log_children
|
224
|
+
# after_save :do_something_else
|
225
|
+
#
|
226
|
+
# private
|
227
|
+
#
|
228
|
+
# def log_children
|
229
|
+
# # Child processing
|
230
|
+
# end
|
231
|
+
#
|
232
|
+
# def do_something_else
|
233
|
+
# # Something else
|
234
|
+
# end
|
235
|
+
# end
|
236
|
+
#
|
237
|
+
# In this case the +log_children+ is executed before +do_something_else+.
|
238
|
+
# The same applies to all non-transactional callbacks.
|
239
|
+
#
|
240
|
+
# As seen below, in case there are multiple transactional callbacks the order
|
241
|
+
# is reversed.
|
242
|
+
#
|
243
|
+
# For example:
|
244
|
+
#
|
245
|
+
# class Topic < ActiveRecord::Base
|
246
|
+
# has_many :children
|
247
|
+
#
|
248
|
+
# after_commit :log_children
|
249
|
+
# after_commit :do_something_else
|
250
|
+
#
|
251
|
+
# private
|
252
|
+
#
|
253
|
+
# def log_children
|
254
|
+
# # Child processing
|
255
|
+
# end
|
256
|
+
#
|
257
|
+
# def do_something_else
|
258
|
+
# # Something else
|
259
|
+
# end
|
260
|
+
# end
|
261
|
+
#
|
262
|
+
# In this case the +do_something_else+ is executed before +log_children+.
|
239
263
|
#
|
240
|
-
# == Transactions
|
264
|
+
# == \Transactions
|
241
265
|
#
|
242
|
-
# The entire callback chain of a
|
243
|
-
# within a transaction. That includes <tt>after_*</tt> hooks.
|
244
|
-
# goes fine a COMMIT is executed once the chain has been completed.
|
266
|
+
# The entire callback chain of a {#save}[rdoc-ref:Persistence#save], {#save!}[rdoc-ref:Persistence#save!],
|
267
|
+
# or {#destroy}[rdoc-ref:Persistence#destroy] call runs within a transaction. That includes <tt>after_*</tt> hooks.
|
268
|
+
# If everything goes fine a +COMMIT+ is executed once the chain has been completed.
|
245
269
|
#
|
246
|
-
# If a <tt>before_*</tt> callback cancels the action a ROLLBACK is issued. You
|
247
|
-
# can also trigger a ROLLBACK raising an exception in any of the callbacks,
|
270
|
+
# If a <tt>before_*</tt> callback cancels the action a +ROLLBACK+ is issued. You
|
271
|
+
# can also trigger a +ROLLBACK+ raising an exception in any of the callbacks,
|
248
272
|
# including <tt>after_*</tt> hooks. Note, however, that in that case the client
|
249
|
-
# needs to be aware of it because an ordinary
|
273
|
+
# needs to be aware of it because an ordinary {#save}[rdoc-ref:Persistence#save] will raise such exception
|
250
274
|
# instead of quietly returning +false+.
|
251
275
|
#
|
252
276
|
# == Debugging callbacks
|
253
277
|
#
|
254
|
-
# The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object.
|
278
|
+
# The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object. Active Model \Callbacks support
|
255
279
|
# <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt> property
|
256
280
|
# defines what part of the chain the callback runs in.
|
257
281
|
#
|
258
|
-
# To find all callbacks in the before_save callback chain:
|
282
|
+
# To find all callbacks in the +before_save+ callback chain:
|
259
283
|
#
|
260
284
|
# Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }
|
261
285
|
#
|
262
|
-
# Returns an array of callback objects that form the before_save chain.
|
286
|
+
# Returns an array of callback objects that form the +before_save+ chain.
|
263
287
|
#
|
264
288
|
# 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:
|
265
289
|
#
|
266
290
|
# Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).include?(:rest_when_dead)
|
267
291
|
#
|
268
|
-
# Returns true or false depending on whether the proc is contained in the before_save callback chain on a Topic model.
|
292
|
+
# Returns true or false depending on whether the proc is contained in the +before_save+ callback chain on a Topic model.
|
269
293
|
#
|
270
294
|
module Callbacks
|
271
295
|
extend ActiveSupport::Concern
|
@@ -279,34 +303,165 @@ module ActiveRecord
|
|
279
303
|
|
280
304
|
module ClassMethods
|
281
305
|
include ActiveModel::Callbacks
|
306
|
+
|
307
|
+
##
|
308
|
+
# :method: after_initialize
|
309
|
+
#
|
310
|
+
# :call-seq: after_initialize(*args, &block)
|
311
|
+
#
|
312
|
+
# Registers a callback to be called after a record is instantiated. See
|
313
|
+
# ActiveRecord::Callbacks for more information.
|
314
|
+
|
315
|
+
##
|
316
|
+
# :method: after_find
|
317
|
+
#
|
318
|
+
# :call-seq: after_find(*args, &block)
|
319
|
+
#
|
320
|
+
# Registers a callback to be called after a record is instantiated
|
321
|
+
# via a finder. See ActiveRecord::Callbacks for more information.
|
322
|
+
|
323
|
+
##
|
324
|
+
# :method: after_touch
|
325
|
+
#
|
326
|
+
# :call-seq: after_touch(*args, &block)
|
327
|
+
#
|
328
|
+
# Registers a callback to be called after a record is touched. See
|
329
|
+
# ActiveRecord::Callbacks for more information.
|
330
|
+
|
331
|
+
##
|
332
|
+
# :method: before_save
|
333
|
+
#
|
334
|
+
# :call-seq: before_save(*args, &block)
|
335
|
+
#
|
336
|
+
# Registers a callback to be called before a record is saved. See
|
337
|
+
# ActiveRecord::Callbacks for more information.
|
338
|
+
|
339
|
+
##
|
340
|
+
# :method: around_save
|
341
|
+
#
|
342
|
+
# :call-seq: around_save(*args, &block)
|
343
|
+
#
|
344
|
+
# Registers a callback to be called around the save of a record. See
|
345
|
+
# ActiveRecord::Callbacks for more information.
|
346
|
+
|
347
|
+
##
|
348
|
+
# :method: after_save
|
349
|
+
#
|
350
|
+
# :call-seq: after_save(*args, &block)
|
351
|
+
#
|
352
|
+
# Registers a callback to be called after a record is saved. See
|
353
|
+
# ActiveRecord::Callbacks for more information.
|
354
|
+
|
355
|
+
##
|
356
|
+
# :method: before_create
|
357
|
+
#
|
358
|
+
# :call-seq: before_create(*args, &block)
|
359
|
+
#
|
360
|
+
# Registers a callback to be called before a record is created. See
|
361
|
+
# ActiveRecord::Callbacks for more information.
|
362
|
+
|
363
|
+
##
|
364
|
+
# :method: around_create
|
365
|
+
#
|
366
|
+
# :call-seq: around_create(*args, &block)
|
367
|
+
#
|
368
|
+
# Registers a callback to be called around the creation of a record. See
|
369
|
+
# ActiveRecord::Callbacks for more information.
|
370
|
+
|
371
|
+
##
|
372
|
+
# :method: after_create
|
373
|
+
#
|
374
|
+
# :call-seq: after_create(*args, &block)
|
375
|
+
#
|
376
|
+
# Registers a callback to be called after a record is created. See
|
377
|
+
# ActiveRecord::Callbacks for more information.
|
378
|
+
|
379
|
+
##
|
380
|
+
# :method: before_update
|
381
|
+
#
|
382
|
+
# :call-seq: before_update(*args, &block)
|
383
|
+
#
|
384
|
+
# Registers a callback to be called before a record is updated. See
|
385
|
+
# ActiveRecord::Callbacks for more information.
|
386
|
+
|
387
|
+
##
|
388
|
+
# :method: around_update
|
389
|
+
#
|
390
|
+
# :call-seq: around_update(*args, &block)
|
391
|
+
#
|
392
|
+
# Registers a callback to be called around the update of a record. See
|
393
|
+
# ActiveRecord::Callbacks for more information.
|
394
|
+
|
395
|
+
##
|
396
|
+
# :method: after_update
|
397
|
+
#
|
398
|
+
# :call-seq: after_update(*args, &block)
|
399
|
+
#
|
400
|
+
# Registers a callback to be called after a record is updated. See
|
401
|
+
# ActiveRecord::Callbacks for more information.
|
402
|
+
|
403
|
+
##
|
404
|
+
# :method: before_destroy
|
405
|
+
#
|
406
|
+
# :call-seq: before_destroy(*args, &block)
|
407
|
+
#
|
408
|
+
# Registers a callback to be called before a record is destroyed. See
|
409
|
+
# ActiveRecord::Callbacks for more information.
|
410
|
+
|
411
|
+
##
|
412
|
+
# :method: around_destroy
|
413
|
+
#
|
414
|
+
# :call-seq: around_destroy(*args, &block)
|
415
|
+
#
|
416
|
+
# Registers a callback to be called around the destruction of a record.
|
417
|
+
# See ActiveRecord::Callbacks for more information.
|
418
|
+
|
419
|
+
##
|
420
|
+
# :method: after_destroy
|
421
|
+
#
|
422
|
+
# :call-seq: after_destroy(*args, &block)
|
423
|
+
#
|
424
|
+
# Registers a callback to be called after a record is destroyed. See
|
425
|
+
# ActiveRecord::Callbacks for more information.
|
282
426
|
end
|
283
427
|
|
284
428
|
included do
|
285
429
|
include ActiveModel::Validations::Callbacks
|
286
430
|
|
287
|
-
define_model_callbacks :initialize, :find, :touch, :
|
431
|
+
define_model_callbacks :initialize, :find, :touch, only: :after
|
288
432
|
define_model_callbacks :save, :create, :update, :destroy
|
289
433
|
end
|
290
434
|
|
291
435
|
def destroy #:nodoc:
|
436
|
+
@_destroy_callback_already_called ||= false
|
437
|
+
return if @_destroy_callback_already_called
|
438
|
+
@_destroy_callback_already_called = true
|
292
439
|
_run_destroy_callbacks { super }
|
440
|
+
rescue RecordNotDestroyed => e
|
441
|
+
@_association_destroy_exception = e
|
442
|
+
false
|
443
|
+
ensure
|
444
|
+
@_destroy_callback_already_called = false
|
293
445
|
end
|
294
446
|
|
295
|
-
def touch(
|
447
|
+
def touch(*, **) #:nodoc:
|
296
448
|
_run_touch_callbacks { super }
|
297
449
|
end
|
298
450
|
|
299
|
-
|
451
|
+
def increment!(attribute, by = 1, touch: nil) # :nodoc:
|
452
|
+
touch ? _run_touch_callbacks { super } : super
|
453
|
+
end
|
300
454
|
|
301
|
-
|
455
|
+
private
|
456
|
+
def create_or_update(**)
|
302
457
|
_run_save_callbacks { super }
|
303
458
|
end
|
304
459
|
|
305
|
-
def _create_record
|
460
|
+
def _create_record
|
306
461
|
_run_create_callbacks { super }
|
307
462
|
end
|
308
463
|
|
309
|
-
def _update_record
|
464
|
+
def _update_record
|
310
465
|
_run_update_callbacks { super }
|
311
466
|
end
|
312
467
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module Coders # :nodoc:
|
3
5
|
class JSON # :nodoc:
|
@@ -6,7 +8,7 @@ module ActiveRecord
|
|
6
8
|
end
|
7
9
|
|
8
10
|
def self.load(json)
|
9
|
-
ActiveSupport::JSON.decode(json) unless json.
|
11
|
+
ActiveSupport::JSON.decode(json) unless json.blank?
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
@@ -1,38 +1,59 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "yaml"
|
2
4
|
|
3
5
|
module ActiveRecord
|
4
6
|
module Coders # :nodoc:
|
5
7
|
class YAMLColumn # :nodoc:
|
6
|
-
|
7
8
|
attr_accessor :object_class
|
8
9
|
|
9
|
-
def initialize(object_class = Object)
|
10
|
+
def initialize(attr_name, object_class = Object)
|
11
|
+
@attr_name = attr_name
|
10
12
|
@object_class = object_class
|
13
|
+
check_arity_of_constructor
|
11
14
|
end
|
12
15
|
|
13
16
|
def dump(obj)
|
14
17
|
return if obj.nil?
|
15
18
|
|
16
|
-
|
17
|
-
raise SerializationTypeMismatch,
|
18
|
-
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
|
19
|
-
end
|
19
|
+
assert_valid_value(obj, action: "dump")
|
20
20
|
YAML.dump obj
|
21
21
|
end
|
22
22
|
|
23
23
|
def load(yaml)
|
24
24
|
return object_class.new if object_class != Object && yaml.nil?
|
25
|
-
return yaml unless yaml.is_a?(String) && yaml
|
26
|
-
obj =
|
25
|
+
return yaml unless yaml.is_a?(String) && yaml.start_with?("---")
|
26
|
+
obj = yaml_load(yaml)
|
27
27
|
|
28
|
-
|
29
|
-
raise SerializationTypeMismatch,
|
30
|
-
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
|
31
|
-
end
|
28
|
+
assert_valid_value(obj, action: "load")
|
32
29
|
obj ||= object_class.new if object_class != Object
|
33
30
|
|
34
31
|
obj
|
35
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}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def check_arity_of_constructor
|
43
|
+
load(nil)
|
44
|
+
rescue ArgumentError
|
45
|
+
raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
|
46
|
+
end
|
47
|
+
|
48
|
+
if YAML.respond_to?(:unsafe_load)
|
49
|
+
def yaml_load(payload)
|
50
|
+
YAML.unsafe_load(payload)
|
51
|
+
end
|
52
|
+
else
|
53
|
+
def yaml_load(payload)
|
54
|
+
YAML.load(payload)
|
55
|
+
end
|
56
|
+
end
|
36
57
|
end
|
37
58
|
end
|
38
59
|
end
|