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,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord::Associations::Builder # :nodoc:
|
2
4
|
class HasMany < CollectionAssociation #:nodoc:
|
3
5
|
def self.macro
|
@@ -5,11 +7,17 @@ module ActiveRecord::Associations::Builder # :nodoc:
|
|
5
7
|
end
|
6
8
|
|
7
9
|
def self.valid_options(options)
|
8
|
-
super + [:
|
10
|
+
valid = super + [:counter_cache, :join_table, :index_errors, :ensuring_owner_was]
|
11
|
+
valid += [:as, :foreign_type] if options[:as]
|
12
|
+
valid += [:through, :source, :source_type] if options[:through]
|
13
|
+
valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async
|
14
|
+
valid
|
9
15
|
end
|
10
16
|
|
11
17
|
def self.valid_dependent_options
|
12
|
-
[:destroy, :delete_all, :nullify, :restrict_with_error, :restrict_with_exception]
|
18
|
+
[:destroy, :delete_all, :nullify, :restrict_with_error, :restrict_with_exception, :destroy_async]
|
13
19
|
end
|
20
|
+
|
21
|
+
private_class_method :macro, :valid_options, :valid_dependent_options
|
14
22
|
end
|
15
23
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord::Associations::Builder # :nodoc:
|
2
4
|
class HasOne < SingularAssociation #:nodoc:
|
3
5
|
def self.macro
|
@@ -5,13 +7,20 @@ module ActiveRecord::Associations::Builder # :nodoc:
|
|
5
7
|
end
|
6
8
|
|
7
9
|
def self.valid_options(options)
|
8
|
-
valid = super
|
10
|
+
valid = super
|
11
|
+
valid += [:as, :foreign_type] if options[:as]
|
12
|
+
valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async
|
9
13
|
valid += [:through, :source, :source_type] if options[:through]
|
10
14
|
valid
|
11
15
|
end
|
12
16
|
|
13
17
|
def self.valid_dependent_options
|
14
|
-
[:destroy, :delete, :nullify, :restrict_with_error, :restrict_with_exception]
|
18
|
+
[:destroy, :destroy_async, :delete, :nullify, :restrict_with_error, :restrict_with_exception]
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.define_callbacks(model, reflection)
|
22
|
+
super
|
23
|
+
add_touch_callbacks(model, reflection) if reflection.options[:touch]
|
15
24
|
end
|
16
25
|
|
17
26
|
def self.add_destroy_callbacks(model, reflection)
|
@@ -24,5 +33,29 @@ module ActiveRecord::Associations::Builder # :nodoc:
|
|
24
33
|
model.validates_presence_of reflection.name, message: :required
|
25
34
|
end
|
26
35
|
end
|
36
|
+
|
37
|
+
def self.touch_record(record, name, touch)
|
38
|
+
instance = record.send(name)
|
39
|
+
|
40
|
+
if instance&.persisted?
|
41
|
+
touch != true ?
|
42
|
+
instance.touch(touch) : instance.touch
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.add_touch_callbacks(model, reflection)
|
47
|
+
name = reflection.name
|
48
|
+
touch = reflection.options[:touch]
|
49
|
+
|
50
|
+
callback = -> (record) { HasOne.touch_record(record, name, touch) }
|
51
|
+
model.after_create callback, if: :saved_changes?
|
52
|
+
model.after_create_commit { association(name).reset_negative_cache }
|
53
|
+
model.after_update callback, if: :saved_changes?
|
54
|
+
model.after_destroy callback
|
55
|
+
model.after_touch callback
|
56
|
+
end
|
57
|
+
|
58
|
+
private_class_method :macro, :valid_options, :valid_dependent_options, :add_destroy_callbacks,
|
59
|
+
:define_callbacks, :define_validations, :add_touch_callbacks
|
27
60
|
end
|
28
61
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# This class is inherited by the has_one and belongs_to association classes
|
2
4
|
|
3
5
|
module ActiveRecord::Associations::Builder # :nodoc:
|
4
6
|
class SingularAssociation < Association #:nodoc:
|
5
7
|
def self.valid_options(options)
|
6
|
-
super + [:
|
8
|
+
super + [:required, :touch]
|
7
9
|
end
|
8
10
|
|
9
11
|
def self.define_accessors(model, reflection)
|
@@ -36,5 +38,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
|
|
36
38
|
end
|
37
39
|
CODE
|
38
40
|
end
|
41
|
+
|
42
|
+
private_class_method :valid_options, :define_accessors, :define_constructors
|
39
43
|
end
|
40
44
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module Associations
|
3
5
|
# = Active Record Association Collection
|
@@ -10,9 +12,9 @@ module ActiveRecord
|
|
10
12
|
# HasManyAssociation => has_many
|
11
13
|
# HasManyThroughAssociation + ThroughAssociation => has_many :through
|
12
14
|
#
|
13
|
-
# CollectionAssociation class provides common methods to the collections
|
15
|
+
# The CollectionAssociation class provides common methods to the collections
|
14
16
|
# defined by +has_and_belongs_to_many+, +has_many+ or +has_many+ with
|
15
|
-
# +:through association+ option.
|
17
|
+
# the +:through association+ option.
|
16
18
|
#
|
17
19
|
# You need to be careful with assumptions regarding the target: The proxy
|
18
20
|
# does not fetch records from the database until it needs them, but new
|
@@ -24,18 +26,9 @@ module ActiveRecord
|
|
24
26
|
# If you need to work on all current children, new and existing records,
|
25
27
|
# +load_target+ and the +loaded+ flag are your friends.
|
26
28
|
class CollectionAssociation < Association #:nodoc:
|
27
|
-
|
28
29
|
# Implements the reader method, e.g. foo.items for Foo.has_many :items
|
29
|
-
def reader
|
30
|
-
if
|
31
|
-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
32
|
-
Passing an argument to force an association to reload is now
|
33
|
-
deprecated and will be removed in Rails 5.1. Please call `reload`
|
34
|
-
on the result collection proxy instead.
|
35
|
-
MSG
|
36
|
-
|
37
|
-
klass.uncached { reload }
|
38
|
-
elsif stale_target?
|
30
|
+
def reader
|
31
|
+
if stale_target?
|
39
32
|
reload
|
40
33
|
end
|
41
34
|
|
@@ -51,30 +44,29 @@ module ActiveRecord
|
|
51
44
|
# Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
|
52
45
|
def ids_reader
|
53
46
|
if loaded?
|
54
|
-
|
55
|
-
|
56
|
-
|
47
|
+
target.pluck(reflection.association_primary_key)
|
48
|
+
elsif !target.empty?
|
49
|
+
load_target.pluck(reflection.association_primary_key)
|
57
50
|
else
|
58
|
-
@association_ids ||= (
|
59
|
-
column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}"
|
60
|
-
scope.pluck(column)
|
61
|
-
)
|
51
|
+
@association_ids ||= scope.pluck(reflection.association_primary_key)
|
62
52
|
end
|
63
53
|
end
|
64
54
|
|
65
55
|
# Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items
|
66
56
|
def ids_writer(ids)
|
67
|
-
|
68
|
-
|
57
|
+
primary_key = reflection.association_primary_key
|
58
|
+
pk_type = klass.type_for_attribute(primary_key)
|
59
|
+
ids = Array(ids).compact_blank
|
69
60
|
ids.map! { |i| pk_type.cast(i) }
|
70
61
|
|
71
|
-
primary_key = reflection.association_primary_key
|
72
62
|
records = klass.where(primary_key => ids).index_by do |r|
|
73
63
|
r.public_send(primary_key)
|
74
64
|
end.values_at(*ids).compact
|
75
65
|
|
76
66
|
if records.size != ids.size
|
77
|
-
|
67
|
+
found_ids = records.map { |record| record.public_send(primary_key) }
|
68
|
+
not_found_ids = ids - found_ids
|
69
|
+
klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, primary_key, not_found_ids)
|
78
70
|
else
|
79
71
|
replace(records)
|
80
72
|
end
|
@@ -83,104 +75,42 @@ module ActiveRecord
|
|
83
75
|
def reset
|
84
76
|
super
|
85
77
|
@target = []
|
86
|
-
|
87
|
-
|
88
|
-
def select(*fields)
|
89
|
-
if block_given?
|
90
|
-
load_target.select.each { |e| yield e }
|
91
|
-
else
|
92
|
-
scope.select(*fields)
|
93
|
-
end
|
78
|
+
@association_ids = nil
|
94
79
|
end
|
95
80
|
|
96
81
|
def find(*args)
|
97
|
-
if
|
98
|
-
|
99
|
-
|
100
|
-
if options[:inverse_of] && loaded?
|
101
|
-
args_flatten = args.flatten
|
102
|
-
raise RecordNotFound, "Couldn't find #{scope.klass.name} without an ID" if args_flatten.blank?
|
103
|
-
result = find_by_scan(*args)
|
104
|
-
|
105
|
-
result_size = Array(result).size
|
106
|
-
if !result || result_size != args_flatten.size
|
107
|
-
scope.raise_record_not_found_exception!(args_flatten, result_size, args_flatten.size)
|
108
|
-
else
|
109
|
-
result
|
110
|
-
end
|
111
|
-
else
|
112
|
-
scope.find(*args)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def first(*args)
|
118
|
-
first_nth_or_last(:first, *args)
|
119
|
-
end
|
120
|
-
|
121
|
-
def second(*args)
|
122
|
-
first_nth_or_last(:second, *args)
|
123
|
-
end
|
124
|
-
|
125
|
-
def third(*args)
|
126
|
-
first_nth_or_last(:third, *args)
|
127
|
-
end
|
82
|
+
if options[:inverse_of] && loaded?
|
83
|
+
args_flatten = args.flatten
|
84
|
+
model = scope.klass
|
128
85
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
def fifth(*args)
|
134
|
-
first_nth_or_last(:fifth, *args)
|
135
|
-
end
|
136
|
-
|
137
|
-
def forty_two(*args)
|
138
|
-
first_nth_or_last(:forty_two, *args)
|
139
|
-
end
|
140
|
-
|
141
|
-
def third_to_last(*args)
|
142
|
-
first_nth_or_last(:third_to_last, *args)
|
143
|
-
end
|
144
|
-
|
145
|
-
def second_to_last(*args)
|
146
|
-
first_nth_or_last(:second_to_last, *args)
|
147
|
-
end
|
86
|
+
if args_flatten.blank?
|
87
|
+
error_message = "Couldn't find #{model.name} without an ID"
|
88
|
+
raise RecordNotFound.new(error_message, model.name, model.primary_key, args)
|
89
|
+
end
|
148
90
|
|
149
|
-
|
150
|
-
first_nth_or_last(:last, *args)
|
151
|
-
end
|
91
|
+
result = find_by_scan(*args)
|
152
92
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
set_inverse_instance record if record.is_a? ActiveRecord::Base
|
93
|
+
result_size = Array(result).size
|
94
|
+
if !result || result_size != args_flatten.size
|
95
|
+
scope.raise_record_not_found_exception!(args_flatten, result_size, args_flatten.size)
|
96
|
+
else
|
97
|
+
result
|
159
98
|
end
|
99
|
+
else
|
100
|
+
scope.find(*args)
|
160
101
|
end
|
161
102
|
end
|
162
103
|
|
163
|
-
def build(attributes =
|
104
|
+
def build(attributes = nil, &block)
|
164
105
|
if attributes.is_a?(Array)
|
165
106
|
attributes.collect { |attr| build(attr, &block) }
|
166
107
|
else
|
167
|
-
add_to_target(build_record(attributes)
|
168
|
-
yield(record) if block_given?
|
169
|
-
end
|
108
|
+
add_to_target(build_record(attributes, &block), replace: true)
|
170
109
|
end
|
171
110
|
end
|
172
111
|
|
173
|
-
|
174
|
-
|
175
|
-
end
|
176
|
-
|
177
|
-
def create!(attributes = {}, &block)
|
178
|
-
_create_record(attributes, true, &block)
|
179
|
-
end
|
180
|
-
|
181
|
-
# Add +records+ to this association. Returns +self+ so method calls may
|
182
|
-
# be chained. Since << flattens its argument list and inserts each record,
|
183
|
-
# +push+ and +concat+ behave identically.
|
112
|
+
# Add +records+ to this association. Since +<<+ flattens its argument list
|
113
|
+
# and inserts each record, +push+ and +concat+ behave identically.
|
184
114
|
def concat(*records)
|
185
115
|
records = records.flatten
|
186
116
|
if owner.new_record?
|
@@ -225,12 +155,12 @@ module ActiveRecord
|
|
225
155
|
end
|
226
156
|
|
227
157
|
dependent = if dependent
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
158
|
+
dependent
|
159
|
+
elsif options[:dependent] == :destroy
|
160
|
+
:delete_all
|
161
|
+
else
|
162
|
+
options[:dependent]
|
163
|
+
end
|
234
164
|
|
235
165
|
delete_or_nullify_all_records(dependent).tap do
|
236
166
|
reset
|
@@ -248,28 +178,6 @@ module ActiveRecord
|
|
248
178
|
end
|
249
179
|
end
|
250
180
|
|
251
|
-
# Count all records using SQL. Construct options and pass them with
|
252
|
-
# scope to the target class's +count+.
|
253
|
-
def count(column_name = nil)
|
254
|
-
relation = scope
|
255
|
-
if association_scope.distinct_value
|
256
|
-
# This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.
|
257
|
-
column_name ||= reflection.klass.primary_key
|
258
|
-
relation = relation.distinct
|
259
|
-
end
|
260
|
-
|
261
|
-
value = relation.count(column_name)
|
262
|
-
|
263
|
-
limit = options[:limit]
|
264
|
-
offset = options[:offset]
|
265
|
-
|
266
|
-
if limit || offset
|
267
|
-
[ [value - offset.to_i, 0].max, limit.to_i ].min
|
268
|
-
else
|
269
|
-
value
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
181
|
# Removes +records+ from this association calling +before_remove+ and
|
274
182
|
# +after_remove+ callbacks.
|
275
183
|
#
|
@@ -278,12 +186,7 @@ module ActiveRecord
|
|
278
186
|
# are actually removed from the database, that depends precisely on
|
279
187
|
# +delete_records+. They are in any case removed from the collection.
|
280
188
|
def delete(*records)
|
281
|
-
|
282
|
-
_options = records.extract_options!
|
283
|
-
dependent = _options[:dependent] || options[:dependent]
|
284
|
-
|
285
|
-
records = find(records) if records.any? { |record| record.kind_of?(Integer) || record.kind_of?(String) }
|
286
|
-
delete_or_destroy(records, dependent)
|
189
|
+
delete_or_destroy(records, options[:dependent])
|
287
190
|
end
|
288
191
|
|
289
192
|
# Deletes the +records+ and removes them from this association calling
|
@@ -292,8 +195,6 @@ module ActiveRecord
|
|
292
195
|
# Note that this method removes records from the database ignoring the
|
293
196
|
# +:dependent+ option.
|
294
197
|
def destroy(*records)
|
295
|
-
return if records.empty?
|
296
|
-
records = find(records) if records.any? { |record| record.kind_of?(Integer) || record.kind_of?(String) }
|
297
198
|
delete_or_destroy(records, :destroy)
|
298
199
|
end
|
299
200
|
|
@@ -309,14 +210,12 @@ module ActiveRecord
|
|
309
210
|
# +count_records+, which is a method descendants have to provide.
|
310
211
|
def size
|
311
212
|
if !find_target? || loaded?
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
target.size
|
316
|
-
end
|
213
|
+
target.size
|
214
|
+
elsif @association_ids
|
215
|
+
@association_ids.size
|
317
216
|
elsif !association_scope.group_values.empty?
|
318
217
|
load_target.size
|
319
|
-
elsif !association_scope.distinct_value && target.
|
218
|
+
elsif !association_scope.distinct_value && !target.empty?
|
320
219
|
unsaved_records = target.select(&:new_record?)
|
321
220
|
unsaved_records.size + count_records
|
322
221
|
else
|
@@ -324,61 +223,22 @@ module ActiveRecord
|
|
324
223
|
end
|
325
224
|
end
|
326
225
|
|
327
|
-
# Returns the size of the collection calling +size+ on the target.
|
328
|
-
#
|
329
|
-
# If the collection has been already loaded +length+ and +size+ are
|
330
|
-
# equivalent. If not and you are going to need the records anyway this
|
331
|
-
# method will take one less query. Otherwise +size+ is more efficient.
|
332
|
-
def length
|
333
|
-
load_target.size
|
334
|
-
end
|
335
|
-
|
336
226
|
# Returns true if the collection is empty.
|
337
227
|
#
|
338
228
|
# If the collection has been loaded
|
339
229
|
# it is equivalent to <tt>collection.size.zero?</tt>. If the
|
340
230
|
# collection has not been loaded, it is equivalent to
|
341
|
-
# <tt
|
231
|
+
# <tt>!collection.exists?</tt>. If the collection has not already been
|
342
232
|
# loaded and you are going to fetch the records anyway it is better to
|
343
233
|
# check <tt>collection.length.zero?</tt>.
|
344
234
|
def empty?
|
345
|
-
if loaded?
|
235
|
+
if loaded? || @association_ids || reflection.has_cached_counter?
|
346
236
|
size.zero?
|
347
237
|
else
|
348
|
-
|
349
|
-
end
|
350
|
-
end
|
351
|
-
|
352
|
-
# Returns true if the collections is not empty.
|
353
|
-
# If block given, loads all records and checks for one or more matches.
|
354
|
-
# Otherwise, equivalent to +!collection.empty?+.
|
355
|
-
def any?
|
356
|
-
if block_given?
|
357
|
-
load_target.any? { |*block_args| yield(*block_args) }
|
358
|
-
else
|
359
|
-
!empty?
|
360
|
-
end
|
361
|
-
end
|
362
|
-
|
363
|
-
# Returns true if the collection has more than 1 record.
|
364
|
-
# If block given, loads all records and checks for two or more matches.
|
365
|
-
# Otherwise, equivalent to +collection.size > 1+.
|
366
|
-
def many?
|
367
|
-
if block_given?
|
368
|
-
load_target.many? { |*block_args| yield(*block_args) }
|
369
|
-
else
|
370
|
-
size > 1
|
238
|
+
target.empty? && !scope.exists?
|
371
239
|
end
|
372
240
|
end
|
373
241
|
|
374
|
-
def distinct
|
375
|
-
seen = {}
|
376
|
-
load_target.find_all do |record|
|
377
|
-
seen[record.id] = true unless seen.key?(record.id)
|
378
|
-
end
|
379
|
-
end
|
380
|
-
alias uniq distinct
|
381
|
-
|
382
242
|
# Replace this collection with +other_array+. This will perform a diff
|
383
243
|
# and delete/add only records that have changed.
|
384
244
|
def replace(other_array)
|
@@ -418,13 +278,24 @@ module ActiveRecord
|
|
418
278
|
target
|
419
279
|
end
|
420
280
|
|
421
|
-
def add_to_target(record, skip_callbacks
|
422
|
-
if association_scope.distinct_value
|
281
|
+
def add_to_target(record, skip_callbacks: false, replace: false, &block)
|
282
|
+
if replace || association_scope.distinct_value
|
423
283
|
index = @target.index(record)
|
424
284
|
end
|
425
285
|
replace_on_target(record, index, skip_callbacks, &block)
|
426
286
|
end
|
427
287
|
|
288
|
+
def target=(record)
|
289
|
+
return super unless ActiveRecord::Base.has_many_inversing
|
290
|
+
|
291
|
+
case record
|
292
|
+
when Array
|
293
|
+
super
|
294
|
+
else
|
295
|
+
add_to_target(record, skip_callbacks: true, replace: true)
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
428
299
|
def scope
|
429
300
|
scope = super
|
430
301
|
scope.none! if null_scope?
|
@@ -435,28 +306,15 @@ module ActiveRecord
|
|
435
306
|
owner.new_record? && !foreign_key_present?
|
436
307
|
end
|
437
308
|
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
StatementCache.create(conn) { |params|
|
445
|
-
as = AssociationScope.create { params.bind }
|
446
|
-
target_scope.merge as.scope(self, conn)
|
447
|
-
}
|
448
|
-
end
|
449
|
-
|
450
|
-
binds = AssociationScope.get_bind_values(owner, reflection.chain)
|
451
|
-
sc.execute(binds, klass, klass.connection, &block)
|
309
|
+
def find_from_target?
|
310
|
+
loaded? ||
|
311
|
+
owner.strict_loading? ||
|
312
|
+
reflection.strict_loading? ||
|
313
|
+
owner.new_record? ||
|
314
|
+
target.any? { |record| record.new_record? || record.changed? }
|
452
315
|
end
|
453
316
|
|
454
|
-
|
455
|
-
get_records do |record|
|
456
|
-
set_inverse_instance(record)
|
457
|
-
end
|
458
|
-
end
|
459
|
-
|
317
|
+
private
|
460
318
|
# We have some records loaded from the database (persisted) and some that are
|
461
319
|
# in-memory (memory). The same record may be represented in the persisted array
|
462
320
|
# and in the memory array.
|
@@ -474,7 +332,7 @@ module ActiveRecord
|
|
474
332
|
persisted.map! do |record|
|
475
333
|
if mem_record = memory.delete(record)
|
476
334
|
|
477
|
-
((record.attribute_names & mem_record.attribute_names) - mem_record.
|
335
|
+
((record.attribute_names & mem_record.attribute_names) - mem_record.changed_attribute_names_to_save).each do |name|
|
478
336
|
mem_record[name] = record[name]
|
479
337
|
end
|
480
338
|
|
@@ -495,12 +353,17 @@ module ActiveRecord
|
|
495
353
|
if attributes.is_a?(Array)
|
496
354
|
attributes.collect { |attr| _create_record(attr, raise, &block) }
|
497
355
|
else
|
356
|
+
record = build_record(attributes, &block)
|
498
357
|
transaction do
|
499
|
-
|
500
|
-
|
501
|
-
insert_record(record, true, raise) {
|
358
|
+
result = nil
|
359
|
+
add_to_target(record) do
|
360
|
+
result = insert_record(record, true, raise) {
|
361
|
+
@_was_loaded = loaded?
|
362
|
+
}
|
502
363
|
end
|
364
|
+
raise ActiveRecord::Rollback unless result
|
503
365
|
end
|
366
|
+
record
|
504
367
|
end
|
505
368
|
end
|
506
369
|
|
@@ -513,11 +376,9 @@ module ActiveRecord
|
|
513
376
|
end
|
514
377
|
end
|
515
378
|
|
516
|
-
def create_scope
|
517
|
-
scope.scope_for_create.stringify_keys
|
518
|
-
end
|
519
|
-
|
520
379
|
def delete_or_destroy(records, method)
|
380
|
+
return if records.empty?
|
381
|
+
records = find(records) if records.any? { |record| record.kind_of?(Integer) || record.kind_of?(String) }
|
521
382
|
records = records.flatten
|
522
383
|
records.each { |record| raise_on_type_mismatch!(record) }
|
523
384
|
existing_records = records.reject(&:new_record?)
|
@@ -530,24 +391,28 @@ module ActiveRecord
|
|
530
391
|
end
|
531
392
|
|
532
393
|
def remove_records(existing_records, records, method)
|
533
|
-
|
394
|
+
catch(:abort) do
|
395
|
+
records.each { |record| callback(:before_remove, record) }
|
396
|
+
end || return
|
534
397
|
|
535
398
|
delete_records(existing_records, method) if existing_records.any?
|
536
|
-
|
399
|
+
@target -= records
|
400
|
+
@association_ids = nil
|
537
401
|
|
538
402
|
records.each { |record| callback(:after_remove, record) }
|
539
403
|
end
|
540
404
|
|
541
|
-
# Delete the given records from the association,
|
542
|
-
#
|
405
|
+
# Delete the given records from the association,
|
406
|
+
# using one of the methods +:destroy+, +:delete_all+
|
407
|
+
# or +:nullify+ (or +nil+, in which case a default is used).
|
543
408
|
def delete_records(records, method)
|
544
409
|
raise NotImplementedError
|
545
410
|
end
|
546
411
|
|
547
412
|
def replace_records(new_target, original_target)
|
548
|
-
delete(target
|
413
|
+
delete(difference(target, new_target))
|
549
414
|
|
550
|
-
unless concat(new_target
|
415
|
+
unless concat(difference(new_target, target))
|
551
416
|
@target = original_target
|
552
417
|
raise RecordNotSaved, "Failed to replace #{reflection.name} because one or more of the " \
|
553
418
|
"new records could not be saved."
|
@@ -557,7 +422,7 @@ module ActiveRecord
|
|
557
422
|
end
|
558
423
|
|
559
424
|
def replace_common_records_in_memory(new_target, original_target)
|
560
|
-
common_records = new_target
|
425
|
+
common_records = intersection(new_target, original_target)
|
561
426
|
common_records.each do |record|
|
562
427
|
skip_callbacks = true
|
563
428
|
replace_on_target(record, @target.index(record), skip_callbacks)
|
@@ -570,15 +435,23 @@ module ActiveRecord
|
|
570
435
|
records.each do |record|
|
571
436
|
raise_on_type_mismatch!(record)
|
572
437
|
add_to_target(record) do
|
573
|
-
|
438
|
+
unless owner.new_record?
|
439
|
+
result &&= insert_record(record, true, raise) {
|
440
|
+
@_was_loaded = loaded?
|
441
|
+
}
|
442
|
+
end
|
574
443
|
end
|
575
444
|
end
|
576
445
|
|
577
|
-
|
446
|
+
raise ActiveRecord::Rollback unless result
|
447
|
+
|
448
|
+
records
|
578
449
|
end
|
579
450
|
|
580
451
|
def replace_on_target(record, index, skip_callbacks)
|
581
|
-
|
452
|
+
catch(:abort) do
|
453
|
+
callback(:before_add, record)
|
454
|
+
end || return unless skip_callbacks
|
582
455
|
|
583
456
|
set_inverse_instance(record)
|
584
457
|
|
@@ -589,6 +462,7 @@ module ActiveRecord
|
|
589
462
|
if index
|
590
463
|
target[index] = record
|
591
464
|
elsif @_was_loaded || !loaded?
|
465
|
+
@association_ids = nil
|
592
466
|
target << record
|
593
467
|
end
|
594
468
|
|
@@ -610,25 +484,6 @@ module ActiveRecord
|
|
610
484
|
owner.class.send(full_callback_name)
|
611
485
|
end
|
612
486
|
|
613
|
-
# Should we deal with assoc.first or assoc.last by issuing an independent query to
|
614
|
-
# the database, or by getting the target, and then taking the first/last item from that?
|
615
|
-
#
|
616
|
-
# If the args is just a non-empty options hash, go to the database.
|
617
|
-
#
|
618
|
-
# Otherwise, go to the database only if none of the following are true:
|
619
|
-
# * target already loaded
|
620
|
-
# * owner is new record
|
621
|
-
# * target contains new or changed record(s)
|
622
|
-
def fetch_first_nth_or_last_using_find?(args)
|
623
|
-
if args.first.is_a?(Hash)
|
624
|
-
true
|
625
|
-
else
|
626
|
-
!(loaded? ||
|
627
|
-
owner.new_record? ||
|
628
|
-
target.any? { |record| record.new_record? || record.changed? })
|
629
|
-
end
|
630
|
-
end
|
631
|
-
|
632
487
|
def include_in_memory?(record)
|
633
488
|
if reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
|
634
489
|
assoc = owner.association(reflection.through_reflection.name)
|
@@ -655,16 +510,6 @@ module ActiveRecord
|
|
655
510
|
load_target.select { |r| ids.include?(r.id.to_s) }
|
656
511
|
end
|
657
512
|
end
|
658
|
-
|
659
|
-
# Fetches the first/last using SQL if possible, otherwise from the target array.
|
660
|
-
def first_nth_or_last(type, *args)
|
661
|
-
args.shift if args.first.is_a?(Hash) && args.first.empty?
|
662
|
-
|
663
|
-
collection = fetch_first_nth_or_last_using_find?(args) ? scope : load_target
|
664
|
-
collection.send(type, *args).tap do |record|
|
665
|
-
set_inverse_instance record if record.is_a? ActiveRecord::Base
|
666
|
-
end
|
667
|
-
end
|
668
513
|
end
|
669
514
|
end
|
670
515
|
end
|