activerecord 4.2.11.1 → 6.0.3.5
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 +721 -1522
- data/MIT-LICENSE +4 -2
- data/README.rdoc +14 -13
- data/examples/performance.rb +33 -32
- data/examples/simple.rb +5 -4
- data/lib/active_record/advisory_lock_base.rb +18 -0
- data/lib/active_record/aggregations.rb +266 -251
- data/lib/active_record/association_relation.rb +20 -13
- data/lib/active_record/associations/alias_tracker.rb +29 -36
- data/lib/active_record/associations/association.rb +128 -57
- data/lib/active_record/associations/association_scope.rb +103 -132
- data/lib/active_record/associations/belongs_to_association.rb +65 -60
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -12
- data/lib/active_record/associations/builder/association.rb +27 -40
- data/lib/active_record/associations/builder/belongs_to.rb +69 -55
- data/lib/active_record/associations/builder/collection_association.rb +10 -33
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +50 -66
- data/lib/active_record/associations/builder/has_many.rb +8 -4
- data/lib/active_record/associations/builder/has_one.rb +46 -5
- data/lib/active_record/associations/builder/singular_association.rb +16 -10
- data/lib/active_record/associations/collection_association.rb +136 -288
- data/lib/active_record/associations/collection_proxy.rb +241 -147
- data/lib/active_record/associations/foreign_association.rb +10 -1
- data/lib/active_record/associations/has_many_association.rb +34 -98
- data/lib/active_record/associations/has_many_through_association.rb +60 -87
- data/lib/active_record/associations/has_one_association.rb +61 -49
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +38 -86
- 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 +149 -166
- data/lib/active_record/associations/preloader/association.rb +90 -123
- data/lib/active_record/associations/preloader/through_association.rb +85 -65
- data/lib/active_record/associations/preloader.rb +90 -93
- data/lib/active_record/associations/singular_association.rb +18 -39
- data/lib/active_record/associations/through_association.rb +38 -18
- data/lib/active_record/associations.rb +1737 -1597
- data/lib/active_record/attribute_assignment.rb +57 -185
- data/lib/active_record/attribute_decorators.rb +39 -17
- data/lib/active_record/attribute_methods/before_type_cast.rb +13 -9
- data/lib/active_record/attribute_methods/dirty.rb +174 -144
- data/lib/active_record/attribute_methods/primary_key.rb +90 -84
- data/lib/active_record/attribute_methods/query.rb +6 -5
- data/lib/active_record/attribute_methods/read.rb +20 -77
- data/lib/active_record/attribute_methods/serialization.rb +40 -21
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +57 -37
- data/lib/active_record/attribute_methods/write.rb +32 -55
- data/lib/active_record/attribute_methods.rb +120 -135
- data/lib/active_record/attributes.rb +213 -82
- data/lib/active_record/autosave_association.rb +97 -41
- data/lib/active_record/base.rb +57 -45
- data/lib/active_record/callbacks.rb +101 -76
- data/lib/active_record/coders/json.rb +3 -1
- data/lib/active_record/coders/yaml_column.rb +23 -12
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +804 -297
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +26 -8
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +240 -115
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +83 -24
- data/lib/active_record/connection_adapters/abstract/quoting.rb +170 -53
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +5 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +74 -47
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +371 -242
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +79 -36
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +694 -256
- data/lib/active_record/connection_adapters/abstract/transaction.rb +190 -83
- data/lib/active_record/connection_adapters/abstract_adapter.rb +473 -202
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +507 -639
- data/lib/active_record/connection_adapters/column.rb +56 -43
- data/lib/active_record/connection_adapters/connection_specification.rb +174 -153
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +29 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +196 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +71 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +71 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +95 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +88 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +58 -181
- data/lib/active_record/connection_adapters/postgresql/column.rb +21 -11
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +70 -114
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +49 -58
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +9 -8
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +5 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +13 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +9 -22
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +5 -4
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +31 -20
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/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/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 +51 -34
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -5
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +58 -54
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +9 -5
- 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 +23 -25
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +144 -47
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +27 -14
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -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 +462 -296
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +11 -8
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +558 -356
- data/lib/active_record/connection_adapters/schema_cache.rb +72 -25
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +119 -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 +17 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +137 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +299 -349
- data/lib/active_record/connection_adapters/statement_pool.rb +33 -13
- data/lib/active_record/connection_handling.rb +167 -41
- data/lib/active_record/core.rb +252 -230
- data/lib/active_record/counter_cache.rb +70 -49
- data/lib/active_record/database_configurations/database_config.rb +37 -0
- data/lib/active_record/database_configurations/hash_config.rb +50 -0
- data/lib/active_record/database_configurations/url_config.rb +78 -0
- data/lib/active_record/database_configurations.rb +233 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +87 -106
- data/lib/active_record/enum.rb +163 -86
- data/lib/active_record/errors.rb +188 -53
- data/lib/active_record/explain.rb +22 -11
- data/lib/active_record/explain_registry.rb +4 -2
- data/lib/active_record/explain_subscriber.rb +10 -5
- data/lib/active_record/fixture_set/file.rb +35 -9
- data/lib/active_record/fixture_set/model_metadata.rb +33 -0
- data/lib/active_record/fixture_set/render_context.rb +17 -0
- data/lib/active_record/fixture_set/table_row.rb +152 -0
- data/lib/active_record/fixture_set/table_rows.rb +46 -0
- data/lib/active_record/fixtures.rb +227 -501
- data/lib/active_record/gem_version.rb +6 -4
- data/lib/active_record/inheritance.rb +158 -115
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +123 -29
- data/lib/active_record/internal_metadata.rb +53 -0
- data/lib/active_record/legacy_yaml_adapter.rb +21 -3
- data/lib/active_record/locale/en.yml +3 -2
- data/lib/active_record/locking/optimistic.rb +86 -96
- data/lib/active_record/locking/pessimistic.rb +18 -6
- data/lib/active_record/log_subscriber.rb +76 -33
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +87 -0
- data/lib/active_record/middleware/database_selector.rb +74 -0
- data/lib/active_record/migration/command_recorder.rb +166 -91
- data/lib/active_record/migration/compatibility.rb +244 -0
- data/lib/active_record/migration/join_table.rb +8 -7
- data/lib/active_record/migration.rb +623 -305
- data/lib/active_record/model_schema.rb +313 -112
- data/lib/active_record/nested_attributes.rb +263 -223
- data/lib/active_record/no_touching.rb +15 -2
- data/lib/active_record/null_relation.rb +24 -38
- data/lib/active_record/persistence.rb +557 -126
- data/lib/active_record/query_cache.rb +19 -23
- data/lib/active_record/querying.rb +44 -30
- data/lib/active_record/railtie.rb +143 -44
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- 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 +331 -185
- data/lib/active_record/readonly_attributes.rb +5 -4
- data/lib/active_record/reflection.rb +430 -281
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/batches.rb +206 -55
- data/lib/active_record/relation/calculations.rb +268 -254
- data/lib/active_record/relation/delegation.rb +75 -84
- data/lib/active_record/relation/finder_methods.rb +285 -241
- data/lib/active_record/relation/from_clause.rb +30 -0
- data/lib/active_record/relation/merger.rb +78 -88
- data/lib/active_record/relation/predicate_builder/array_handler.rb +27 -26
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +18 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +7 -1
- data/lib/active_record/relation/predicate_builder.rb +110 -119
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +603 -397
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +11 -14
- data/lib/active_record/relation/where_clause.rb +189 -0
- data/lib/active_record/relation/where_clause_factory.rb +33 -0
- data/lib/active_record/relation.rb +530 -341
- data/lib/active_record/result.rb +79 -43
- data/lib/active_record/runtime_registry.rb +6 -4
- data/lib/active_record/sanitization.rb +144 -121
- data/lib/active_record/schema.rb +21 -24
- data/lib/active_record/schema_dumper.rb +112 -93
- data/lib/active_record/schema_migration.rb +24 -17
- data/lib/active_record/scoping/default.rb +98 -83
- data/lib/active_record/scoping/named.rb +86 -33
- data/lib/active_record/scoping.rb +45 -27
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +5 -5
- data/lib/active_record/statement_cache.rb +73 -36
- data/lib/active_record/store.rb +127 -42
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +90 -0
- data/lib/active_record/tasks/database_tasks.rb +307 -100
- data/lib/active_record/tasks/mysql_database_tasks.rb +55 -100
- data/lib/active_record/tasks/postgresql_database_tasks.rb +80 -41
- data/lib/active_record/tasks/sqlite_database_tasks.rb +37 -16
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +225 -0
- data/lib/active_record/timestamp.rb +86 -41
- data/lib/active_record/touch_later.rb +65 -0
- data/lib/active_record/transactions.rb +223 -157
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type/adapter_specific_registry.rb +126 -0
- data/lib/active_record/type/date.rb +4 -45
- data/lib/active_record/type/date_time.rb +4 -49
- data/lib/active_record/type/decimal_without_scale.rb +6 -2
- data/lib/active_record/type/hash_lookup_type_map.rb +5 -4
- data/lib/active_record/type/internal/timezone.rb +17 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +23 -15
- data/lib/active_record/type/text.rb +2 -2
- data/lib/active_record/type/time.rb +11 -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 +77 -23
- data/lib/active_record/type_caster/connection.rb +34 -0
- data/lib/active_record/type_caster/map.rb +20 -0
- data/lib/active_record/type_caster.rb +9 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +12 -4
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +14 -13
- data/lib/active_record/validations/uniqueness.rb +42 -55
- data/lib/active_record/validations.rb +38 -35
- data/lib/active_record/version.rb +3 -1
- data/lib/active_record.rb +42 -22
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes/attribute.rb +37 -0
- data/lib/arel/attributes.rb +22 -0
- data/lib/arel/collectors/bind.rb +24 -0
- data/lib/arel/collectors/composite.rb +31 -0
- data/lib/arel/collectors/plain_string.rb +20 -0
- data/lib/arel/collectors/sql_string.rb +20 -0
- data/lib/arel/collectors/substitute_binds.rb +28 -0
- data/lib/arel/crud.rb +42 -0
- data/lib/arel/delete_manager.rb +18 -0
- data/lib/arel/errors.rb +9 -0
- data/lib/arel/expressions.rb +29 -0
- data/lib/arel/factory_methods.rb +49 -0
- data/lib/arel/insert_manager.rb +49 -0
- data/lib/arel/math.rb +45 -0
- data/lib/arel/nodes/and.rb +32 -0
- data/lib/arel/nodes/ascending.rb +23 -0
- data/lib/arel/nodes/binary.rb +52 -0
- data/lib/arel/nodes/bind_param.rb +36 -0
- data/lib/arel/nodes/case.rb +55 -0
- data/lib/arel/nodes/casted.rb +50 -0
- data/lib/arel/nodes/comment.rb +29 -0
- data/lib/arel/nodes/count.rb +12 -0
- data/lib/arel/nodes/delete_statement.rb +45 -0
- data/lib/arel/nodes/descending.rb +23 -0
- data/lib/arel/nodes/equality.rb +18 -0
- data/lib/arel/nodes/extract.rb +24 -0
- data/lib/arel/nodes/false.rb +16 -0
- data/lib/arel/nodes/full_outer_join.rb +8 -0
- data/lib/arel/nodes/function.rb +44 -0
- data/lib/arel/nodes/grouping.rb +8 -0
- data/lib/arel/nodes/in.rb +8 -0
- data/lib/arel/nodes/infix_operation.rb +80 -0
- data/lib/arel/nodes/inner_join.rb +8 -0
- data/lib/arel/nodes/insert_statement.rb +37 -0
- data/lib/arel/nodes/join_source.rb +20 -0
- data/lib/arel/nodes/matches.rb +18 -0
- data/lib/arel/nodes/named_function.rb +23 -0
- data/lib/arel/nodes/node.rb +50 -0
- data/lib/arel/nodes/node_expression.rb +13 -0
- data/lib/arel/nodes/outer_join.rb +8 -0
- data/lib/arel/nodes/over.rb +15 -0
- data/lib/arel/nodes/regexp.rb +16 -0
- data/lib/arel/nodes/right_outer_join.rb +8 -0
- data/lib/arel/nodes/select_core.rb +67 -0
- data/lib/arel/nodes/select_statement.rb +41 -0
- data/lib/arel/nodes/sql_literal.rb +16 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/table_alias.rb +27 -0
- data/lib/arel/nodes/terminal.rb +16 -0
- data/lib/arel/nodes/true.rb +16 -0
- data/lib/arel/nodes/unary.rb +45 -0
- data/lib/arel/nodes/unary_operation.rb +20 -0
- data/lib/arel/nodes/unqualified_column.rb +22 -0
- data/lib/arel/nodes/update_statement.rb +41 -0
- data/lib/arel/nodes/values_list.rb +9 -0
- data/lib/arel/nodes/window.rb +126 -0
- data/lib/arel/nodes/with.rb +11 -0
- data/lib/arel/nodes.rb +68 -0
- data/lib/arel/order_predications.rb +13 -0
- data/lib/arel/predications.rb +256 -0
- data/lib/arel/select_manager.rb +271 -0
- data/lib/arel/table.rb +110 -0
- data/lib/arel/tree_manager.rb +72 -0
- data/lib/arel/update_manager.rb +34 -0
- data/lib/arel/visitors/depth_first.rb +203 -0
- data/lib/arel/visitors/dot.rb +296 -0
- data/lib/arel/visitors/ibm_db.rb +34 -0
- data/lib/arel/visitors/informix.rb +62 -0
- data/lib/arel/visitors/mssql.rb +156 -0
- data/lib/arel/visitors/mysql.rb +83 -0
- data/lib/arel/visitors/oracle.rb +158 -0
- data/lib/arel/visitors/oracle12.rb +65 -0
- data/lib/arel/visitors/postgresql.rb +109 -0
- data/lib/arel/visitors/sqlite.rb +38 -0
- data/lib/arel/visitors/to_sql.rb +888 -0
- data/lib/arel/visitors/visitor.rb +45 -0
- data/lib/arel/visitors/where_sql.rb +22 -0
- data/lib/arel/visitors.rb +20 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +62 -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 +42 -37
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +11 -2
- data/lib/rails/generators/active_record/migration.rb +30 -1
- data/lib/rails/generators/active_record/model/model_generator.rb +18 -22
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- data/lib/rails/generators/active_record.rb +7 -5
- metadata +168 -59
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -24
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- data/lib/active_record/associations/preloader/has_one.rb +0 -23
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -21
- data/lib/active_record/attribute.rb +0 -163
- data/lib/active_record/attribute_set/builder.rb +0 -106
- data/lib/active_record/attribute_set.rb +0 -81
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -498
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
- data/lib/active_record/connection_adapters/postgresql/oid/float.rb +0 -21
- data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +0 -11
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -35
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -193
- data/lib/active_record/type/big_integer.rb +0 -13
- data/lib/active_record/type/binary.rb +0 -50
- data/lib/active_record/type/boolean.rb +0 -31
- data/lib/active_record/type/decimal.rb +0 -64
- data/lib/active_record/type/decorator.rb +0 -14
- data/lib/active_record/type/float.rb +0 -19
- data/lib/active_record/type/integer.rb +0 -59
- data/lib/active_record/type/mutable.rb +0 -16
- data/lib/active_record/type/numeric.rb +0 -36
- data/lib/active_record/type/string.rb +0 -40
- data/lib/active_record/type/time_value.rb +0 -38
- data/lib/active_record/type/value.rb +0 -110
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +0 -19
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -10
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/string/filters"
|
4
|
+
require "concurrent/map"
|
3
5
|
|
4
6
|
module ActiveRecord
|
5
7
|
# = Active Record Reflection
|
@@ -7,42 +9,46 @@ module ActiveRecord
|
|
7
9
|
extend ActiveSupport::Concern
|
8
10
|
|
9
11
|
included do
|
10
|
-
class_attribute :_reflections, instance_writer: false
|
11
|
-
class_attribute :aggregate_reflections, instance_writer: false
|
12
|
-
self._reflections = {}
|
13
|
-
self.aggregate_reflections = {}
|
12
|
+
class_attribute :_reflections, instance_writer: false, default: {}
|
13
|
+
class_attribute :aggregate_reflections, instance_writer: false, default: {}
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
HasManyReflection
|
22
|
-
when :has_one
|
23
|
-
HasOneReflection
|
24
|
-
when :belongs_to
|
25
|
-
BelongsToReflection
|
26
|
-
else
|
27
|
-
raise "Unsupported Macro: #{macro}"
|
28
|
-
end
|
29
|
-
|
30
|
-
reflection = klass.new(name, scope, options, ar)
|
31
|
-
options[:through] ? ThroughReflection.new(reflection) : reflection
|
32
|
-
end
|
16
|
+
class << self
|
17
|
+
def create(macro, name, scope, options, ar)
|
18
|
+
reflection = reflection_class_for(macro).new(name, scope, options, ar)
|
19
|
+
options[:through] ? ThroughReflection.new(reflection) : reflection
|
20
|
+
end
|
33
21
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
22
|
+
def add_reflection(ar, name, reflection)
|
23
|
+
ar.clear_reflections_cache
|
24
|
+
name = -name.to_s
|
25
|
+
ar._reflections = ar._reflections.except(name).merge!(name => reflection)
|
26
|
+
end
|
27
|
+
|
28
|
+
def add_aggregate_reflection(ar, name, reflection)
|
29
|
+
ar.aggregate_reflections = ar.aggregate_reflections.merge(-name.to_s => reflection)
|
30
|
+
end
|
38
31
|
|
39
|
-
|
40
|
-
|
32
|
+
private
|
33
|
+
def reflection_class_for(macro)
|
34
|
+
case macro
|
35
|
+
when :composed_of
|
36
|
+
AggregateReflection
|
37
|
+
when :has_many
|
38
|
+
HasManyReflection
|
39
|
+
when :has_one
|
40
|
+
HasOneReflection
|
41
|
+
when :belongs_to
|
42
|
+
BelongsToReflection
|
43
|
+
else
|
44
|
+
raise "Unsupported Macro: #{macro}"
|
45
|
+
end
|
46
|
+
end
|
41
47
|
end
|
42
48
|
|
43
|
-
# \Reflection enables
|
44
|
-
#
|
45
|
-
#
|
49
|
+
# \Reflection enables the ability to examine the associations and aggregations of
|
50
|
+
# Active Record classes and objects. This information, for example,
|
51
|
+
# can be used in a form builder that takes an Active Record object
|
46
52
|
# and creates input fields for all of the attributes depending on their type
|
47
53
|
# and displays the associations to other objects.
|
48
54
|
#
|
@@ -62,20 +68,20 @@ module ActiveRecord
|
|
62
68
|
aggregate_reflections[aggregation.to_s]
|
63
69
|
end
|
64
70
|
|
65
|
-
# Returns a Hash of name of the reflection as the key and
|
71
|
+
# Returns a Hash of name of the reflection as the key and an AssociationReflection as the value.
|
66
72
|
#
|
67
73
|
# Account.reflections # => {"balance" => AggregateReflection}
|
68
74
|
#
|
69
|
-
# @api public
|
70
75
|
def reflections
|
71
76
|
@__reflections ||= begin
|
72
77
|
ref = {}
|
73
78
|
|
74
79
|
_reflections.each do |name, reflection|
|
75
|
-
|
80
|
+
parent_reflection = reflection.parent_reflection
|
76
81
|
|
77
|
-
if
|
78
|
-
|
82
|
+
if parent_reflection
|
83
|
+
parent_name = parent_reflection.name
|
84
|
+
ref[parent_name.to_s] = parent_reflection
|
79
85
|
else
|
80
86
|
ref[name] = reflection
|
81
87
|
end
|
@@ -95,10 +101,10 @@ module ActiveRecord
|
|
95
101
|
# Account.reflect_on_all_associations # returns an array of all associations
|
96
102
|
# Account.reflect_on_all_associations(:has_many) # returns an array of all has_many associations
|
97
103
|
#
|
98
|
-
# @api public
|
99
104
|
def reflect_on_all_associations(macro = nil)
|
100
105
|
association_reflections = reflections.values
|
101
|
-
|
106
|
+
association_reflections.select! { |reflection| reflection.macro == macro } if macro
|
107
|
+
association_reflections
|
102
108
|
end
|
103
109
|
|
104
110
|
# Returns the AssociationReflection object for the +association+ (use the symbol).
|
@@ -106,31 +112,42 @@ module ActiveRecord
|
|
106
112
|
# Account.reflect_on_association(:owner) # returns the owner AssociationReflection
|
107
113
|
# Invoice.reflect_on_association(:line_items).macro # returns :has_many
|
108
114
|
#
|
109
|
-
# @api public
|
110
115
|
def reflect_on_association(association)
|
111
116
|
reflections[association.to_s]
|
112
117
|
end
|
113
118
|
|
114
|
-
# @api private
|
115
119
|
def _reflect_on_association(association) #:nodoc:
|
116
120
|
_reflections[association.to_s]
|
117
121
|
end
|
118
122
|
|
119
123
|
# Returns an array of AssociationReflection objects for all associations which have <tt>:autosave</tt> enabled.
|
120
|
-
#
|
121
|
-
# @api public
|
122
124
|
def reflect_on_all_autosave_associations
|
123
125
|
reflections.values.select { |reflection| reflection.options[:autosave] }
|
124
126
|
end
|
125
127
|
|
126
|
-
def clear_reflections_cache
|
128
|
+
def clear_reflections_cache # :nodoc:
|
127
129
|
@__reflections = nil
|
128
130
|
end
|
129
131
|
end
|
130
132
|
|
131
|
-
# Holds all the methods that are shared between MacroReflection
|
132
|
-
#
|
133
|
+
# Holds all the methods that are shared between MacroReflection and ThroughReflection.
|
134
|
+
#
|
135
|
+
# AbstractReflection
|
136
|
+
# MacroReflection
|
137
|
+
# AggregateReflection
|
138
|
+
# AssociationReflection
|
139
|
+
# HasManyReflection
|
140
|
+
# HasOneReflection
|
141
|
+
# BelongsToReflection
|
142
|
+
# HasAndBelongsToManyReflection
|
143
|
+
# ThroughReflection
|
144
|
+
# PolymorphicReflection
|
145
|
+
# RuntimeReflection
|
133
146
|
class AbstractReflection # :nodoc:
|
147
|
+
def through_reflection?
|
148
|
+
false
|
149
|
+
end
|
150
|
+
|
134
151
|
def table_name
|
135
152
|
klass.table_name
|
136
153
|
end
|
@@ -141,14 +158,6 @@ module ActiveRecord
|
|
141
158
|
klass.new(attributes, &block)
|
142
159
|
end
|
143
160
|
|
144
|
-
def quoted_table_name
|
145
|
-
klass.quoted_table_name
|
146
|
-
end
|
147
|
-
|
148
|
-
def primary_key_type
|
149
|
-
klass.type_for_attribute(klass.primary_key)
|
150
|
-
end
|
151
|
-
|
152
161
|
# Returns the class name for the macro.
|
153
162
|
#
|
154
163
|
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>'Money'</tt>
|
@@ -159,17 +168,66 @@ module ActiveRecord
|
|
159
168
|
|
160
169
|
JoinKeys = Struct.new(:key, :foreign_key) # :nodoc:
|
161
170
|
|
162
|
-
def join_keys
|
163
|
-
|
171
|
+
def join_keys
|
172
|
+
@join_keys ||= get_join_keys(klass)
|
173
|
+
end
|
174
|
+
|
175
|
+
# Returns a list of scopes that should be applied for this Reflection
|
176
|
+
# object when querying the database.
|
177
|
+
def scopes
|
178
|
+
scope ? [scope] : []
|
179
|
+
end
|
180
|
+
|
181
|
+
def join_scope(table, foreign_table, foreign_klass)
|
182
|
+
predicate_builder = predicate_builder(table)
|
183
|
+
scope_chain_items = join_scopes(table, predicate_builder)
|
184
|
+
klass_scope = klass_join_scope(table, predicate_builder)
|
185
|
+
|
186
|
+
if type
|
187
|
+
klass_scope.where!(type => foreign_klass.polymorphic_name)
|
188
|
+
end
|
189
|
+
|
190
|
+
scope_chain_items.inject(klass_scope, &:merge!)
|
191
|
+
|
192
|
+
key = join_keys.key
|
193
|
+
foreign_key = join_keys.foreign_key
|
194
|
+
|
195
|
+
klass_scope.where!(table[key].eq(foreign_table[foreign_key]))
|
196
|
+
|
197
|
+
if klass.finder_needs_type_condition?
|
198
|
+
klass_scope.where!(klass.send(:type_condition, table))
|
199
|
+
end
|
200
|
+
|
201
|
+
klass_scope
|
202
|
+
end
|
203
|
+
|
204
|
+
def join_scopes(table, predicate_builder) # :nodoc:
|
205
|
+
if scope
|
206
|
+
[scope_for(build_scope(table, predicate_builder))]
|
207
|
+
else
|
208
|
+
[]
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
def klass_join_scope(table, predicate_builder) # :nodoc:
|
213
|
+
relation = build_scope(table, predicate_builder)
|
214
|
+
klass.scope_for_association(relation)
|
164
215
|
end
|
165
216
|
|
166
|
-
def
|
167
|
-
|
168
|
-
|
169
|
-
without replacement.
|
170
|
-
MSG
|
217
|
+
def constraints
|
218
|
+
chain.flat_map(&:scopes)
|
219
|
+
end
|
171
220
|
|
172
|
-
|
221
|
+
def counter_cache_column
|
222
|
+
if belongs_to?
|
223
|
+
if options[:counter_cache] == true
|
224
|
+
"#{active_record.name.demodulize.underscore.pluralize}_count"
|
225
|
+
elsif options[:counter_cache]
|
226
|
+
options[:counter_cache].to_s
|
227
|
+
end
|
228
|
+
else
|
229
|
+
options[:counter_cache] ? options[:counter_cache].to_s : "#{name}_count"
|
230
|
+
end
|
173
231
|
end
|
174
232
|
|
175
233
|
def inverse_of
|
@@ -185,17 +243,88 @@ module ActiveRecord
|
|
185
243
|
end
|
186
244
|
end
|
187
245
|
end
|
246
|
+
|
247
|
+
# This shit is nasty. We need to avoid the following situation:
|
248
|
+
#
|
249
|
+
# * An associated record is deleted via record.destroy
|
250
|
+
# * Hence the callbacks run, and they find a belongs_to on the record with a
|
251
|
+
# :counter_cache options which points back at our owner. So they update the
|
252
|
+
# counter cache.
|
253
|
+
# * In which case, we must make sure to *not* update the counter cache, or else
|
254
|
+
# it will be decremented twice.
|
255
|
+
#
|
256
|
+
# Hence this method.
|
257
|
+
def inverse_which_updates_counter_cache
|
258
|
+
return @inverse_which_updates_counter_cache if defined?(@inverse_which_updates_counter_cache)
|
259
|
+
@inverse_which_updates_counter_cache = klass.reflect_on_all_associations(:belongs_to).find do |inverse|
|
260
|
+
inverse.counter_cache_column == counter_cache_column
|
261
|
+
end
|
262
|
+
end
|
263
|
+
alias inverse_updates_counter_cache? inverse_which_updates_counter_cache
|
264
|
+
|
265
|
+
def inverse_updates_counter_in_memory?
|
266
|
+
inverse_of && inverse_which_updates_counter_cache == inverse_of
|
267
|
+
end
|
268
|
+
|
269
|
+
# Returns whether a counter cache should be used for this association.
|
270
|
+
#
|
271
|
+
# The counter_cache option must be given on either the owner or inverse
|
272
|
+
# association, and the column must be present on the owner.
|
273
|
+
def has_cached_counter?
|
274
|
+
options[:counter_cache] ||
|
275
|
+
inverse_which_updates_counter_cache && inverse_which_updates_counter_cache.options[:counter_cache] &&
|
276
|
+
!!active_record.columns_hash[counter_cache_column]
|
277
|
+
end
|
278
|
+
|
279
|
+
def counter_must_be_updated_by_has_many?
|
280
|
+
!inverse_updates_counter_in_memory? && has_cached_counter?
|
281
|
+
end
|
282
|
+
|
283
|
+
def alias_candidate(name)
|
284
|
+
"#{plural_name}_#{name}"
|
285
|
+
end
|
286
|
+
|
287
|
+
def chain
|
288
|
+
collect_join_chain
|
289
|
+
end
|
290
|
+
|
291
|
+
def get_join_keys(association_klass)
|
292
|
+
JoinKeys.new(join_primary_key(association_klass), join_foreign_key)
|
293
|
+
end
|
294
|
+
|
295
|
+
def build_scope(table, predicate_builder = predicate_builder(table))
|
296
|
+
Relation.create(
|
297
|
+
klass,
|
298
|
+
table: table,
|
299
|
+
predicate_builder: predicate_builder
|
300
|
+
)
|
301
|
+
end
|
302
|
+
|
303
|
+
def join_primary_key(*)
|
304
|
+
foreign_key
|
305
|
+
end
|
306
|
+
|
307
|
+
def join_foreign_key
|
308
|
+
active_record_primary_key
|
309
|
+
end
|
310
|
+
|
311
|
+
protected
|
312
|
+
def actual_source_reflection # FIXME: this is a horrible name
|
313
|
+
self
|
314
|
+
end
|
315
|
+
|
316
|
+
private
|
317
|
+
def predicate_builder(table)
|
318
|
+
PredicateBuilder.new(TableMetadata.new(klass, table))
|
319
|
+
end
|
320
|
+
|
321
|
+
def primary_key(klass)
|
322
|
+
klass.primary_key || raise(UnknownPrimaryKey.new(klass))
|
323
|
+
end
|
188
324
|
end
|
325
|
+
|
189
326
|
# Base class for AggregateReflection and AssociationReflection. Objects of
|
190
327
|
# AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods.
|
191
|
-
#
|
192
|
-
# MacroReflection
|
193
|
-
# AssociationReflection
|
194
|
-
# AggregateReflection
|
195
|
-
# HasManyReflection
|
196
|
-
# HasOneReflection
|
197
|
-
# BelongsToReflection
|
198
|
-
# ThroughReflection
|
199
328
|
class MacroReflection < AbstractReflection
|
200
329
|
# Returns the name of the macro.
|
201
330
|
#
|
@@ -226,9 +355,8 @@ module ActiveRecord
|
|
226
355
|
end
|
227
356
|
|
228
357
|
def autosave=(autosave)
|
229
|
-
@automatic_inverse_of = false
|
230
358
|
@options[:autosave] = autosave
|
231
|
-
|
359
|
+
parent_reflection = self.parent_reflection
|
232
360
|
if parent_reflection
|
233
361
|
parent_reflection.autosave = autosave
|
234
362
|
end
|
@@ -238,6 +366,17 @@ module ActiveRecord
|
|
238
366
|
#
|
239
367
|
# <tt>composed_of :balance, class_name: 'Money'</tt> returns the Money class
|
240
368
|
# <tt>has_many :clients</tt> returns the Client class
|
369
|
+
#
|
370
|
+
# class Company < ActiveRecord::Base
|
371
|
+
# has_many :clients
|
372
|
+
# end
|
373
|
+
#
|
374
|
+
# Company.reflect_on_association(:clients).klass
|
375
|
+
# # => Client
|
376
|
+
#
|
377
|
+
# <b>Note:</b> Do not call +klass.new+ or +klass.create+ to instantiate
|
378
|
+
# a new association object. Use +build_association+ or +create_association+
|
379
|
+
# instead. This allows plugins to hook into association object creation.
|
241
380
|
def klass
|
242
381
|
@klass ||= compute_class(class_name)
|
243
382
|
end
|
@@ -256,14 +395,17 @@ module ActiveRecord
|
|
256
395
|
active_record == other_aggregation.active_record
|
257
396
|
end
|
258
397
|
|
398
|
+
def scope_for(relation, owner = nil)
|
399
|
+
relation.instance_exec(owner, &scope) || relation
|
400
|
+
end
|
401
|
+
|
259
402
|
private
|
260
403
|
def derive_class_name
|
261
404
|
name.to_s.camelize
|
262
405
|
end
|
263
406
|
end
|
264
407
|
|
265
|
-
|
266
|
-
# Holds all the meta-data about an aggregation as it was specified in the
|
408
|
+
# Holds all the metadata about an aggregation as it was specified in the
|
267
409
|
# Active Record class.
|
268
410
|
class AggregateReflection < MacroReflection #:nodoc:
|
269
411
|
def mapping
|
@@ -272,50 +414,37 @@ module ActiveRecord
|
|
272
414
|
end
|
273
415
|
end
|
274
416
|
|
275
|
-
# Holds all the
|
417
|
+
# Holds all the metadata about an association as it was specified in the
|
276
418
|
# Active Record class.
|
277
419
|
class AssociationReflection < MacroReflection #:nodoc:
|
278
|
-
# Returns the target association's class.
|
279
|
-
#
|
280
|
-
# class Author < ActiveRecord::Base
|
281
|
-
# has_many :books
|
282
|
-
# end
|
283
|
-
#
|
284
|
-
# Author.reflect_on_association(:books).klass
|
285
|
-
# # => Book
|
286
|
-
#
|
287
|
-
# <b>Note:</b> Do not call +klass.new+ or +klass.create+ to instantiate
|
288
|
-
# a new association object. Use +build_association+ or +create_association+
|
289
|
-
# instead. This allows plugins to hook into association object creation.
|
290
|
-
def klass
|
291
|
-
@klass ||= compute_class(class_name)
|
292
|
-
end
|
293
|
-
|
294
420
|
def compute_class(name)
|
421
|
+
if polymorphic?
|
422
|
+
raise ArgumentError, "Polymorphic associations do not support computing the class."
|
423
|
+
end
|
295
424
|
active_record.send(:compute_type, name)
|
296
425
|
end
|
297
426
|
|
298
427
|
attr_reader :type, :foreign_type
|
299
|
-
attr_accessor :parent_reflection #
|
428
|
+
attr_accessor :parent_reflection # Reflection
|
300
429
|
|
301
430
|
def initialize(name, scope, options, active_record)
|
302
431
|
super
|
303
|
-
@automatic_inverse_of = nil
|
304
432
|
@type = options[:as] && (options[:foreign_type] || "#{options[:as]}_type")
|
305
|
-
@foreign_type = options[:foreign_type] || "#{name}_type"
|
433
|
+
@foreign_type = options[:polymorphic] && (options[:foreign_type] || "#{name}_type")
|
306
434
|
@constructable = calculate_constructable(macro, options)
|
307
|
-
@association_scope_cache =
|
308
|
-
|
435
|
+
@association_scope_cache = Concurrent::Map.new
|
436
|
+
|
437
|
+
if options[:class_name] && options[:class_name].class == Class
|
438
|
+
raise ArgumentError, "A class was passed to `:class_name` but we are expecting a string."
|
439
|
+
end
|
309
440
|
end
|
310
441
|
|
311
|
-
def association_scope_cache(conn, owner)
|
442
|
+
def association_scope_cache(conn, owner, &block)
|
312
443
|
key = conn.prepared_statements
|
313
444
|
if polymorphic?
|
314
445
|
key = [key, owner._read_attribute(@foreign_type)]
|
315
446
|
end
|
316
|
-
@association_scope_cache
|
317
|
-
@association_scope_cache[key] ||= yield
|
318
|
-
}
|
447
|
+
@association_scope_cache.compute_if_absent(key) { StatementCache.create(conn, &block) }
|
319
448
|
end
|
320
449
|
|
321
450
|
def constructable? # :nodoc:
|
@@ -343,14 +472,6 @@ module ActiveRecord
|
|
343
472
|
@active_record_primary_key ||= options[:primary_key] || primary_key(active_record)
|
344
473
|
end
|
345
474
|
|
346
|
-
def counter_cache_column
|
347
|
-
if options[:counter_cache] == true
|
348
|
-
"#{active_record.name.demodulize.underscore.pluralize}_count"
|
349
|
-
elsif options[:counter_cache]
|
350
|
-
options[:counter_cache].to_s
|
351
|
-
end
|
352
|
-
end
|
353
|
-
|
354
475
|
def check_validity!
|
355
476
|
check_validity_of_inverse!
|
356
477
|
end
|
@@ -358,21 +479,18 @@ module ActiveRecord
|
|
358
479
|
def check_preloadable!
|
359
480
|
return unless scope
|
360
481
|
|
361
|
-
|
362
|
-
|
482
|
+
unless scope.arity == 0
|
483
|
+
raise ArgumentError, <<-MSG.squish
|
363
484
|
The association scope '#{name}' is instance dependent (the scope
|
364
|
-
block takes an argument). Preloading
|
365
|
-
|
366
|
-
passed to the association scope. This will most likely result in
|
367
|
-
broken or incorrect behavior. Joining, Preloading and eager loading
|
368
|
-
of these associations is deprecated and will be removed in the future.
|
485
|
+
block takes an argument). Preloading instance dependent scopes is
|
486
|
+
not supported.
|
369
487
|
MSG
|
370
488
|
end
|
371
489
|
end
|
372
490
|
alias :check_eager_loadable! :check_preloadable!
|
373
491
|
|
374
492
|
def join_id_for(owner) # :nodoc:
|
375
|
-
owner[
|
493
|
+
owner[join_foreign_key]
|
376
494
|
end
|
377
495
|
|
378
496
|
def through_reflection
|
@@ -385,18 +503,22 @@ module ActiveRecord
|
|
385
503
|
|
386
504
|
# A chain of reflections from this one back to the owner. For more see the explanation in
|
387
505
|
# ThroughReflection.
|
388
|
-
def
|
506
|
+
def collect_join_chain
|
389
507
|
[self]
|
390
508
|
end
|
391
509
|
|
510
|
+
# This is for clearing cache on the reflection. Useful for tests that need to compare
|
511
|
+
# SQL queries on associations.
|
512
|
+
def clear_association_scope_cache # :nodoc:
|
513
|
+
@association_scope_cache.clear
|
514
|
+
end
|
515
|
+
|
392
516
|
def nested?
|
393
517
|
false
|
394
518
|
end
|
395
519
|
|
396
|
-
|
397
|
-
|
398
|
-
def scope_chain
|
399
|
-
scope ? [[scope]] : [[]]
|
520
|
+
def has_scope?
|
521
|
+
scope
|
400
522
|
end
|
401
523
|
|
402
524
|
def has_inverse?
|
@@ -444,69 +566,48 @@ module ActiveRecord
|
|
444
566
|
# Returns +true+ if +self+ is a +has_one+ reflection.
|
445
567
|
def has_one?; false; end
|
446
568
|
|
447
|
-
def association_class
|
448
|
-
case macro
|
449
|
-
when :belongs_to
|
450
|
-
if polymorphic?
|
451
|
-
Associations::BelongsToPolymorphicAssociation
|
452
|
-
else
|
453
|
-
Associations::BelongsToAssociation
|
454
|
-
end
|
455
|
-
when :has_many
|
456
|
-
if options[:through]
|
457
|
-
Associations::HasManyThroughAssociation
|
458
|
-
else
|
459
|
-
Associations::HasManyAssociation
|
460
|
-
end
|
461
|
-
when :has_one
|
462
|
-
if options[:through]
|
463
|
-
Associations::HasOneThroughAssociation
|
464
|
-
else
|
465
|
-
Associations::HasOneAssociation
|
466
|
-
end
|
467
|
-
end
|
468
|
-
end
|
569
|
+
def association_class; raise NotImplementedError; end
|
469
570
|
|
470
571
|
def polymorphic?
|
471
572
|
options[:polymorphic]
|
472
573
|
end
|
473
574
|
|
474
575
|
VALID_AUTOMATIC_INVERSE_MACROS = [:has_many, :has_one, :belongs_to]
|
475
|
-
INVALID_AUTOMATIC_INVERSE_OPTIONS = [:
|
576
|
+
INVALID_AUTOMATIC_INVERSE_OPTIONS = [:through, :foreign_key]
|
476
577
|
|
477
|
-
|
578
|
+
def add_as_source(seed)
|
579
|
+
seed
|
580
|
+
end
|
478
581
|
|
479
|
-
|
480
|
-
|
481
|
-
|
582
|
+
def add_as_polymorphic_through(reflection, seed)
|
583
|
+
seed + [PolymorphicReflection.new(self, reflection)]
|
584
|
+
end
|
482
585
|
|
483
|
-
|
586
|
+
def add_as_through(seed)
|
587
|
+
seed + [self]
|
588
|
+
end
|
589
|
+
|
590
|
+
def extensions
|
591
|
+
Array(options[:extend])
|
592
|
+
end
|
484
593
|
|
594
|
+
private
|
485
595
|
def calculate_constructable(macro, options)
|
486
|
-
|
487
|
-
when :belongs_to
|
488
|
-
!polymorphic?
|
489
|
-
when :has_one
|
490
|
-
!options[:through]
|
491
|
-
else
|
492
|
-
true
|
493
|
-
end
|
596
|
+
true
|
494
597
|
end
|
495
598
|
|
496
599
|
# Attempts to find the inverse association name automatically.
|
497
600
|
# If it cannot find a suitable inverse association name, it returns
|
498
|
-
# nil
|
601
|
+
# +nil+.
|
499
602
|
def inverse_name
|
500
|
-
|
501
|
-
|
502
|
-
nil
|
503
|
-
else
|
504
|
-
@automatic_inverse_of ||= automatic_inverse_of
|
505
|
-
end
|
603
|
+
unless defined?(@inverse_name)
|
604
|
+
@inverse_name = options.fetch(:inverse_of) { automatic_inverse_of }
|
506
605
|
end
|
606
|
+
|
607
|
+
@inverse_name
|
507
608
|
end
|
508
609
|
|
509
|
-
# returns either nil or the inverse association name that it finds.
|
610
|
+
# returns either +nil+ or the inverse association name that it finds.
|
510
611
|
def automatic_inverse_of
|
511
612
|
if can_find_inverse_of_automatically?(self)
|
512
613
|
inverse_name = ActiveSupport::Inflector.underscore(options[:as] || active_record.name.demodulize).to_sym
|
@@ -520,23 +621,18 @@ module ActiveRecord
|
|
520
621
|
end
|
521
622
|
|
522
623
|
if valid_inverse_reflection?(reflection)
|
523
|
-
|
624
|
+
inverse_name
|
524
625
|
end
|
525
626
|
end
|
526
|
-
|
527
|
-
false
|
528
627
|
end
|
529
628
|
|
530
629
|
# Checks if the inverse reflection that is returned from the
|
531
630
|
# +automatic_inverse_of+ method is a valid reflection. We must
|
532
631
|
# make sure that the reflection's active_record name matches up
|
533
632
|
# with the current reflection's klass name.
|
534
|
-
#
|
535
|
-
# Note: klass will always be valid because when there's a NameError
|
536
|
-
# from calling +klass+, +reflection+ will already be set to false.
|
537
633
|
def valid_inverse_reflection?(reflection)
|
538
634
|
reflection &&
|
539
|
-
klass
|
635
|
+
klass <= reflection.active_record &&
|
540
636
|
can_find_inverse_of_automatically?(reflection)
|
541
637
|
end
|
542
638
|
|
@@ -544,9 +640,8 @@ module ActiveRecord
|
|
544
640
|
# us from being able to guess the inverse automatically. First, the
|
545
641
|
# <tt>inverse_of</tt> option cannot be set to false. Second, we must
|
546
642
|
# have <tt>has_many</tt>, <tt>has_one</tt>, <tt>belongs_to</tt> associations.
|
547
|
-
# Third, we must not have options such as <tt>:
|
548
|
-
#
|
549
|
-
# inverse association.
|
643
|
+
# Third, we must not have options such as <tt>:foreign_key</tt>
|
644
|
+
# which prevent us from correctly guessing the inverse association.
|
550
645
|
#
|
551
646
|
# Anything with a scope can additionally ruin our attempt at finding an
|
552
647
|
# inverse, so we exclude reflections with scopes.
|
@@ -576,56 +671,77 @@ module ActiveRecord
|
|
576
671
|
def derive_join_table
|
577
672
|
ModelSchema.derive_join_table_name active_record.table_name, klass.table_name
|
578
673
|
end
|
579
|
-
|
580
|
-
def primary_key(klass)
|
581
|
-
klass.primary_key || raise(UnknownPrimaryKey.new(klass))
|
582
|
-
end
|
583
674
|
end
|
584
675
|
|
585
676
|
class HasManyReflection < AssociationReflection # :nodoc:
|
586
|
-
def initialize(name, scope, options, active_record)
|
587
|
-
super(name, scope, options, active_record)
|
588
|
-
end
|
589
|
-
|
590
677
|
def macro; :has_many; end
|
591
678
|
|
592
679
|
def collection?; true; end
|
593
|
-
end
|
594
680
|
|
595
|
-
|
596
|
-
|
597
|
-
|
681
|
+
def association_class
|
682
|
+
if options[:through]
|
683
|
+
Associations::HasManyThroughAssociation
|
684
|
+
else
|
685
|
+
Associations::HasManyAssociation
|
686
|
+
end
|
598
687
|
end
|
599
688
|
|
689
|
+
def association_primary_key(klass = nil)
|
690
|
+
primary_key(klass || self.klass)
|
691
|
+
end
|
692
|
+
end
|
693
|
+
|
694
|
+
class HasOneReflection < AssociationReflection # :nodoc:
|
600
695
|
def macro; :has_one; end
|
601
696
|
|
602
697
|
def has_one?; true; end
|
603
|
-
end
|
604
698
|
|
605
|
-
|
606
|
-
|
607
|
-
|
699
|
+
def association_class
|
700
|
+
if options[:through]
|
701
|
+
Associations::HasOneThroughAssociation
|
702
|
+
else
|
703
|
+
Associations::HasOneAssociation
|
704
|
+
end
|
608
705
|
end
|
609
706
|
|
707
|
+
private
|
708
|
+
def calculate_constructable(macro, options)
|
709
|
+
!options[:through]
|
710
|
+
end
|
711
|
+
end
|
712
|
+
|
713
|
+
class BelongsToReflection < AssociationReflection # :nodoc:
|
610
714
|
def macro; :belongs_to; end
|
611
715
|
|
612
716
|
def belongs_to?; true; end
|
613
717
|
|
614
|
-
def
|
615
|
-
|
616
|
-
|
718
|
+
def association_class
|
719
|
+
if polymorphic?
|
720
|
+
Associations::BelongsToPolymorphicAssociation
|
721
|
+
else
|
722
|
+
Associations::BelongsToAssociation
|
723
|
+
end
|
617
724
|
end
|
618
725
|
|
619
|
-
def
|
620
|
-
|
726
|
+
def join_primary_key(klass = nil)
|
727
|
+
polymorphic? ? association_primary_key(klass) : association_primary_key
|
621
728
|
end
|
622
|
-
end
|
623
729
|
|
624
|
-
|
625
|
-
|
626
|
-
super
|
730
|
+
def join_foreign_key
|
731
|
+
foreign_key
|
627
732
|
end
|
628
733
|
|
734
|
+
private
|
735
|
+
def can_find_inverse_of_automatically?(_)
|
736
|
+
!polymorphic? && super
|
737
|
+
end
|
738
|
+
|
739
|
+
def calculate_constructable(macro, options)
|
740
|
+
!polymorphic?
|
741
|
+
end
|
742
|
+
end
|
743
|
+
|
744
|
+
class HasAndBelongsToManyReflection < AssociationReflection # :nodoc:
|
629
745
|
def macro; :has_and_belongs_to_many; end
|
630
746
|
|
631
747
|
def collection?
|
@@ -633,19 +749,22 @@ module ActiveRecord
|
|
633
749
|
end
|
634
750
|
end
|
635
751
|
|
636
|
-
# Holds all the
|
752
|
+
# Holds all the metadata about a :through association as it was specified
|
637
753
|
# in the Active Record class.
|
638
754
|
class ThroughReflection < AbstractReflection #:nodoc:
|
639
|
-
|
640
|
-
|
641
|
-
:active_record_primary_key, :type, :to => :source_reflection
|
755
|
+
delegate :foreign_key, :foreign_type, :association_foreign_key, :join_id_for,
|
756
|
+
:active_record_primary_key, :type, :get_join_keys, to: :source_reflection
|
642
757
|
|
643
758
|
def initialize(delegate_reflection)
|
644
759
|
@delegate_reflection = delegate_reflection
|
645
|
-
@klass
|
760
|
+
@klass = delegate_reflection.options[:anonymous_class]
|
646
761
|
@source_reflection_name = delegate_reflection.options[:source]
|
647
762
|
end
|
648
763
|
|
764
|
+
def through_reflection?
|
765
|
+
true
|
766
|
+
end
|
767
|
+
|
649
768
|
def klass
|
650
769
|
@klass ||= delegate_reflection.compute_class(class_name)
|
651
770
|
end
|
@@ -704,74 +823,35 @@ module ActiveRecord
|
|
704
823
|
# # => [<ActiveRecord::Reflection::ThroughReflection: @delegate_reflection=#<ActiveRecord::Reflection::HasManyReflection: @name=:tags...>,
|
705
824
|
# <ActiveRecord::Reflection::HasManyReflection: @name=:taggings, @options={}, @active_record=Post>]
|
706
825
|
#
|
707
|
-
def
|
708
|
-
|
709
|
-
a = source_reflection.chain
|
710
|
-
b = through_reflection.chain
|
711
|
-
chain = a + b
|
712
|
-
chain[0] = self # Use self so we don't lose the information from :source_type
|
713
|
-
chain
|
714
|
-
end
|
826
|
+
def collect_join_chain
|
827
|
+
collect_join_reflections [self]
|
715
828
|
end
|
716
829
|
|
717
|
-
#
|
718
|
-
#
|
719
|
-
#
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
#
|
724
|
-
# class Article
|
725
|
-
# has_many :comments
|
726
|
-
# has_many :comment_tags, through: :comments, source: :tags
|
727
|
-
# end
|
728
|
-
#
|
729
|
-
# class Comment
|
730
|
-
# has_many :tags
|
731
|
-
# end
|
732
|
-
#
|
733
|
-
# There may be scopes on Person.comment_tags, Article.comment_tags and/or Comment.tags,
|
734
|
-
# but only Comment.tags will be represented in the #chain. So this method creates an array
|
735
|
-
# of scopes corresponding to the chain.
|
736
|
-
def scope_chain
|
737
|
-
@scope_chain ||= begin
|
738
|
-
scope_chain = source_reflection.scope_chain.map(&:dup)
|
739
|
-
|
740
|
-
# Add to it the scope from this reflection (if any)
|
741
|
-
scope_chain.first << scope if scope
|
742
|
-
|
743
|
-
through_scope_chain = through_reflection.scope_chain.map(&:dup)
|
744
|
-
|
745
|
-
if options[:source_type]
|
746
|
-
type = foreign_type
|
747
|
-
source_type = options[:source_type]
|
748
|
-
through_scope_chain.first << lambda { |object|
|
749
|
-
where(type => source_type)
|
750
|
-
}
|
751
|
-
end
|
752
|
-
|
753
|
-
# Recursively fill out the rest of the array from the through reflection
|
754
|
-
scope_chain + through_scope_chain
|
755
|
-
end
|
830
|
+
# This is for clearing cache on the reflection. Useful for tests that need to compare
|
831
|
+
# SQL queries on associations.
|
832
|
+
def clear_association_scope_cache # :nodoc:
|
833
|
+
delegate_reflection.clear_association_scope_cache
|
834
|
+
source_reflection.clear_association_scope_cache
|
835
|
+
through_reflection.clear_association_scope_cache
|
756
836
|
end
|
757
837
|
|
758
|
-
def
|
759
|
-
source_reflection.
|
838
|
+
def scopes
|
839
|
+
source_reflection.scopes + super
|
760
840
|
end
|
761
841
|
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
ActiveRecord::Base.source_macro is deprecated and will be removed
|
766
|
-
without replacement.
|
767
|
-
MSG
|
842
|
+
def join_scopes(table, predicate_builder) # :nodoc:
|
843
|
+
source_reflection.join_scopes(table, predicate_builder) + super
|
844
|
+
end
|
768
845
|
|
769
|
-
|
846
|
+
def has_scope?
|
847
|
+
scope || options[:source_type] ||
|
848
|
+
source_reflection.has_scope? ||
|
849
|
+
through_reflection.has_scope?
|
770
850
|
end
|
771
851
|
|
772
852
|
# A through association is nested if there would be more than one join table
|
773
853
|
def nested?
|
774
|
-
|
854
|
+
source_reflection.through_reflection? || through_reflection.through_reflection?
|
775
855
|
end
|
776
856
|
|
777
857
|
# We want to use the klass from this reflection, rather than just delegate straight to
|
@@ -801,21 +881,19 @@ module ActiveRecord
|
|
801
881
|
def source_reflection_name # :nodoc:
|
802
882
|
return @source_reflection_name if @source_reflection_name
|
803
883
|
|
804
|
-
names = [name.to_s.singularize, name].collect
|
884
|
+
names = [name.to_s.singularize, name].collect(&:to_sym).uniq
|
805
885
|
names = names.find_all { |n|
|
806
886
|
through_reflection.klass._reflect_on_association(n)
|
807
887
|
}
|
808
888
|
|
809
889
|
if names.length > 1
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
" #{macro} :#{name}, #{example_options}\n" \
|
818
|
-
" end"
|
890
|
+
raise AmbiguousSourceReflectionForThroughAssociation.new(
|
891
|
+
active_record.name,
|
892
|
+
macro,
|
893
|
+
name,
|
894
|
+
options,
|
895
|
+
source_reflection_names
|
896
|
+
)
|
819
897
|
end
|
820
898
|
|
821
899
|
@source_reflection_name = names.first
|
@@ -829,10 +907,6 @@ module ActiveRecord
|
|
829
907
|
through_reflection.options
|
830
908
|
end
|
831
909
|
|
832
|
-
def join_id_for(owner) # :nodoc:
|
833
|
-
source_reflection.join_id_for(owner)
|
834
|
-
end
|
835
|
-
|
836
910
|
def check_validity!
|
837
911
|
if through_reflection.nil?
|
838
912
|
raise HasManyThroughAssociationNotFoundError.new(active_record.name, self)
|
@@ -862,22 +936,54 @@ module ActiveRecord
|
|
862
936
|
raise HasOneThroughCantAssociateThroughCollection.new(active_record.name, self, through_reflection)
|
863
937
|
end
|
864
938
|
|
939
|
+
if parent_reflection.nil?
|
940
|
+
reflections = active_record.reflections.keys.map(&:to_sym)
|
941
|
+
|
942
|
+
if reflections.index(through_reflection.name) > reflections.index(name)
|
943
|
+
raise HasManyThroughOrderError.new(active_record.name, self, through_reflection)
|
944
|
+
end
|
945
|
+
end
|
946
|
+
|
865
947
|
check_validity_of_inverse!
|
866
948
|
end
|
867
949
|
|
868
|
-
|
950
|
+
def constraints
|
951
|
+
scope_chain = source_reflection.constraints
|
952
|
+
scope_chain << scope if scope
|
953
|
+
scope_chain
|
954
|
+
end
|
955
|
+
|
956
|
+
def add_as_source(seed)
|
957
|
+
collect_join_reflections seed
|
958
|
+
end
|
959
|
+
|
960
|
+
def add_as_polymorphic_through(reflection, seed)
|
961
|
+
collect_join_reflections(seed + [PolymorphicReflection.new(self, reflection)])
|
962
|
+
end
|
963
|
+
|
964
|
+
def add_as_through(seed)
|
965
|
+
collect_join_reflections(seed + [self])
|
966
|
+
end
|
869
967
|
|
968
|
+
protected
|
870
969
|
def actual_source_reflection # FIXME: this is a horrible name
|
871
|
-
source_reflection.
|
970
|
+
source_reflection.actual_source_reflection
|
872
971
|
end
|
873
972
|
|
874
|
-
|
875
|
-
|
973
|
+
private
|
974
|
+
attr_reader :delegate_reflection
|
975
|
+
|
976
|
+
def collect_join_reflections(seed)
|
977
|
+
a = source_reflection.add_as_source seed
|
978
|
+
if options[:source_type]
|
979
|
+
through_reflection.add_as_polymorphic_through self, a
|
980
|
+
else
|
981
|
+
through_reflection.add_as_through a
|
982
|
+
end
|
876
983
|
end
|
877
984
|
|
878
985
|
def inverse_name; delegate_reflection.send(:inverse_name); end
|
879
986
|
|
880
|
-
private
|
881
987
|
def derive_class_name
|
882
988
|
# get the class_name of the belongs_to association of the through reflection
|
883
989
|
options[:source_type] || source_reflection.class_name
|
@@ -887,7 +993,50 @@ module ActiveRecord
|
|
887
993
|
public_instance_methods
|
888
994
|
|
889
995
|
delegate(*delegate_methods, to: :delegate_reflection)
|
996
|
+
end
|
997
|
+
|
998
|
+
class PolymorphicReflection < AbstractReflection # :nodoc:
|
999
|
+
delegate :klass, :scope, :plural_name, :type, :get_join_keys, :scope_for, to: :@reflection
|
1000
|
+
|
1001
|
+
def initialize(reflection, previous_reflection)
|
1002
|
+
@reflection = reflection
|
1003
|
+
@previous_reflection = previous_reflection
|
1004
|
+
end
|
1005
|
+
|
1006
|
+
def join_scopes(table, predicate_builder) # :nodoc:
|
1007
|
+
scopes = @previous_reflection.join_scopes(table, predicate_builder) + super
|
1008
|
+
scopes << build_scope(table, predicate_builder).instance_exec(nil, &source_type_scope)
|
1009
|
+
end
|
1010
|
+
|
1011
|
+
def constraints
|
1012
|
+
@reflection.constraints + [source_type_scope]
|
1013
|
+
end
|
1014
|
+
|
1015
|
+
private
|
1016
|
+
def source_type_scope
|
1017
|
+
type = @previous_reflection.foreign_type
|
1018
|
+
source_type = @previous_reflection.options[:source_type]
|
1019
|
+
lambda { |object| where(type => source_type) }
|
1020
|
+
end
|
1021
|
+
end
|
1022
|
+
|
1023
|
+
class RuntimeReflection < AbstractReflection # :nodoc:
|
1024
|
+
delegate :scope, :type, :constraints, :get_join_keys, to: :@reflection
|
1025
|
+
|
1026
|
+
def initialize(reflection, association)
|
1027
|
+
@reflection = reflection
|
1028
|
+
@association = association
|
1029
|
+
end
|
1030
|
+
|
1031
|
+
def klass
|
1032
|
+
@association.klass
|
1033
|
+
end
|
1034
|
+
|
1035
|
+
def aliased_table
|
1036
|
+
@aliased_table ||= Arel::Table.new(table_name, type_caster: klass.type_caster)
|
1037
|
+
end
|
890
1038
|
|
1039
|
+
def all_includes; yield; end
|
891
1040
|
end
|
892
1041
|
end
|
893
1042
|
end
|