activerecord 4.2.0 → 6.1.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1221 -796
- 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 +267 -249
- data/lib/active_record/association_relation.rb +45 -7
- data/lib/active_record/associations/alias_tracker.rb +40 -43
- data/lib/active_record/associations/association.rb +172 -67
- data/lib/active_record/associations/association_scope.rb +105 -129
- data/lib/active_record/associations/belongs_to_association.rb +85 -59
- 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 -33
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +57 -70
- 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 +168 -279
- data/lib/active_record/associations/collection_proxy.rb +263 -155
- data/lib/active_record/associations/foreign_association.rb +33 -0
- data/lib/active_record/associations/has_many_association.rb +57 -84
- data/lib/active_record/associations/has_many_through_association.rb +70 -82
- data/lib/active_record/associations/has_one_association.rb +74 -47
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +54 -73
- 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 +175 -164
- data/lib/active_record/associations/preloader/association.rb +107 -112
- data/lib/active_record/associations/preloader/through_association.rb +85 -65
- data/lib/active_record/associations/preloader.rb +99 -96
- data/lib/active_record/associations/singular_association.rb +18 -45
- data/lib/active_record/associations/through_association.rb +49 -24
- data/lib/active_record/associations.rb +1845 -1597
- data/lib/active_record/attribute_assignment.rb +59 -185
- data/lib/active_record/attribute_methods/before_type_cast.rb +20 -7
- data/lib/active_record/attribute_methods/dirty.rb +168 -138
- 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 +59 -36
- data/lib/active_record/attribute_methods/write.rb +25 -56
- data/lib/active_record/attribute_methods.rb +153 -162
- data/lib/active_record/attributes.rb +234 -70
- data/lib/active_record/autosave_association.rb +157 -69
- data/lib/active_record/base.rb +49 -50
- 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 +46 -13
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +887 -317
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +17 -41
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +301 -113
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +78 -24
- data/lib/active_record/connection_adapters/abstract/quoting.rb +187 -60
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +9 -7
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +157 -93
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +485 -253
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +79 -36
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +909 -263
- data/lib/active_record/connection_adapters/abstract/transaction.rb +254 -92
- data/lib/active_record/connection_adapters/abstract_adapter.rb +492 -221
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +580 -608
- 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 +196 -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 +271 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +40 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +81 -199
- 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 +78 -161
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +49 -57
- 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 +5 -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 +17 -13
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +6 -3
- 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 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +67 -51
- 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 +171 -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 +499 -293
- 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 +595 -382
- data/lib/active_record/connection_adapters/schema_cache.rb +191 -29
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +45 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +146 -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 -389
- 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 +488 -243
- data/lib/active_record/counter_cache.rb +71 -50
- data/lib/active_record/database_configurations/connection_url_resolver.rb +99 -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 +273 -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 +212 -94
- data/lib/active_record/errors.rb +225 -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 +11 -6
- 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 +273 -496
- 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 +64 -0
- data/lib/active_record/legacy_yaml_adapter.rb +52 -0
- data/lib/active_record/locale/en.yml +3 -2
- data/lib/active_record/locking/optimistic.rb +103 -95
- 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 +298 -0
- data/lib/active_record/migration/join_table.rb +8 -7
- data/lib/active_record/migration.rb +685 -309
- data/lib/active_record/model_schema.rb +420 -113
- data/lib/active_record/nested_attributes.rb +265 -216
- data/lib/active_record/no_touching.rb +15 -2
- data/lib/active_record/null_relation.rb +24 -38
- data/lib/active_record/persistence.rb +574 -135
- data/lib/active_record/query_cache.rb +29 -23
- data/lib/active_record/querying.rb +50 -31
- data/lib/active_record/railtie.rb +175 -54
- 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 +533 -216
- data/lib/active_record/readonly_attributes.rb +9 -4
- data/lib/active_record/reflection.rb +485 -310
- 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 +326 -244
- data/lib/active_record/relation/delegation.rb +76 -84
- data/lib/active_record/relation/finder_methods.rb +318 -256
- data/lib/active_record/relation/from_clause.rb +30 -0
- data/lib/active_record/relation/merger.rb +99 -84
- data/lib/active_record/relation/predicate_builder/array_handler.rb +26 -25
- 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 +139 -96
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +757 -409
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +23 -21
- data/lib/active_record/relation/where_clause.rb +239 -0
- data/lib/active_record/relation.rb +554 -342
- 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 -26
- data/lib/active_record/scoping/default.rb +96 -82
- 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 +133 -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 +366 -129
- data/lib/active_record/tasks/mysql_database_tasks.rb +68 -100
- data/lib/active_record/tasks/postgresql_database_tasks.rb +87 -39
- 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 +291 -0
- data/lib/active_record/timestamp.rb +86 -43
- data/lib/active_record/touch_later.rb +65 -0
- data/lib/active_record/transactions.rb +181 -152
- 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 -41
- data/lib/active_record/type/date_time.rb +4 -38
- data/lib/active_record/type/decimal_without_scale.rb +6 -2
- data/lib/active_record/type/hash_lookup_type_map.rb +12 -5
- 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 +33 -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 +65 -48
- data/lib/active_record/validations.rb +39 -35
- data/lib/active_record/version.rb +3 -1
- data/lib/active_record.rb +44 -28
- 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 -10
- 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.rb +7 -5
- metadata +175 -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 -149
- data/lib/active_record/attribute_decorators.rb +0 -66
- data/lib/active_record/attribute_set/builder.rb +0 -86
- data/lib/active_record/attribute_set.rb +0 -77
- 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 -30
- data/lib/active_record/type/decimal.rb +0 -40
- 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 -55
- 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 -36
- data/lib/active_record/type/time_value.rb +0 -38
- data/lib/active_record/type/value.rb +0 -101
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +0 -22
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -10
- /data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
@@ -0,0 +1,273 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "uri"
|
4
|
+
require "active_record/database_configurations/database_config"
|
5
|
+
require "active_record/database_configurations/hash_config"
|
6
|
+
require "active_record/database_configurations/url_config"
|
7
|
+
require "active_record/database_configurations/connection_url_resolver"
|
8
|
+
|
9
|
+
module ActiveRecord
|
10
|
+
# ActiveRecord::DatabaseConfigurations returns an array of DatabaseConfig
|
11
|
+
# objects (either a HashConfig or UrlConfig) that are constructed from the
|
12
|
+
# application's database configuration hash or URL string.
|
13
|
+
class DatabaseConfigurations
|
14
|
+
class InvalidConfigurationError < StandardError; end
|
15
|
+
|
16
|
+
attr_reader :configurations
|
17
|
+
delegate :any?, to: :configurations
|
18
|
+
|
19
|
+
def initialize(configurations = {})
|
20
|
+
@configurations = build_configs(configurations)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Collects the configs for the environment and optionally the specification
|
24
|
+
# name passed in. To include replica configurations pass <tt>include_replicas: true</tt>.
|
25
|
+
#
|
26
|
+
# If a name is provided a single DatabaseConfig object will be
|
27
|
+
# returned, otherwise an array of DatabaseConfig objects will be
|
28
|
+
# returned that corresponds with the environment and type requested.
|
29
|
+
#
|
30
|
+
# ==== Options
|
31
|
+
#
|
32
|
+
# * <tt>env_name:</tt> The environment name. Defaults to +nil+ which will collect
|
33
|
+
# configs for all environments.
|
34
|
+
# * <tt>name:</tt> The db config name (i.e. primary, animals, etc.). Defaults
|
35
|
+
# to +nil+. If no +env_name+ is specified the config for the default env and the
|
36
|
+
# passed +name+ will be returned.
|
37
|
+
# * <tt>include_replicas:</tt> Determines whether to include replicas in
|
38
|
+
# the returned list. Most of the time we're only iterating over the write
|
39
|
+
# connection (i.e. migrations don't need to run for the write and read connection).
|
40
|
+
# Defaults to +false+.
|
41
|
+
def configs_for(env_name: nil, spec_name: nil, name: nil, include_replicas: false)
|
42
|
+
if spec_name
|
43
|
+
name = spec_name
|
44
|
+
ActiveSupport::Deprecation.warn("The kwarg `spec_name` is deprecated in favor of `name`. `spec_name` will be removed in Rails 7.0")
|
45
|
+
end
|
46
|
+
|
47
|
+
env_name ||= default_env if name
|
48
|
+
configs = env_with_configs(env_name)
|
49
|
+
|
50
|
+
unless include_replicas
|
51
|
+
configs = configs.select do |db_config|
|
52
|
+
!db_config.replica?
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
if name
|
57
|
+
configs.find do |db_config|
|
58
|
+
db_config.name == name
|
59
|
+
end
|
60
|
+
else
|
61
|
+
configs
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Returns the config hash that corresponds with the environment
|
66
|
+
#
|
67
|
+
# If the application has multiple databases +default_hash+ will
|
68
|
+
# return the first config hash for the environment.
|
69
|
+
#
|
70
|
+
# { database: "my_db", adapter: "mysql2" }
|
71
|
+
def default_hash(env = default_env)
|
72
|
+
default = find_db_config(env)
|
73
|
+
default.configuration_hash if default
|
74
|
+
end
|
75
|
+
alias :[] :default_hash
|
76
|
+
deprecate "[]": "Use configs_for", default_hash: "Use configs_for"
|
77
|
+
|
78
|
+
# Returns a single DatabaseConfig object based on the requested environment.
|
79
|
+
#
|
80
|
+
# If the application has multiple databases +find_db_config+ will return
|
81
|
+
# the first DatabaseConfig for the environment.
|
82
|
+
def find_db_config(env)
|
83
|
+
configurations
|
84
|
+
.sort_by.with_index { |db_config, i| db_config.for_current_env? ? [0, i] : [1, i] }
|
85
|
+
.find do |db_config|
|
86
|
+
db_config.env_name == env.to_s ||
|
87
|
+
(db_config.for_current_env? && db_config.name == env.to_s)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# A primary configuration is one that is named primary or if there is
|
92
|
+
# no primary, the first configuration for an environment will be treated
|
93
|
+
# as primary. This is used as the "default" configuration and is used
|
94
|
+
# when the application needs to treat one configuration differently. For
|
95
|
+
# example, when Rails dumps the schema, the primary configuration's schema
|
96
|
+
# file will be named `schema.rb` instead of `primary_schema.rb`.
|
97
|
+
def primary?(name) # :nodoc:
|
98
|
+
return true if name == "primary"
|
99
|
+
|
100
|
+
first_config = find_db_config(default_env)
|
101
|
+
first_config && name == first_config.name
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns the DatabaseConfigurations object as a Hash.
|
105
|
+
def to_h
|
106
|
+
configurations.inject({}) do |memo, db_config|
|
107
|
+
memo.merge(db_config.env_name => db_config.configuration_hash.stringify_keys)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
deprecate to_h: "You can use `ActiveRecord::Base.configurations.configs_for(env_name: 'env', name: 'primary').configuration_hash` to get the configuration hashes."
|
111
|
+
|
112
|
+
# Checks if the application's configurations are empty.
|
113
|
+
#
|
114
|
+
# Aliased to blank?
|
115
|
+
def empty?
|
116
|
+
configurations.empty?
|
117
|
+
end
|
118
|
+
alias :blank? :empty?
|
119
|
+
|
120
|
+
# Returns fully resolved connection, accepts hash, string or symbol.
|
121
|
+
# Always returns a DatabaseConfiguration::DatabaseConfig
|
122
|
+
#
|
123
|
+
# == Examples
|
124
|
+
#
|
125
|
+
# Symbol representing current environment.
|
126
|
+
#
|
127
|
+
# DatabaseConfigurations.new("production" => {}).resolve(:production)
|
128
|
+
# # => DatabaseConfigurations::HashConfig.new(env_name: "production", config: {})
|
129
|
+
#
|
130
|
+
# One layer deep hash of connection values.
|
131
|
+
#
|
132
|
+
# DatabaseConfigurations.new({}).resolve("adapter" => "sqlite3")
|
133
|
+
# # => DatabaseConfigurations::HashConfig.new(config: {"adapter" => "sqlite3"})
|
134
|
+
#
|
135
|
+
# Connection URL.
|
136
|
+
#
|
137
|
+
# DatabaseConfigurations.new({}).resolve("postgresql://localhost/foo")
|
138
|
+
# # => DatabaseConfigurations::UrlConfig.new(config: {"adapter" => "postgresql", "host" => "localhost", "database" => "foo"})
|
139
|
+
def resolve(config) # :nodoc:
|
140
|
+
return config if DatabaseConfigurations::DatabaseConfig === config
|
141
|
+
|
142
|
+
case config
|
143
|
+
when Symbol
|
144
|
+
resolve_symbol_connection(config)
|
145
|
+
when Hash, String
|
146
|
+
build_db_config_from_raw_config(default_env, "primary", config)
|
147
|
+
else
|
148
|
+
raise TypeError, "Invalid type for configuration. Expected Symbol, String, or Hash. Got #{config.inspect}"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
def default_env
|
154
|
+
ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_s
|
155
|
+
end
|
156
|
+
|
157
|
+
def env_with_configs(env = nil)
|
158
|
+
if env
|
159
|
+
configurations.select { |db_config| db_config.env_name == env }
|
160
|
+
else
|
161
|
+
configurations
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def build_configs(configs)
|
166
|
+
return configs.configurations if configs.is_a?(DatabaseConfigurations)
|
167
|
+
return configs if configs.is_a?(Array)
|
168
|
+
|
169
|
+
db_configs = configs.flat_map do |env_name, config|
|
170
|
+
if config.is_a?(Hash) && config.all? { |_, v| v.is_a?(Hash) }
|
171
|
+
walk_configs(env_name.to_s, config)
|
172
|
+
else
|
173
|
+
build_db_config_from_raw_config(env_name.to_s, "primary", config)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
unless db_configs.find(&:for_current_env?)
|
178
|
+
db_configs << environment_url_config(default_env, "primary", {})
|
179
|
+
end
|
180
|
+
|
181
|
+
merge_db_environment_variables(default_env, db_configs.compact)
|
182
|
+
end
|
183
|
+
|
184
|
+
def walk_configs(env_name, config)
|
185
|
+
config.map do |name, sub_config|
|
186
|
+
build_db_config_from_raw_config(env_name, name.to_s, sub_config)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def resolve_symbol_connection(name)
|
191
|
+
if db_config = find_db_config(name)
|
192
|
+
db_config
|
193
|
+
else
|
194
|
+
raise AdapterNotSpecified, <<~MSG
|
195
|
+
The `#{name}` database is not configured for the `#{default_env}` environment.
|
196
|
+
|
197
|
+
Available databases configurations are:
|
198
|
+
|
199
|
+
#{build_configuration_sentence}
|
200
|
+
MSG
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def build_configuration_sentence
|
205
|
+
configs = configs_for(include_replicas: true)
|
206
|
+
|
207
|
+
configs.group_by(&:env_name).map do |env, config|
|
208
|
+
names = config.map(&:name)
|
209
|
+
if names.size > 1
|
210
|
+
"#{env}: #{names.join(", ")}"
|
211
|
+
else
|
212
|
+
env
|
213
|
+
end
|
214
|
+
end.join("\n")
|
215
|
+
end
|
216
|
+
|
217
|
+
def build_db_config_from_raw_config(env_name, name, config)
|
218
|
+
case config
|
219
|
+
when String
|
220
|
+
build_db_config_from_string(env_name, name, config)
|
221
|
+
when Hash
|
222
|
+
build_db_config_from_hash(env_name, name, config.symbolize_keys)
|
223
|
+
else
|
224
|
+
raise InvalidConfigurationError, "'{ #{env_name} => #{config} }' is not a valid configuration. Expected '#{config}' to be a URL string or a Hash."
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
def build_db_config_from_string(env_name, name, config)
|
229
|
+
url = config
|
230
|
+
uri = URI.parse(url)
|
231
|
+
if uri.scheme
|
232
|
+
UrlConfig.new(env_name, name, url)
|
233
|
+
else
|
234
|
+
raise InvalidConfigurationError, "'{ #{env_name} => #{config} }' is not a valid configuration. Expected '#{config}' to be a URL string or a Hash."
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def build_db_config_from_hash(env_name, name, config)
|
239
|
+
if config.has_key?(:url)
|
240
|
+
url = config[:url]
|
241
|
+
config_without_url = config.dup
|
242
|
+
config_without_url.delete :url
|
243
|
+
|
244
|
+
UrlConfig.new(env_name, name, url, config_without_url)
|
245
|
+
else
|
246
|
+
HashConfig.new(env_name, name, config)
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
def merge_db_environment_variables(current_env, configs)
|
251
|
+
configs.map do |config|
|
252
|
+
next config if config.is_a?(UrlConfig) || config.env_name != current_env
|
253
|
+
|
254
|
+
url_config = environment_url_config(current_env, config.name, config.configuration_hash)
|
255
|
+
url_config || config
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def environment_url_config(env, name, config)
|
260
|
+
url = environment_value_for(name)
|
261
|
+
return unless url
|
262
|
+
|
263
|
+
UrlConfig.new(env, name, url, config)
|
264
|
+
end
|
265
|
+
|
266
|
+
def environment_value_for(name)
|
267
|
+
name_env_key = "#{name.upcase}_DATABASE_URL"
|
268
|
+
url = ENV[name_env_key]
|
269
|
+
url ||= ENV["DATABASE_URL"] if name == "primary"
|
270
|
+
url
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
@@ -0,0 +1,209 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/string/inquiry"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
# == Delegated types
|
7
|
+
#
|
8
|
+
# Class hierarchies can map to relational database tables in many ways. Active Record, for example, offers
|
9
|
+
# purely abstract classes, where the superclass doesn't persist any attributes, and single-table inheritance,
|
10
|
+
# where all attributes from all levels of the hierarchy are represented in a single table. Both have their
|
11
|
+
# places, but neither are without their drawbacks.
|
12
|
+
#
|
13
|
+
# The problem with purely abstract classes is that all concrete subclasses must persist all the shared
|
14
|
+
# attributes themselves in their own tables (also known as class-table inheritance). This makes it hard to
|
15
|
+
# do queries across the hierarchy. For example, imagine you have the following hierarchy:
|
16
|
+
#
|
17
|
+
# Entry < ApplicationRecord
|
18
|
+
# Message < Entry
|
19
|
+
# Comment < Entry
|
20
|
+
#
|
21
|
+
# How do you show a feed that has both +Message+ and +Comment+ records, which can be easily paginated?
|
22
|
+
# Well, you can't! Messages are backed by a messages table and comments by a comments table. You can't
|
23
|
+
# pull from both tables at once and use a consistent OFFSET/LIMIT scheme.
|
24
|
+
#
|
25
|
+
# You can get around the pagination problem by using single-table inheritance, but now you're forced into
|
26
|
+
# a single mega table with all the attributes from all subclasses. No matter how divergent. If a Message
|
27
|
+
# has a subject, but the comment does not, well, now the comment does anyway! So STI works best when there's
|
28
|
+
# little divergence between the subclasses and their attributes.
|
29
|
+
#
|
30
|
+
# But there's a third way: Delegated types. With this approach, the "superclass" is a concrete class
|
31
|
+
# that is represented by its own table, where all the superclass attributes that are shared amongst all the
|
32
|
+
# "subclasses" are stored. And then each of the subclasses have their own individual tables for additional
|
33
|
+
# attributes that are particular to their implementation. This is similar to what's called multi-table
|
34
|
+
# inheritance in Django, but instead of actual inheritance, this approach uses delegation to form the
|
35
|
+
# hierarchy and share responsibilities.
|
36
|
+
#
|
37
|
+
# Let's look at that entry/message/comment example using delegated types:
|
38
|
+
#
|
39
|
+
# # Schema: entries[ id, account_id, creator_id, created_at, updated_at, entryable_type, entryable_id ]
|
40
|
+
# class Entry < ApplicationRecord
|
41
|
+
# belongs_to :account
|
42
|
+
# belongs_to :creator
|
43
|
+
# delegated_type :entryable, types: %w[ Message Comment ]
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# module Entryable
|
47
|
+
# extend ActiveSupport::Concern
|
48
|
+
#
|
49
|
+
# included do
|
50
|
+
# has_one :entry, as: :entryable, touch: true
|
51
|
+
# end
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# # Schema: messages[ id, subject ]
|
55
|
+
# class Message < ApplicationRecord
|
56
|
+
# include Entryable
|
57
|
+
# has_rich_text :content
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# # Schema: comments[ id, content ]
|
61
|
+
# class Comment < ApplicationRecord
|
62
|
+
# include Entryable
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# As you can see, neither +Message+ nor +Comment+ are meant to stand alone. Crucial metadata for both classes
|
66
|
+
# resides in the +Entry+ "superclass". But the +Entry+ absolutely can stand alone in terms of querying capacity
|
67
|
+
# in particular. You can now easily do things like:
|
68
|
+
#
|
69
|
+
# Account.entries.order(created_at: :desc).limit(50)
|
70
|
+
#
|
71
|
+
# Which is exactly what you want when displaying both comments and messages together. The entry itself can
|
72
|
+
# be rendered as its delegated type easily, like so:
|
73
|
+
#
|
74
|
+
# # entries/_entry.html.erb
|
75
|
+
# <%= render "entries/entryables/#{entry.entryable_name}", entry: entry %>
|
76
|
+
#
|
77
|
+
# # entries/entryables/_message.html.erb
|
78
|
+
# <div class="message">
|
79
|
+
# Posted on <%= entry.created_at %> by <%= entry.creator.name %>: <%= entry.message.content %>
|
80
|
+
# </div>
|
81
|
+
#
|
82
|
+
# # entries/entryables/_comment.html.erb
|
83
|
+
# <div class="comment">
|
84
|
+
# <%= entry.creator.name %> said: <%= entry.comment.content %>
|
85
|
+
# </div>
|
86
|
+
#
|
87
|
+
# == Sharing behavior with concerns and controllers
|
88
|
+
#
|
89
|
+
# The entry "superclass" also serves as a perfect place to put all that shared logic that applies to both
|
90
|
+
# messages and comments, and which acts primarily on the shared attributes. Imagine:
|
91
|
+
#
|
92
|
+
# class Entry < ApplicationRecord
|
93
|
+
# include Eventable, Forwardable, Redeliverable
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# Which allows you to have controllers for things like +ForwardsController+ and +RedeliverableController+
|
97
|
+
# that both act on entries, and thus provide the shared functionality to both messages and comments.
|
98
|
+
#
|
99
|
+
# == Creating new records
|
100
|
+
#
|
101
|
+
# You create a new record that uses delegated typing by creating the delegator and delegatee at the same time,
|
102
|
+
# like so:
|
103
|
+
#
|
104
|
+
# Entry.create! entryable: Comment.new(content: "Hello!"), creator: Current.user
|
105
|
+
#
|
106
|
+
# If you need more complicated composition, or you need to perform dependent validation, you should build a factory
|
107
|
+
# method or class to take care of the complicated needs. This could be as simple as:
|
108
|
+
#
|
109
|
+
# class Entry < ApplicationRecord
|
110
|
+
# def self.create_with_comment(content, creator: Current.user)
|
111
|
+
# create! entryable: Comment.new(content: content), creator: creator
|
112
|
+
# end
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
# == Adding further delegation
|
116
|
+
#
|
117
|
+
# The delegated type shouldn't just answer the question of what the underlying class is called. In fact, that's
|
118
|
+
# an anti-pattern most of the time. The reason you're building this hierarchy is to take advantage of polymorphism.
|
119
|
+
# So here's a simple example of that:
|
120
|
+
#
|
121
|
+
# class Entry < ApplicationRecord
|
122
|
+
# delegated_type :entryable, types: %w[ Message Comment ]
|
123
|
+
# delegate :title, to: :entryable
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# class Message < ApplicationRecord
|
127
|
+
# def title
|
128
|
+
# subject
|
129
|
+
# end
|
130
|
+
# end
|
131
|
+
#
|
132
|
+
# class Comment < ApplicationRecord
|
133
|
+
# def title
|
134
|
+
# content.truncate(20)
|
135
|
+
# end
|
136
|
+
# end
|
137
|
+
#
|
138
|
+
# Now you can list a bunch of entries, call +Entry#title+, and polymorphism will provide you with the answer.
|
139
|
+
module DelegatedType
|
140
|
+
# Defines this as a class that'll delegate its type for the passed +role+ to the class references in +types+.
|
141
|
+
# That'll create a polymorphic +belongs_to+ relationship to that +role+, and it'll add all the delegated
|
142
|
+
# type convenience methods:
|
143
|
+
#
|
144
|
+
# class Entry < ApplicationRecord
|
145
|
+
# delegated_type :entryable, types: %w[ Message Comment ], dependent: :destroy
|
146
|
+
# end
|
147
|
+
#
|
148
|
+
# Entry#entryable_class # => +Message+ or +Comment+
|
149
|
+
# Entry#entryable_name # => "message" or "comment"
|
150
|
+
# Entry.messages # => Entry.where(entryable_type: "Message")
|
151
|
+
# Entry#message? # => true when entryable_type == "Message"
|
152
|
+
# Entry#message # => returns the message record, when entryable_type == "Message", otherwise nil
|
153
|
+
# Entry#message_id # => returns entryable_id, when entryable_type == "Message", otherwise nil
|
154
|
+
# Entry.comments # => Entry.where(entryable_type: "Comment")
|
155
|
+
# Entry#comment? # => true when entryable_type == "Comment"
|
156
|
+
# Entry#comment # => returns the comment record, when entryable_type == "Comment", otherwise nil
|
157
|
+
# Entry#comment_id # => returns entryable_id, when entryable_type == "Comment", otherwise nil
|
158
|
+
#
|
159
|
+
# The +options+ are passed directly to the +belongs_to+ call, so this is where you declare +dependent+ etc.
|
160
|
+
#
|
161
|
+
# You can also declare namespaced types:
|
162
|
+
#
|
163
|
+
# class Entry < ApplicationRecord
|
164
|
+
# delegated_type :entryable, types: %w[ Message Comment Access::NoticeMessage ], dependent: :destroy
|
165
|
+
# end
|
166
|
+
#
|
167
|
+
# Entry.access_notice_messages
|
168
|
+
# entry.access_notice_message
|
169
|
+
# entry.access_notice_message?
|
170
|
+
def delegated_type(role, types:, **options)
|
171
|
+
belongs_to role, options.delete(:scope), **options.merge(polymorphic: true)
|
172
|
+
define_delegated_type_methods role, types: types
|
173
|
+
end
|
174
|
+
|
175
|
+
private
|
176
|
+
def define_delegated_type_methods(role, types:)
|
177
|
+
role_type = "#{role}_type"
|
178
|
+
role_id = "#{role}_id"
|
179
|
+
|
180
|
+
define_method "#{role}_class" do
|
181
|
+
public_send("#{role}_type").constantize
|
182
|
+
end
|
183
|
+
|
184
|
+
define_method "#{role}_name" do
|
185
|
+
public_send("#{role}_class").model_name.singular.inquiry
|
186
|
+
end
|
187
|
+
|
188
|
+
types.each do |type|
|
189
|
+
scope_name = type.tableize.gsub("/", "_")
|
190
|
+
singular = scope_name.singularize
|
191
|
+
query = "#{singular}?"
|
192
|
+
|
193
|
+
scope scope_name, -> { where(role_type => type) }
|
194
|
+
|
195
|
+
define_method query do
|
196
|
+
public_send(role_type) == type
|
197
|
+
end
|
198
|
+
|
199
|
+
define_method singular do
|
200
|
+
public_send(role) if public_send(query)
|
201
|
+
end
|
202
|
+
|
203
|
+
define_method "#{singular}_id" do
|
204
|
+
public_send(role_id) if public_send(query)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class DestroyAssociationAsyncError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
# Job to destroy the records associated with a destroyed record in background.
|
8
|
+
class DestroyAssociationAsyncJob < ActiveJob::Base
|
9
|
+
queue_as { ActiveRecord::Base.queues[:destroy] }
|
10
|
+
|
11
|
+
discard_on ActiveJob::DeserializationError
|
12
|
+
|
13
|
+
def perform(
|
14
|
+
owner_model_name: nil, owner_id: nil,
|
15
|
+
association_class: nil, association_ids: nil, association_primary_key_column: nil,
|
16
|
+
ensuring_owner_was_method: nil
|
17
|
+
)
|
18
|
+
association_model = association_class.constantize
|
19
|
+
owner_class = owner_model_name.constantize
|
20
|
+
owner = owner_class.find_by(owner_class.primary_key.to_sym => owner_id)
|
21
|
+
|
22
|
+
if !owner_destroyed?(owner, ensuring_owner_was_method)
|
23
|
+
raise DestroyAssociationAsyncError, "owner record not destroyed"
|
24
|
+
end
|
25
|
+
|
26
|
+
association_model.where(association_primary_key_column => association_ids).find_each do |r|
|
27
|
+
r.destroy
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def owner_destroyed?(owner, ensuring_owner_was_method)
|
33
|
+
!owner || (ensuring_owner_was_method && owner.public_send(ensuring_owner_was_method))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|