activerecord 5.0.7.2 → 6.1.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 +4 -4
- data/CHANGELOG.md +829 -2015
- data/MIT-LICENSE +3 -1
- data/README.rdoc +11 -9
- data/examples/performance.rb +31 -29
- data/examples/simple.rb +5 -3
- data/lib/active_record.rb +37 -29
- data/lib/active_record/aggregations.rb +249 -247
- data/lib/active_record/association_relation.rb +30 -18
- data/lib/active_record/associations.rb +1714 -1596
- data/lib/active_record/associations/alias_tracker.rb +36 -42
- data/lib/active_record/associations/association.rb +143 -68
- data/lib/active_record/associations/association_scope.rb +98 -94
- data/lib/active_record/associations/belongs_to_association.rb +76 -46
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +13 -12
- data/lib/active_record/associations/builder/association.rb +27 -28
- data/lib/active_record/associations/builder/belongs_to.rb +52 -60
- data/lib/active_record/associations/builder/collection_association.rb +12 -22
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +40 -62
- data/lib/active_record/associations/builder/has_many.rb +10 -2
- data/lib/active_record/associations/builder/has_one.rb +35 -2
- data/lib/active_record/associations/builder/singular_association.rb +5 -1
- data/lib/active_record/associations/collection_association.rb +104 -259
- data/lib/active_record/associations/collection_proxy.rb +169 -125
- data/lib/active_record/associations/foreign_association.rb +22 -0
- data/lib/active_record/associations/has_many_association.rb +46 -31
- data/lib/active_record/associations/has_many_through_association.rb +66 -46
- data/lib/active_record/associations/has_one_association.rb +71 -52
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency.rb +169 -180
- data/lib/active_record/associations/join_dependency/join_association.rb +53 -79
- 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/preloader.rb +97 -104
- data/lib/active_record/associations/preloader/association.rb +109 -97
- data/lib/active_record/associations/preloader/through_association.rb +77 -76
- data/lib/active_record/associations/singular_association.rb +12 -45
- data/lib/active_record/associations/through_association.rb +27 -15
- data/lib/active_record/attribute_assignment.rb +55 -60
- data/lib/active_record/attribute_methods.rb +111 -141
- data/lib/active_record/attribute_methods/before_type_cast.rb +17 -9
- data/lib/active_record/attribute_methods/dirty.rb +172 -112
- data/lib/active_record/attribute_methods/primary_key.rb +88 -91
- data/lib/active_record/attribute_methods/query.rb +6 -8
- data/lib/active_record/attribute_methods/read.rb +18 -50
- data/lib/active_record/attribute_methods/serialization.rb +38 -10
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +38 -66
- data/lib/active_record/attribute_methods/write.rb +25 -32
- data/lib/active_record/attributes.rb +69 -31
- data/lib/active_record/autosave_association.rb +102 -66
- data/lib/active_record/base.rb +16 -25
- data/lib/active_record/callbacks.rb +202 -43
- data/lib/active_record/coders/json.rb +2 -0
- data/lib/active_record/coders/yaml_column.rb +11 -12
- data/lib/active_record/connection_adapters.rb +50 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +661 -375
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +14 -38
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +269 -105
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +54 -35
- data/lib/active_record/connection_adapters/abstract/quoting.rb +137 -93
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +5 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +155 -113
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +328 -162
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +68 -80
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +591 -259
- data/lib/active_record/connection_adapters/abstract/transaction.rb +229 -91
- data/lib/active_record/connection_adapters/abstract_adapter.rb +392 -244
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +457 -582
- data/lib/active_record/connection_adapters/column.rb +55 -13
- data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +31 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +8 -31
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +135 -49
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +24 -23
- data/lib/active_record/connection_adapters/mysql/quoting.rb +50 -20
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +79 -49
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +66 -56
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +70 -36
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +268 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +20 -12
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +74 -37
- data/lib/active_record/connection_adapters/pool_config.rb +63 -0
- data/lib/active_record/connection_adapters/pool_manager.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/column.rb +39 -28
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +70 -101
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid.rb +26 -21
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +22 -11
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +6 -5
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -6
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +14 -4
- 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 +19 -18
- 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 -5
- data/lib/active_record/connection_adapters/postgresql/oid/{json.rb → oid.rb} +6 -1
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +30 -9
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +52 -30
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -1
- 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 +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +98 -38
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +21 -27
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +80 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +147 -105
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +34 -32
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +426 -324
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +32 -23
- data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -6
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +418 -293
- data/lib/active_record/connection_adapters/schema_cache.rb +135 -18
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +22 -7
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +144 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +3 -1
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +72 -18
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +5 -6
- 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 +282 -290
- data/lib/active_record/connection_adapters/statement_pool.rb +9 -8
- data/lib/active_record/connection_handling.rb +287 -45
- data/lib/active_record/core.rb +385 -181
- data/lib/active_record/counter_cache.rb +60 -28
- data/lib/active_record/database_configurations.rb +272 -0
- 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/delegated_type.rb +209 -0
- data/lib/active_record/destroy_association_async_job.rb +36 -0
- data/lib/active_record/dynamic_matchers.rb +87 -87
- data/lib/active_record/enum.rb +122 -47
- data/lib/active_record/errors.rb +153 -22
- data/lib/active_record/explain.rb +13 -8
- data/lib/active_record/explain_registry.rb +3 -1
- data/lib/active_record/explain_subscriber.rb +9 -4
- data/lib/active_record/fixture_set/file.rb +20 -22
- 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 +246 -507
- data/lib/active_record/gem_version.rb +6 -4
- data/lib/active_record/inheritance.rb +168 -95
- data/lib/active_record/insert_all.rb +208 -0
- data/lib/active_record/integration.rb +114 -25
- data/lib/active_record/internal_metadata.rb +30 -24
- data/lib/active_record/legacy_yaml_adapter.rb +11 -5
- data/lib/active_record/locking/optimistic.rb +81 -85
- data/lib/active_record/locking/pessimistic.rb +22 -6
- data/lib/active_record/log_subscriber.rb +68 -31
- data/lib/active_record/middleware/database_selector.rb +77 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
- data/lib/active_record/migration.rb +439 -342
- data/lib/active_record/migration/command_recorder.rb +152 -98
- data/lib/active_record/migration/compatibility.rb +229 -60
- data/lib/active_record/migration/join_table.rb +8 -7
- data/lib/active_record/model_schema.rb +230 -122
- data/lib/active_record/nested_attributes.rb +213 -203
- data/lib/active_record/no_touching.rb +11 -2
- data/lib/active_record/null_relation.rb +12 -34
- data/lib/active_record/persistence.rb +471 -97
- data/lib/active_record/query_cache.rb +23 -12
- data/lib/active_record/querying.rb +43 -25
- data/lib/active_record/railtie.rb +155 -43
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +34 -33
- data/lib/active_record/railties/databases.rake +507 -195
- data/lib/active_record/readonly_attributes.rb +9 -4
- data/lib/active_record/reflection.rb +245 -269
- data/lib/active_record/relation.rb +475 -324
- data/lib/active_record/relation/batches.rb +125 -72
- data/lib/active_record/relation/batches/batch_enumerator.rb +28 -10
- data/lib/active_record/relation/calculations.rb +267 -171
- data/lib/active_record/relation/delegation.rb +73 -69
- data/lib/active_record/relation/finder_methods.rb +238 -248
- data/lib/active_record/relation/from_clause.rb +7 -9
- data/lib/active_record/relation/merger.rb +95 -77
- data/lib/active_record/relation/predicate_builder.rb +109 -110
- data/lib/active_record/relation/predicate_builder/array_handler.rb +22 -17
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +42 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +6 -4
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +55 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +7 -18
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +7 -1
- data/lib/active_record/relation/query_attribute.rb +33 -2
- data/lib/active_record/relation/query_methods.rb +654 -374
- data/lib/active_record/relation/record_fetch_warning.rb +8 -6
- data/lib/active_record/relation/spawn_methods.rb +15 -14
- data/lib/active_record/relation/where_clause.rb +171 -109
- data/lib/active_record/result.rb +88 -51
- data/lib/active_record/runtime_registry.rb +5 -3
- data/lib/active_record/sanitization.rb +73 -100
- data/lib/active_record/schema.rb +7 -14
- data/lib/active_record/schema_dumper.rb +101 -69
- data/lib/active_record/schema_migration.rb +16 -12
- data/lib/active_record/scoping.rb +20 -20
- data/lib/active_record/scoping/default.rb +92 -95
- data/lib/active_record/scoping/named.rb +39 -30
- data/lib/active_record/secure_token.rb +19 -9
- data/lib/active_record/serialization.rb +7 -3
- data/lib/active_record/signed_id.rb +116 -0
- data/lib/active_record/statement_cache.rb +80 -29
- data/lib/active_record/store.rb +122 -42
- data/lib/active_record/suppressor.rb +6 -3
- data/lib/active_record/table_metadata.rb +51 -39
- data/lib/active_record/tasks/database_tasks.rb +332 -115
- data/lib/active_record/tasks/mysql_database_tasks.rb +66 -104
- data/lib/active_record/tasks/postgresql_database_tasks.rb +84 -56
- data/lib/active_record/tasks/sqlite_database_tasks.rb +40 -19
- data/lib/active_record/test_databases.rb +24 -0
- data/lib/active_record/test_fixtures.rb +246 -0
- data/lib/active_record/timestamp.rb +70 -38
- data/lib/active_record/touch_later.rb +26 -24
- data/lib/active_record/transactions.rb +121 -184
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type.rb +29 -17
- data/lib/active_record/type/adapter_specific_registry.rb +44 -48
- data/lib/active_record/type/date.rb +2 -0
- data/lib/active_record/type/date_time.rb +2 -0
- data/lib/active_record/type/decimal_without_scale.rb +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +5 -4
- data/lib/active_record/type/internal/timezone.rb +2 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +20 -9
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +12 -1
- data/lib/active_record/type/type_map.rb +14 -17
- data/lib/active_record/type/unsigned_integer.rb +16 -0
- data/lib/active_record/type_caster.rb +4 -2
- data/lib/active_record/type_caster/connection.rb +17 -13
- data/lib/active_record/type_caster/map.rb +10 -6
- data/lib/active_record/validations.rb +8 -5
- data/lib/active_record/validations/absence.rb +2 -0
- data/lib/active_record/validations/associated.rb +4 -3
- data/lib/active_record/validations/length.rb +2 -0
- data/lib/active_record/validations/numericality.rb +35 -0
- data/lib/active_record/validations/presence.rb +4 -2
- data/lib/active_record/validations/uniqueness.rb +52 -45
- data/lib/active_record/version.rb +3 -1
- data/lib/arel.rb +54 -0
- 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.rb +70 -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 +72 -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/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.rb +13 -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/window_predications.rb +9 -0
- data/lib/rails/generators/active_record.rb +7 -5
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +26 -0
- data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration.rb +22 -3
- data/lib/rails/generators/active_record/migration/migration_generator.rb +38 -35
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +3 -1
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +7 -5
- data/lib/rails/generators/active_record/model/model_generator.rb +41 -25
- 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 → model.rb.tt} +10 -1
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- metadata +141 -57
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -17
- 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 -15
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -20
- data/lib/active_record/attribute.rb +0 -213
- data/lib/active_record/attribute/user_provided_default.rb +0 -28
- data/lib/active_record/attribute_decorators.rb +0 -67
- data/lib/active_record/attribute_mutation_tracker.rb +0 -70
- data/lib/active_record/attribute_set.rb +0 -110
- data/lib/active_record/attribute_set/builder.rb +0 -132
- data/lib/active_record/collection_cache_key.rb +0 -50
- data/lib/active_record/connection_adapters/connection_specification.rb +0 -263
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -22
- data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +0 -50
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
- data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -17
- data/lib/active_record/relation/predicate_builder/class_handler.rb +0 -27
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -57
- data/lib/active_record/relation/where_clause_factory.rb +0 -38
- data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -1,5 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module SecureToken
|
5
|
+
class MinimumLengthError < StandardError; end
|
6
|
+
|
7
|
+
MINIMUM_TOKEN_LENGTH = 24
|
8
|
+
|
3
9
|
extend ActiveSupport::Concern
|
4
10
|
|
5
11
|
module ClassMethods
|
@@ -8,30 +14,34 @@ module ActiveRecord
|
|
8
14
|
# # Schema: User(token:string, auth_token:string)
|
9
15
|
# class User < ActiveRecord::Base
|
10
16
|
# has_secure_token
|
11
|
-
# has_secure_token :auth_token
|
17
|
+
# has_secure_token :auth_token, length: 36
|
12
18
|
# end
|
13
19
|
#
|
14
20
|
# user = User.new
|
15
21
|
# user.save
|
16
22
|
# user.token # => "pX27zsMN2ViQKta1bGfLmVJE"
|
17
|
-
# user.auth_token # => "
|
23
|
+
# user.auth_token # => "tU9bLuZseefXQ4yQxQo8wjtBvsAfPc78os6R"
|
18
24
|
# user.regenerate_token # => true
|
19
25
|
# user.regenerate_auth_token # => true
|
20
26
|
#
|
21
|
-
# <tt>SecureRandom::base58</tt> is used to generate
|
27
|
+
# <tt>SecureRandom::base58</tt> is used to generate at minimum a 24-character unique token, so collisions are highly unlikely.
|
22
28
|
#
|
23
29
|
# Note that it's still possible to generate a race condition in the database in the same way that
|
24
30
|
# {validates_uniqueness_of}[rdoc-ref:Validations::ClassMethods#validates_uniqueness_of] can.
|
25
31
|
# You're encouraged to add a unique index in the database to deal with this even more unlikely scenario.
|
26
|
-
def has_secure_token(attribute = :token)
|
32
|
+
def has_secure_token(attribute = :token, length: MINIMUM_TOKEN_LENGTH)
|
33
|
+
if length < MINIMUM_TOKEN_LENGTH
|
34
|
+
raise MinimumLengthError, "Token requires a minimum length of #{MINIMUM_TOKEN_LENGTH} characters."
|
35
|
+
end
|
36
|
+
|
27
37
|
# Load securerandom only when has_secure_token is used.
|
28
|
-
require
|
29
|
-
define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_secure_token }
|
30
|
-
before_create {
|
38
|
+
require "active_support/core_ext/securerandom"
|
39
|
+
define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_secure_token(length: length) }
|
40
|
+
before_create { send("#{attribute}=", self.class.generate_unique_secure_token(length: length)) unless send("#{attribute}?") }
|
31
41
|
end
|
32
42
|
|
33
|
-
def generate_unique_secure_token
|
34
|
-
SecureRandom.base58(
|
43
|
+
def generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH)
|
44
|
+
SecureRandom.base58(length)
|
35
45
|
end
|
36
46
|
end
|
37
47
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord #:nodoc:
|
2
4
|
# = Active Record \Serialization
|
3
5
|
module Serialization
|
@@ -9,10 +11,12 @@ module ActiveRecord #:nodoc:
|
|
9
11
|
end
|
10
12
|
|
11
13
|
def serializable_hash(options = nil)
|
12
|
-
|
14
|
+
if self.class._has_attribute?(self.class.inheritance_column)
|
15
|
+
options = options ? options.dup : {}
|
13
16
|
|
14
|
-
|
15
|
-
|
17
|
+
options[:except] = Array(options[:except]).map(&:to_s)
|
18
|
+
options[:except] |= Array(self.class.inheritance_column)
|
19
|
+
end
|
16
20
|
|
17
21
|
super(options)
|
18
22
|
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
# = Active Record Signed Id
|
5
|
+
module SignedId
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
##
|
10
|
+
# :singleton-method:
|
11
|
+
# Set the secret used for the signed id verifier instance when using Active Record outside of Rails.
|
12
|
+
# Within Rails, this is automatically set using the Rails application key generator.
|
13
|
+
mattr_accessor :signed_id_verifier_secret, instance_writer: false
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
# Lets you find a record based on a signed id that's safe to put into the world without risk of tampering.
|
18
|
+
# This is particularly useful for things like password reset or email verification, where you want
|
19
|
+
# the bearer of the signed id to be able to interact with the underlying record, but usually only within
|
20
|
+
# a certain time period.
|
21
|
+
#
|
22
|
+
# You set the time period that the signed id is valid for during generation, using the instance method
|
23
|
+
# <tt>signed_id(expires_in: 15.minutes)</tt>. If the time has elapsed before a signed find is attempted,
|
24
|
+
# the signed id will no longer be valid, and nil is returned.
|
25
|
+
#
|
26
|
+
# It's possible to further restrict the use of a signed id with a purpose. This helps when you have a
|
27
|
+
# general base model, like a User, which might have signed ids for several things, like password reset
|
28
|
+
# or email verification. The purpose that was set during generation must match the purpose set when
|
29
|
+
# finding. If there's a mismatch, nil is again returned.
|
30
|
+
#
|
31
|
+
# ==== Examples
|
32
|
+
#
|
33
|
+
# signed_id = User.first.signed_id expires_in: 15.minutes, purpose: :password_reset
|
34
|
+
#
|
35
|
+
# User.find_signed signed_id # => nil, since the purpose does not match
|
36
|
+
#
|
37
|
+
# travel 16.minutes
|
38
|
+
# User.find_signed signed_id, purpose: :password_reset # => nil, since the signed id has expired
|
39
|
+
#
|
40
|
+
# travel_back
|
41
|
+
# User.find_signed signed_id, purpose: :password_reset # => User.first
|
42
|
+
def find_signed(signed_id, purpose: nil)
|
43
|
+
raise UnknownPrimaryKey.new(self) if primary_key.nil?
|
44
|
+
|
45
|
+
if id = signed_id_verifier.verified(signed_id, purpose: combine_signed_id_purposes(purpose))
|
46
|
+
find_by primary_key => id
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Works like +find_signed+, but will raise an +ActiveSupport::MessageVerifier::InvalidSignature+
|
51
|
+
# exception if the +signed_id+ has either expired, has a purpose mismatch, is for another record,
|
52
|
+
# or has been tampered with. It will also raise an +ActiveRecord::RecordNotFound+ exception if
|
53
|
+
# the valid signed id can't find a record.
|
54
|
+
#
|
55
|
+
# === Examples
|
56
|
+
#
|
57
|
+
# User.find_signed! "bad data" # => ActiveSupport::MessageVerifier::InvalidSignature
|
58
|
+
#
|
59
|
+
# signed_id = User.first.signed_id
|
60
|
+
# User.first.destroy
|
61
|
+
# User.find_signed! signed_id # => ActiveRecord::RecordNotFound
|
62
|
+
def find_signed!(signed_id, purpose: nil)
|
63
|
+
if id = signed_id_verifier.verify(signed_id, purpose: combine_signed_id_purposes(purpose))
|
64
|
+
find(id)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# The verifier instance that all signed ids are generated and verified from. By default, it'll be initialized
|
69
|
+
# with the class-level +signed_id_verifier_secret+, which within Rails comes from the
|
70
|
+
# Rails.application.key_generator. By default, it's SHA256 for the digest and JSON for the serialization.
|
71
|
+
def signed_id_verifier
|
72
|
+
@signed_id_verifier ||= begin
|
73
|
+
secret = signed_id_verifier_secret
|
74
|
+
secret = secret.call if secret.respond_to?(:call)
|
75
|
+
|
76
|
+
if secret.nil?
|
77
|
+
raise ArgumentError, "You must set ActiveRecord::Base.signed_id_verifier_secret to use signed ids"
|
78
|
+
else
|
79
|
+
ActiveSupport::MessageVerifier.new secret, digest: "SHA256", serializer: JSON
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Allows you to pass in a custom verifier used for the signed ids. This also allows you to use different
|
85
|
+
# verifiers for different classes. This is also helpful if you need to rotate keys, as you can prepare
|
86
|
+
# your custom verifier for that in advance. See +ActiveSupport::MessageVerifier+ for details.
|
87
|
+
def signed_id_verifier=(verifier)
|
88
|
+
@signed_id_verifier = verifier
|
89
|
+
end
|
90
|
+
|
91
|
+
# :nodoc:
|
92
|
+
def combine_signed_id_purposes(purpose)
|
93
|
+
[ base_class.name.underscore, purpose.to_s ].compact_blank.join("/")
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
# Returns a signed id that's generated using a preconfigured +ActiveSupport::MessageVerifier+ instance.
|
99
|
+
# This signed id is tamper proof, so it's safe to send in an email or otherwise share with the outside world.
|
100
|
+
# It can further more be set to expire (the default is not to expire), and scoped down with a specific purpose.
|
101
|
+
# If the expiration date has been exceeded before +find_signed+ is called, the id won't find the designated
|
102
|
+
# record. If a purpose is set, this too must match.
|
103
|
+
#
|
104
|
+
# If you accidentally let a signed id out in the wild that you wish to retract sooner than its expiration date
|
105
|
+
# (or maybe you forgot to set an expiration date while meaning to!), you can use the purpose to essentially
|
106
|
+
# version the signed_id, like so:
|
107
|
+
#
|
108
|
+
# user.signed_id purpose: :v2
|
109
|
+
#
|
110
|
+
# And you then change your +find_signed+ calls to require this new purpose. Any old signed ids that were not
|
111
|
+
# created with the purpose will no longer find the record.
|
112
|
+
def signed_id(expires_in: nil, purpose: nil)
|
113
|
+
self.class.signed_id_verifier.generate id, expires_in: expires_in, purpose: self.class.combine_signed_id_purposes(purpose)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module ActiveRecord
|
3
4
|
# Statement cache is used to cache a single statement in order to avoid creating the AST again.
|
4
5
|
# Initializing the cache is done by passing the statement in the create block:
|
5
6
|
#
|
@@ -8,12 +9,12 @@ module ActiveRecord
|
|
8
9
|
# end
|
9
10
|
#
|
10
11
|
# The cached statement is executed by using the
|
11
|
-
#
|
12
|
+
# {connection.execute}[rdoc-ref:ConnectionAdapters::DatabaseStatements#execute] method:
|
12
13
|
#
|
13
|
-
# cache.execute([], Book
|
14
|
+
# cache.execute([], Book.connection)
|
14
15
|
#
|
15
16
|
# The relation returned by the block is cached, and for each
|
16
|
-
# [
|
17
|
+
# {execute}[rdoc-ref:ConnectionAdapters::DatabaseStatements#execute]
|
17
18
|
# call the cached relation gets duped. Database is queried when +to_a+ is called on the relation.
|
18
19
|
#
|
19
20
|
# If you want to cache the statement without the values you can use the +bind+ method of the
|
@@ -25,7 +26,7 @@ module ActiveRecord
|
|
25
26
|
#
|
26
27
|
# And pass the bind values as the first argument of +execute+ call.
|
27
28
|
#
|
28
|
-
# cache.execute(["my book"], Book
|
29
|
+
# cache.execute(["my book"], Book.connection)
|
29
30
|
class StatementCache # :nodoc:
|
30
31
|
class Substitute; end # :nodoc:
|
31
32
|
|
@@ -40,28 +41,69 @@ module ActiveRecord
|
|
40
41
|
end
|
41
42
|
|
42
43
|
class PartialQuery < Query # :nodoc:
|
43
|
-
def initialize
|
44
|
+
def initialize(values)
|
44
45
|
@values = values
|
45
|
-
@indexes = values.each_with_index.find_all { |thing,i|
|
46
|
-
|
46
|
+
@indexes = values.each_with_index.find_all { |thing, i|
|
47
|
+
Substitute === thing
|
47
48
|
}.map(&:last)
|
48
49
|
end
|
49
50
|
|
50
51
|
def sql_for(binds, connection)
|
51
52
|
val = @values.dup
|
52
|
-
|
53
|
-
|
53
|
+
@indexes.each do |i|
|
54
|
+
value = binds.shift
|
55
|
+
if ActiveModel::Attribute === value
|
56
|
+
value = value.value_for_database
|
57
|
+
end
|
58
|
+
val[i] = connection.quote(value)
|
59
|
+
end
|
54
60
|
val.join
|
55
61
|
end
|
56
62
|
end
|
57
63
|
|
58
|
-
|
59
|
-
|
64
|
+
class PartialQueryCollector
|
65
|
+
attr_accessor :preparable
|
66
|
+
|
67
|
+
def initialize
|
68
|
+
@parts = []
|
69
|
+
@binds = []
|
70
|
+
end
|
71
|
+
|
72
|
+
def <<(str)
|
73
|
+
@parts << str
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
def add_bind(obj)
|
78
|
+
@binds << obj
|
79
|
+
@parts << Substitute.new
|
80
|
+
self
|
81
|
+
end
|
82
|
+
|
83
|
+
def add_binds(binds)
|
84
|
+
@binds.concat binds
|
85
|
+
binds.size.times do |i|
|
86
|
+
@parts << ", " unless i == 0
|
87
|
+
@parts << Substitute.new
|
88
|
+
end
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
def value
|
93
|
+
[@parts, @binds]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.query(sql)
|
98
|
+
Query.new(sql)
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.partial_query(values)
|
102
|
+
PartialQuery.new(values)
|
60
103
|
end
|
61
104
|
|
62
|
-
def self.
|
63
|
-
|
64
|
-
PartialQuery.new collected
|
105
|
+
def self.partial_query_collector
|
106
|
+
PartialQueryCollector.new
|
65
107
|
end
|
66
108
|
|
67
109
|
class Params # :nodoc:
|
@@ -70,11 +112,11 @@ module ActiveRecord
|
|
70
112
|
|
71
113
|
class BindMap # :nodoc:
|
72
114
|
def initialize(bound_attributes)
|
73
|
-
@indexes
|
115
|
+
@indexes = []
|
74
116
|
@bound_attributes = bound_attributes
|
75
117
|
|
76
118
|
bound_attributes.each_with_index do |attr, i|
|
77
|
-
if Substitute === attr.value
|
119
|
+
if ActiveModel::Attribute === attr && Substitute === attr.value
|
78
120
|
@indexes << i
|
79
121
|
end
|
80
122
|
end
|
@@ -82,32 +124,41 @@ module ActiveRecord
|
|
82
124
|
|
83
125
|
def bind(values)
|
84
126
|
bas = @bound_attributes.dup
|
85
|
-
@indexes.each_with_index { |offset,i| bas[offset] = bas[offset].with_cast_value(values[i]) }
|
127
|
+
@indexes.each_with_index { |offset, i| bas[offset] = bas[offset].with_cast_value(values[i]) }
|
86
128
|
bas
|
87
129
|
end
|
88
130
|
end
|
89
131
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
bind_map
|
95
|
-
query_builder = connection.cacheable_query relation.arel
|
96
|
-
new query_builder, bind_map
|
132
|
+
def self.create(connection, callable = nil, &block)
|
133
|
+
relation = (callable || block).call Params.new
|
134
|
+
query_builder, binds = connection.cacheable_query(self, relation.arel)
|
135
|
+
bind_map = BindMap.new(binds)
|
136
|
+
new(query_builder, bind_map, relation.klass)
|
97
137
|
end
|
98
138
|
|
99
|
-
def initialize(query_builder, bind_map)
|
139
|
+
def initialize(query_builder, bind_map, klass)
|
100
140
|
@query_builder = query_builder
|
101
|
-
@bind_map
|
141
|
+
@bind_map = bind_map
|
142
|
+
@klass = klass
|
102
143
|
end
|
103
144
|
|
104
|
-
def execute(params,
|
145
|
+
def execute(params, connection, &block)
|
105
146
|
bind_values = bind_map.bind params
|
106
147
|
|
107
148
|
sql = query_builder.sql_for bind_values, connection
|
108
149
|
|
109
150
|
klass.find_by_sql(sql, bind_values, preparable: true, &block)
|
151
|
+
rescue ::RangeError
|
152
|
+
[]
|
153
|
+
end
|
154
|
+
|
155
|
+
def self.unsupported_value?(value)
|
156
|
+
case value
|
157
|
+
when NilClass, Array, Range, Hash, Relation, Base then true
|
158
|
+
end
|
110
159
|
end
|
111
|
-
|
160
|
+
|
161
|
+
private
|
162
|
+
attr_reader :query_builder, :bind_map, :klass
|
112
163
|
end
|
113
164
|
end
|
data/lib/active_record/store.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
2
4
|
|
3
5
|
module ActiveRecord
|
4
6
|
# Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column.
|
@@ -9,14 +11,20 @@ module ActiveRecord
|
|
9
11
|
# of the model. This is very helpful for easily exposing store keys to a form or elsewhere that's
|
10
12
|
# already built around just accessing attributes on the model.
|
11
13
|
#
|
14
|
+
# Every accessor comes with dirty tracking methods (+key_changed?+, +key_was+ and +key_change+) and
|
15
|
+
# methods to access the changes made during the last save (+saved_change_to_key?+, +saved_change_to_key+ and
|
16
|
+
# +key_before_last_save+).
|
17
|
+
#
|
18
|
+
# NOTE: There is no +key_will_change!+ method for accessors, use +store_will_change!+ instead.
|
19
|
+
#
|
12
20
|
# Make sure that you declare the database column used for the serialized store as a text, so there's
|
13
21
|
# plenty of room.
|
14
22
|
#
|
15
23
|
# You can set custom coder to encode/decode your serialized attributes to/from different formats.
|
16
24
|
# JSON, YAML, Marshal are supported out of the box. Generally it can be any wrapper that provides +load+ and +dump+.
|
17
25
|
#
|
18
|
-
# NOTE: If you are using
|
19
|
-
# the serialization provided by {.store}[rdoc-ref:rdoc-ref:ClassMethods#store].
|
26
|
+
# NOTE: If you are using structured database data types (e.g. PostgreSQL +hstore+/+json+, or MySQL 5.7+
|
27
|
+
# +json+) there is no need for the serialization provided by {.store}[rdoc-ref:rdoc-ref:ClassMethods#store].
|
20
28
|
# Simply use {.store_accessor}[rdoc-ref:ClassMethods#store_accessor] instead to generate
|
21
29
|
# the accessor methods. Be aware that these columns use a string keyed hash and do not allow access
|
22
30
|
# using a symbol.
|
@@ -29,24 +37,40 @@ module ActiveRecord
|
|
29
37
|
#
|
30
38
|
# class User < ActiveRecord::Base
|
31
39
|
# store :settings, accessors: [ :color, :homepage ], coder: JSON
|
40
|
+
# store :parent, accessors: [ :name ], coder: JSON, prefix: true
|
41
|
+
# store :spouse, accessors: [ :name ], coder: JSON, prefix: :partner
|
42
|
+
# store :settings, accessors: [ :two_factor_auth ], suffix: true
|
43
|
+
# store :settings, accessors: [ :login_retry ], suffix: :config
|
32
44
|
# end
|
33
45
|
#
|
34
|
-
# u = User.new(color: 'black', homepage: '37signals.com')
|
46
|
+
# u = User.new(color: 'black', homepage: '37signals.com', parent_name: 'Mary', partner_name: 'Lily')
|
35
47
|
# u.color # Accessor stored attribute
|
48
|
+
# u.parent_name # Accessor stored attribute with prefix
|
49
|
+
# u.partner_name # Accessor stored attribute with custom prefix
|
50
|
+
# u.two_factor_auth_settings # Accessor stored attribute with suffix
|
51
|
+
# u.login_retry_config # Accessor stored attribute with custom suffix
|
36
52
|
# u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
|
37
53
|
#
|
38
54
|
# # There is no difference between strings and symbols for accessing custom attributes
|
39
55
|
# u.settings[:country] # => 'Denmark'
|
40
56
|
# u.settings['country'] # => 'Denmark'
|
41
57
|
#
|
58
|
+
# # Dirty tracking
|
59
|
+
# u.color = 'green'
|
60
|
+
# u.color_changed? # => true
|
61
|
+
# u.color_was # => 'black'
|
62
|
+
# u.color_change # => ['black', 'red']
|
63
|
+
#
|
42
64
|
# # Add additional accessors to an existing store through store_accessor
|
43
65
|
# class SuperUser < User
|
44
66
|
# store_accessor :settings, :privileges, :servants
|
67
|
+
# store_accessor :parent, :birthday, prefix: true
|
68
|
+
# store_accessor :settings, :secret_question, suffix: :config
|
45
69
|
# end
|
46
70
|
#
|
47
71
|
# The stored attribute names can be retrieved using {.stored_attributes}[rdoc-ref:rdoc-ref:ClassMethods#stored_attributes].
|
48
72
|
#
|
49
|
-
# User.stored_attributes[:settings] # [:color, :homepage]
|
73
|
+
# User.stored_attributes[:settings] # [:color, :homepage, :two_factor_auth, :login_retry]
|
50
74
|
#
|
51
75
|
# == Overwriting default accessors
|
52
76
|
#
|
@@ -78,22 +102,79 @@ module ActiveRecord
|
|
78
102
|
|
79
103
|
module ClassMethods
|
80
104
|
def store(store_attribute, options = {})
|
81
|
-
serialize store_attribute, IndifferentCoder.new(options[:coder])
|
82
|
-
store_accessor(store_attribute, options[:accessors]) if options.has_key? :accessors
|
105
|
+
serialize store_attribute, IndifferentCoder.new(store_attribute, options[:coder])
|
106
|
+
store_accessor(store_attribute, options[:accessors], **options.slice(:prefix, :suffix)) if options.has_key? :accessors
|
83
107
|
end
|
84
108
|
|
85
|
-
def store_accessor(store_attribute, *keys)
|
109
|
+
def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
|
86
110
|
keys = keys.flatten
|
87
111
|
|
112
|
+
accessor_prefix =
|
113
|
+
case prefix
|
114
|
+
when String, Symbol
|
115
|
+
"#{prefix}_"
|
116
|
+
when TrueClass
|
117
|
+
"#{store_attribute}_"
|
118
|
+
else
|
119
|
+
""
|
120
|
+
end
|
121
|
+
accessor_suffix =
|
122
|
+
case suffix
|
123
|
+
when String, Symbol
|
124
|
+
"_#{suffix}"
|
125
|
+
when TrueClass
|
126
|
+
"_#{store_attribute}"
|
127
|
+
else
|
128
|
+
""
|
129
|
+
end
|
130
|
+
|
88
131
|
_store_accessors_module.module_eval do
|
89
132
|
keys.each do |key|
|
90
|
-
|
133
|
+
accessor_key = "#{accessor_prefix}#{key}#{accessor_suffix}"
|
134
|
+
|
135
|
+
define_method("#{accessor_key}=") do |value|
|
91
136
|
write_store_attribute(store_attribute, key, value)
|
92
137
|
end
|
93
138
|
|
94
|
-
define_method(
|
139
|
+
define_method(accessor_key) do
|
95
140
|
read_store_attribute(store_attribute, key)
|
96
141
|
end
|
142
|
+
|
143
|
+
define_method("#{accessor_key}_changed?") do
|
144
|
+
return false unless attribute_changed?(store_attribute)
|
145
|
+
prev_store, new_store = changes[store_attribute]
|
146
|
+
prev_store&.dig(key) != new_store&.dig(key)
|
147
|
+
end
|
148
|
+
|
149
|
+
define_method("#{accessor_key}_change") do
|
150
|
+
return unless attribute_changed?(store_attribute)
|
151
|
+
prev_store, new_store = changes[store_attribute]
|
152
|
+
[prev_store&.dig(key), new_store&.dig(key)]
|
153
|
+
end
|
154
|
+
|
155
|
+
define_method("#{accessor_key}_was") do
|
156
|
+
return unless attribute_changed?(store_attribute)
|
157
|
+
prev_store, _new_store = changes[store_attribute]
|
158
|
+
prev_store&.dig(key)
|
159
|
+
end
|
160
|
+
|
161
|
+
define_method("saved_change_to_#{accessor_key}?") do
|
162
|
+
return false unless saved_change_to_attribute?(store_attribute)
|
163
|
+
prev_store, new_store = saved_change_to_attribute(store_attribute)
|
164
|
+
prev_store&.dig(key) != new_store&.dig(key)
|
165
|
+
end
|
166
|
+
|
167
|
+
define_method("saved_change_to_#{accessor_key}") do
|
168
|
+
return unless saved_change_to_attribute?(store_attribute)
|
169
|
+
prev_store, new_store = saved_change_to_attribute(store_attribute)
|
170
|
+
[prev_store&.dig(key), new_store&.dig(key)]
|
171
|
+
end
|
172
|
+
|
173
|
+
define_method("#{accessor_key}_before_last_save") do
|
174
|
+
return unless saved_change_to_attribute?(store_attribute)
|
175
|
+
prev_store, _new_store = saved_change_to_attribute(store_attribute)
|
176
|
+
prev_store&.dig(key)
|
177
|
+
end
|
97
178
|
end
|
98
179
|
end
|
99
180
|
|
@@ -114,27 +195,26 @@ module ActiveRecord
|
|
114
195
|
|
115
196
|
def stored_attributes
|
116
197
|
parent = superclass.respond_to?(:stored_attributes) ? superclass.stored_attributes : {}
|
117
|
-
if
|
118
|
-
parent.merge!(
|
198
|
+
if local_stored_attributes
|
199
|
+
parent.merge!(local_stored_attributes) { |k, a, b| a | b }
|
119
200
|
end
|
120
201
|
parent
|
121
202
|
end
|
122
203
|
end
|
123
204
|
|
124
|
-
|
125
|
-
def read_store_attribute(store_attribute, key)
|
205
|
+
private
|
206
|
+
def read_store_attribute(store_attribute, key) # :doc:
|
126
207
|
accessor = store_accessor_for(store_attribute)
|
127
208
|
accessor.read(self, store_attribute, key)
|
128
209
|
end
|
129
210
|
|
130
|
-
def write_store_attribute(store_attribute, key, value)
|
211
|
+
def write_store_attribute(store_attribute, key, value) # :doc:
|
131
212
|
accessor = store_accessor_for(store_attribute)
|
132
213
|
accessor.write(self, store_attribute, key, value)
|
133
214
|
end
|
134
215
|
|
135
|
-
private
|
136
216
|
def store_accessor_for(store_attribute)
|
137
|
-
type_for_attribute(store_attribute
|
217
|
+
type_for_attribute(store_attribute).accessor
|
138
218
|
end
|
139
219
|
|
140
220
|
class HashAccessor # :nodoc:
|
@@ -171,40 +251,40 @@ module ActiveRecord
|
|
171
251
|
attribute = object.send(store_attribute)
|
172
252
|
unless attribute.is_a?(ActiveSupport::HashWithIndifferentAccess)
|
173
253
|
attribute = IndifferentCoder.as_indifferent_hash(attribute)
|
174
|
-
object.
|
254
|
+
object.public_send :"#{store_attribute}=", attribute
|
175
255
|
end
|
176
256
|
attribute
|
177
257
|
end
|
178
258
|
end
|
179
259
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
260
|
+
class IndifferentCoder # :nodoc:
|
261
|
+
def initialize(attr_name, coder_or_class_name)
|
262
|
+
@coder =
|
263
|
+
if coder_or_class_name.respond_to?(:load) && coder_or_class_name.respond_to?(:dump)
|
264
|
+
coder_or_class_name
|
265
|
+
else
|
266
|
+
ActiveRecord::Coders::YAMLColumn.new(attr_name, coder_or_class_name || Object)
|
267
|
+
end
|
268
|
+
end
|
189
269
|
|
190
|
-
|
191
|
-
|
192
|
-
|
270
|
+
def dump(obj)
|
271
|
+
@coder.dump self.class.as_indifferent_hash(obj)
|
272
|
+
end
|
193
273
|
|
194
|
-
|
195
|
-
|
196
|
-
|
274
|
+
def load(yaml)
|
275
|
+
self.class.as_indifferent_hash(@coder.load(yaml || ""))
|
276
|
+
end
|
197
277
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
278
|
+
def self.as_indifferent_hash(obj)
|
279
|
+
case obj
|
280
|
+
when ActiveSupport::HashWithIndifferentAccess
|
281
|
+
obj
|
282
|
+
when Hash
|
283
|
+
obj.with_indifferent_access
|
284
|
+
else
|
285
|
+
ActiveSupport::HashWithIndifferentAccess.new
|
286
|
+
end
|
206
287
|
end
|
207
288
|
end
|
208
|
-
end
|
209
289
|
end
|
210
290
|
end
|