activerecord 3.2.22.5 → 5.2.8
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +657 -621
- data/MIT-LICENSE +2 -2
- data/README.rdoc +41 -46
- data/examples/performance.rb +55 -42
- data/examples/simple.rb +6 -5
- data/lib/active_record/aggregations.rb +264 -236
- data/lib/active_record/association_relation.rb +40 -0
- data/lib/active_record/associations/alias_tracker.rb +47 -42
- data/lib/active_record/associations/association.rb +127 -75
- data/lib/active_record/associations/association_scope.rb +126 -92
- data/lib/active_record/associations/belongs_to_association.rb +78 -27
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +9 -4
- data/lib/active_record/associations/builder/association.rb +117 -32
- data/lib/active_record/associations/builder/belongs_to.rb +135 -60
- data/lib/active_record/associations/builder/collection_association.rb +61 -54
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +120 -42
- data/lib/active_record/associations/builder/has_many.rb +10 -64
- data/lib/active_record/associations/builder/has_one.rb +19 -51
- data/lib/active_record/associations/builder/singular_association.rb +28 -18
- data/lib/active_record/associations/collection_association.rb +226 -293
- data/lib/active_record/associations/collection_proxy.rb +1067 -69
- data/lib/active_record/associations/foreign_association.rb +13 -0
- data/lib/active_record/associations/has_many_association.rb +83 -47
- data/lib/active_record/associations/has_many_through_association.rb +98 -65
- data/lib/active_record/associations/has_one_association.rb +57 -20
- data/lib/active_record/associations/has_one_through_association.rb +18 -9
- data/lib/active_record/associations/join_dependency/join_association.rb +48 -126
- data/lib/active_record/associations/join_dependency/join_base.rb +11 -12
- data/lib/active_record/associations/join_dependency/join_part.rb +35 -42
- data/lib/active_record/associations/join_dependency.rb +212 -164
- data/lib/active_record/associations/preloader/association.rb +95 -89
- data/lib/active_record/associations/preloader/through_association.rb +84 -44
- data/lib/active_record/associations/preloader.rb +123 -111
- data/lib/active_record/associations/singular_association.rb +33 -24
- data/lib/active_record/associations/through_association.rb +60 -26
- data/lib/active_record/associations.rb +1759 -1506
- data/lib/active_record/attribute_assignment.rb +60 -193
- data/lib/active_record/attribute_decorators.rb +90 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +55 -8
- data/lib/active_record/attribute_methods/dirty.rb +113 -74
- data/lib/active_record/attribute_methods/primary_key.rb +106 -77
- data/lib/active_record/attribute_methods/query.rb +8 -5
- data/lib/active_record/attribute_methods/read.rb +63 -114
- data/lib/active_record/attribute_methods/serialization.rb +60 -90
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -43
- data/lib/active_record/attribute_methods/write.rb +43 -45
- data/lib/active_record/attribute_methods.rb +366 -149
- data/lib/active_record/attributes.rb +266 -0
- data/lib/active_record/autosave_association.rb +312 -225
- data/lib/active_record/base.rb +114 -505
- data/lib/active_record/callbacks.rb +145 -67
- data/lib/active_record/coders/json.rb +15 -0
- data/lib/active_record/coders/yaml_column.rb +32 -23
- data/lib/active_record/collection_cache_key.rb +53 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +883 -284
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +16 -2
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +350 -200
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +82 -27
- data/lib/active_record/connection_adapters/abstract/quoting.rb +150 -65
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +146 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +477 -284
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +95 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1100 -310
- data/lib/active_record/connection_adapters/abstract/transaction.rb +283 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +450 -118
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +657 -446
- data/lib/active_record/connection_adapters/column.rb +50 -255
- data/lib/active_record/connection_adapters/connection_specification.rb +287 -0
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +59 -210
- data/lib/active_record/connection_adapters/postgresql/column.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +163 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +56 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +111 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +168 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +206 -0
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +774 -0
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +620 -1080
- data/lib/active_record/connection_adapters/schema_cache.rb +85 -36
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -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 +106 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +545 -27
- data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
- data/lib/active_record/connection_handling.rb +145 -0
- data/lib/active_record/core.rb +559 -0
- data/lib/active_record/counter_cache.rb +200 -105
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +107 -69
- data/lib/active_record/enum.rb +244 -0
- data/lib/active_record/errors.rb +245 -60
- data/lib/active_record/explain.rb +35 -71
- data/lib/active_record/explain_registry.rb +32 -0
- data/lib/active_record/explain_subscriber.rb +18 -9
- data/lib/active_record/fixture_set/file.rb +82 -0
- data/lib/active_record/fixtures.rb +418 -275
- data/lib/active_record/gem_version.rb +17 -0
- data/lib/active_record/inheritance.rb +209 -100
- data/lib/active_record/integration.rb +116 -21
- data/lib/active_record/internal_metadata.rb +45 -0
- data/lib/active_record/legacy_yaml_adapter.rb +48 -0
- data/lib/active_record/locale/en.yml +9 -1
- data/lib/active_record/locking/optimistic.rb +107 -94
- data/lib/active_record/locking/pessimistic.rb +20 -8
- data/lib/active_record/log_subscriber.rb +99 -34
- data/lib/active_record/migration/command_recorder.rb +199 -64
- data/lib/active_record/migration/compatibility.rb +217 -0
- data/lib/active_record/migration/join_table.rb +17 -0
- data/lib/active_record/migration.rb +893 -296
- data/lib/active_record/model_schema.rb +328 -175
- data/lib/active_record/nested_attributes.rb +338 -242
- data/lib/active_record/no_touching.rb +58 -0
- data/lib/active_record/null_relation.rb +68 -0
- data/lib/active_record/persistence.rb +557 -170
- data/lib/active_record/query_cache.rb +14 -43
- data/lib/active_record/querying.rb +36 -24
- data/lib/active_record/railtie.rb +147 -52
- data/lib/active_record/railties/console_sandbox.rb +5 -4
- data/lib/active_record/railties/controller_runtime.rb +13 -6
- data/lib/active_record/railties/databases.rake +206 -488
- data/lib/active_record/readonly_attributes.rb +4 -6
- data/lib/active_record/reflection.rb +734 -228
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/batches.rb +249 -52
- data/lib/active_record/relation/calculations.rb +330 -284
- data/lib/active_record/relation/delegation.rb +135 -37
- data/lib/active_record/relation/finder_methods.rb +450 -287
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +193 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder.rb +132 -43
- data/lib/active_record/relation/query_attribute.rb +45 -0
- data/lib/active_record/relation/query_methods.rb +1037 -221
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +48 -151
- data/lib/active_record/relation/where_clause.rb +186 -0
- data/lib/active_record/relation/where_clause_factory.rb +34 -0
- data/lib/active_record/relation.rb +451 -359
- data/lib/active_record/result.rb +129 -20
- data/lib/active_record/runtime_registry.rb +24 -0
- data/lib/active_record/sanitization.rb +164 -136
- data/lib/active_record/schema.rb +31 -19
- data/lib/active_record/schema_dumper.rb +154 -107
- data/lib/active_record/schema_migration.rb +56 -0
- data/lib/active_record/scoping/default.rb +108 -98
- data/lib/active_record/scoping/named.rb +125 -112
- data/lib/active_record/scoping.rb +77 -123
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +10 -6
- data/lib/active_record/statement_cache.rb +121 -0
- data/lib/active_record/store.rb +175 -16
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +82 -0
- data/lib/active_record/tasks/database_tasks.rb +337 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +143 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +83 -0
- data/lib/active_record/timestamp.rb +80 -41
- data/lib/active_record/touch_later.rb +64 -0
- data/lib/active_record/transactions.rb +240 -119
- data/lib/active_record/translation.rb +2 -0
- data/lib/active_record/type/adapter_specific_registry.rb +136 -0
- data/lib/active_record/type/date.rb +9 -0
- data/lib/active_record/type/date_time.rb +9 -0
- data/lib/active_record/type/decimal_without_scale.rb +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
- 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 +71 -0
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +21 -0
- data/lib/active_record/type/type_map.rb +62 -0
- data/lib/active_record/type/unsigned_integer.rb +17 -0
- data/lib/active_record/type.rb +79 -0
- data/lib/active_record/type_caster/connection.rb +33 -0
- data/lib/active_record/type_caster/map.rb +23 -0
- data/lib/active_record/type_caster.rb +9 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +35 -18
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +68 -0
- data/lib/active_record/validations/uniqueness.rb +133 -75
- data/lib/active_record/validations.rb +53 -43
- data/lib/active_record/version.rb +7 -7
- data/lib/active_record.rb +89 -57
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -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 +61 -8
- 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.tt +46 -0
- data/lib/rails/generators/active_record/migration.rb +28 -8
- data/lib/rails/generators/active_record/model/model_generator.rb +23 -22
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +13 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +1 -1
- data/lib/rails/generators/active_record.rb +10 -16
- metadata +141 -62
- data/examples/associations.png +0 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
- data/lib/active_record/associations/join_helper.rb +0 -55
- 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_and_belongs_to_many.rb +0 -60
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
- 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_methods/deprecated_underscore_read.rb +0 -32
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -191
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -441
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -583
- data/lib/active_record/dynamic_finder_match.rb +0 -68
- data/lib/active_record/dynamic_scope_match.rb +0 -23
- data/lib/active_record/fixtures/file.rb +0 -65
- data/lib/active_record/identity_map.rb +0 -162
- data/lib/active_record/observer.rb +0 -121
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -203
- data/lib/active_record/session_store.rb +0 -360
- data/lib/active_record/test_case.rb +0 -73
- data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
- data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
data/lib/active_record.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#--
|
2
|
-
# Copyright (c) 2004-
|
4
|
+
# Copyright (c) 2004-2018 David Heinemeier Hansson
|
3
5
|
#
|
4
6
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
7
|
# a copy of this software and associated documentation files (the
|
@@ -21,85 +23,93 @@
|
|
21
23
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
24
|
#++
|
23
25
|
|
24
|
-
require
|
25
|
-
require
|
26
|
-
require
|
27
|
-
require
|
26
|
+
require "active_support"
|
27
|
+
require "active_support/rails"
|
28
|
+
require "active_model"
|
29
|
+
require "arel"
|
30
|
+
require "yaml"
|
28
31
|
|
29
|
-
require
|
32
|
+
require "active_record/version"
|
33
|
+
require "active_model/attribute_set"
|
30
34
|
|
31
35
|
module ActiveRecord
|
32
36
|
extend ActiveSupport::Autoload
|
33
37
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
autoload :
|
38
|
+
autoload :Base
|
39
|
+
autoload :Callbacks
|
40
|
+
autoload :Core
|
41
|
+
autoload :ConnectionHandling
|
42
|
+
autoload :CounterCache
|
43
|
+
autoload :DynamicMatchers
|
44
|
+
autoload :Enum
|
45
|
+
autoload :InternalMetadata
|
46
|
+
autoload :Explain
|
47
|
+
autoload :Inheritance
|
48
|
+
autoload :Integration
|
49
|
+
autoload :Migration
|
50
|
+
autoload :Migrator, "active_record/migration"
|
51
|
+
autoload :ModelSchema
|
52
|
+
autoload :NestedAttributes
|
53
|
+
autoload :NoTouching
|
54
|
+
autoload :TouchLater
|
55
|
+
autoload :Persistence
|
56
|
+
autoload :QueryCache
|
57
|
+
autoload :Querying
|
58
|
+
autoload :CollectionCacheKey
|
59
|
+
autoload :ReadonlyAttributes
|
60
|
+
autoload :RecordInvalid, "active_record/validations"
|
61
|
+
autoload :Reflection
|
62
|
+
autoload :RuntimeRegistry
|
63
|
+
autoload :Sanitization
|
64
|
+
autoload :Schema
|
65
|
+
autoload :SchemaDumper
|
66
|
+
autoload :SchemaMigration
|
67
|
+
autoload :Scoping
|
68
|
+
autoload :Serialization
|
69
|
+
autoload :StatementCache
|
70
|
+
autoload :Store
|
71
|
+
autoload :Suppressor
|
72
|
+
autoload :Timestamp
|
73
|
+
autoload :Transactions
|
74
|
+
autoload :Translation
|
75
|
+
autoload :Validations
|
76
|
+
autoload :SecureToken
|
43
77
|
|
44
78
|
eager_autoload do
|
45
|
-
autoload :ActiveRecordError,
|
46
|
-
autoload :ConnectionNotEstablished,
|
47
|
-
autoload :ConnectionAdapters,
|
79
|
+
autoload :ActiveRecordError, "active_record/errors"
|
80
|
+
autoload :ConnectionNotEstablished, "active_record/errors"
|
81
|
+
autoload :ConnectionAdapters, "active_record/connection_adapters/abstract_adapter"
|
48
82
|
|
49
83
|
autoload :Aggregations
|
50
84
|
autoload :Associations
|
51
|
-
autoload :AttributeMethods
|
52
85
|
autoload :AttributeAssignment
|
86
|
+
autoload :AttributeMethods
|
53
87
|
autoload :AutosaveAssociation
|
54
88
|
|
89
|
+
autoload :LegacyYamlAdapter
|
90
|
+
|
55
91
|
autoload :Relation
|
92
|
+
autoload :AssociationRelation
|
93
|
+
autoload :NullRelation
|
56
94
|
|
57
|
-
autoload_under
|
95
|
+
autoload_under "relation" do
|
58
96
|
autoload :QueryMethods
|
59
97
|
autoload :FinderMethods
|
60
98
|
autoload :Calculations
|
61
99
|
autoload :PredicateBuilder
|
62
100
|
autoload :SpawnMethods
|
63
101
|
autoload :Batches
|
64
|
-
autoload :Explain
|
65
102
|
autoload :Delegation
|
66
103
|
end
|
67
104
|
|
68
|
-
autoload :Base
|
69
|
-
autoload :Callbacks
|
70
|
-
autoload :CounterCache
|
71
|
-
autoload :DynamicMatchers
|
72
|
-
autoload :DynamicFinderMatch
|
73
|
-
autoload :DynamicScopeMatch
|
74
|
-
autoload :Explain
|
75
|
-
autoload :IdentityMap
|
76
|
-
autoload :Inheritance
|
77
|
-
autoload :Integration
|
78
|
-
autoload :Migration
|
79
|
-
autoload :Migrator, 'active_record/migration'
|
80
|
-
autoload :ModelSchema
|
81
|
-
autoload :NestedAttributes
|
82
|
-
autoload :Observer
|
83
|
-
autoload :Persistence
|
84
|
-
autoload :QueryCache
|
85
|
-
autoload :Querying
|
86
|
-
autoload :ReadonlyAttributes
|
87
|
-
autoload :Reflection
|
88
105
|
autoload :Result
|
89
|
-
autoload :
|
90
|
-
autoload :
|
91
|
-
autoload :SchemaDumper
|
92
|
-
autoload :Scoping
|
93
|
-
autoload :Serialization
|
94
|
-
autoload :Store
|
95
|
-
autoload :Timestamp
|
96
|
-
autoload :Transactions
|
97
|
-
autoload :Translation
|
98
|
-
autoload :Validations
|
106
|
+
autoload :TableMetadata
|
107
|
+
autoload :Type
|
99
108
|
end
|
100
109
|
|
101
110
|
module Coders
|
102
|
-
autoload :YAMLColumn,
|
111
|
+
autoload :YAMLColumn, "active_record/coders/yaml_column"
|
112
|
+
autoload :JSON, "active_record/coders/json"
|
103
113
|
end
|
104
114
|
|
105
115
|
module AttributeMethods
|
@@ -114,7 +124,6 @@ module ActiveRecord
|
|
114
124
|
autoload :TimeZoneConversion
|
115
125
|
autoload :Write
|
116
126
|
autoload :Serialization
|
117
|
-
autoload :DeprecatedUnderscoreRead
|
118
127
|
end
|
119
128
|
end
|
120
129
|
|
@@ -132,7 +141,6 @@ module ActiveRecord
|
|
132
141
|
|
133
142
|
eager_autoload do
|
134
143
|
autoload :AbstractAdapter
|
135
|
-
autoload :ConnectionManagement, "active_record/connection_adapters/abstract/connection_pool"
|
136
144
|
end
|
137
145
|
end
|
138
146
|
|
@@ -145,12 +153,36 @@ module ActiveRecord
|
|
145
153
|
end
|
146
154
|
end
|
147
155
|
|
148
|
-
|
149
|
-
|
156
|
+
module Tasks
|
157
|
+
extend ActiveSupport::Autoload
|
158
|
+
|
159
|
+
autoload :DatabaseTasks
|
160
|
+
autoload :SQLiteDatabaseTasks, "active_record/tasks/sqlite_database_tasks"
|
161
|
+
autoload :MySQLDatabaseTasks, "active_record/tasks/mysql_database_tasks"
|
162
|
+
autoload :PostgreSQLDatabaseTasks,
|
163
|
+
"active_record/tasks/postgresql_database_tasks"
|
164
|
+
end
|
165
|
+
|
166
|
+
autoload :TestFixtures, "active_record/fixtures"
|
167
|
+
|
168
|
+
def self.eager_load!
|
169
|
+
super
|
170
|
+
ActiveRecord::Locking.eager_load!
|
171
|
+
ActiveRecord::Scoping.eager_load!
|
172
|
+
ActiveRecord::Associations.eager_load!
|
173
|
+
ActiveRecord::AttributeMethods.eager_load!
|
174
|
+
ActiveRecord::ConnectionAdapters.eager_load!
|
175
|
+
end
|
150
176
|
end
|
151
177
|
|
152
178
|
ActiveSupport.on_load(:active_record) do
|
153
179
|
Arel::Table.engine = self
|
154
180
|
end
|
155
181
|
|
156
|
-
|
182
|
+
ActiveSupport.on_load(:i18n) do
|
183
|
+
I18n.load_path << File.expand_path("active_record/locale/en.yml", __dir__)
|
184
|
+
end
|
185
|
+
|
186
|
+
YAML.load_tags["!ruby/object:ActiveRecord::AttributeSet"] = "ActiveModel::AttributeSet"
|
187
|
+
YAML.load_tags["!ruby/object:ActiveRecord::Attribute::FromDatabase"] = "ActiveModel::Attribute::FromDatabase"
|
188
|
+
YAML.load_tags["!ruby/object:ActiveRecord::LazyAttributeHash"] = "ActiveModel::LazyAttributeHash"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/active_record"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module Generators # :nodoc:
|
7
|
+
class ApplicationRecordGenerator < ::Rails::Generators::Base # :nodoc:
|
8
|
+
source_root File.expand_path("templates", __dir__)
|
9
|
+
|
10
|
+
# FIXME: Change this file to a symlink once RubyGems 2.5.0 is required.
|
11
|
+
def create_application_record
|
12
|
+
template "application_record.rb", application_record_file_name
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def application_record_file_name
|
18
|
+
@application_record_file_name ||=
|
19
|
+
if namespaced?
|
20
|
+
"app/models/#{namespaced_path}/application_record.rb"
|
21
|
+
else
|
22
|
+
"app/models/application_record.rb"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -1,25 +1,78 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/active_record"
|
2
4
|
|
3
5
|
module ActiveRecord
|
4
|
-
module Generators
|
5
|
-
class MigrationGenerator < Base
|
6
|
-
argument :attributes, :
|
6
|
+
module Generators # :nodoc:
|
7
|
+
class MigrationGenerator < Base # :nodoc:
|
8
|
+
argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
|
9
|
+
|
10
|
+
class_option :primary_key_type, type: :string, desc: "The type for primary key"
|
7
11
|
|
8
12
|
def create_migration_file
|
9
13
|
set_local_assigns!
|
10
|
-
|
14
|
+
validate_file_name!
|
15
|
+
migration_template @migration_template, File.join(db_migrate_path, "#{file_name}.rb")
|
11
16
|
end
|
12
17
|
|
18
|
+
# TODO Change this to private once we've dropped Ruby 2.2 support.
|
19
|
+
# Workaround for Ruby 2.2 "private attribute?" warning.
|
13
20
|
protected
|
14
|
-
attr_reader :migration_action
|
21
|
+
attr_reader :migration_action, :join_tables
|
15
22
|
|
23
|
+
private
|
24
|
+
|
25
|
+
# Sets the default migration template that is being used for the generation of the migration.
|
26
|
+
# Depending on command line arguments, the migration template and the table name instance
|
27
|
+
# variables are set up.
|
16
28
|
def set_local_assigns!
|
17
|
-
|
29
|
+
@migration_template = "migration.rb"
|
30
|
+
case file_name
|
31
|
+
when /^(add)_.*_to_(.*)/, /^(remove)_.*?_from_(.*)/
|
18
32
|
@migration_action = $1
|
19
|
-
@table_name = $2
|
33
|
+
@table_name = normalize_table_name($2)
|
34
|
+
when /join_table/
|
35
|
+
if attributes.length == 2
|
36
|
+
@migration_action = "join"
|
37
|
+
@join_tables = pluralize_table_names? ? attributes.map(&:plural_name) : attributes.map(&:singular_name)
|
38
|
+
|
39
|
+
set_index_names
|
40
|
+
end
|
41
|
+
when /^create_(.+)/
|
42
|
+
@table_name = normalize_table_name($1)
|
43
|
+
@migration_template = "create_table_migration.rb"
|
20
44
|
end
|
21
45
|
end
|
22
46
|
|
47
|
+
def set_index_names
|
48
|
+
attributes.each_with_index do |attr, i|
|
49
|
+
attr.index_name = [attr, attributes[i - 1]].map { |a| index_name_for(a) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def index_name_for(attribute)
|
54
|
+
if attribute.foreign_key?
|
55
|
+
attribute.name
|
56
|
+
else
|
57
|
+
attribute.name.singularize.foreign_key
|
58
|
+
end.to_sym
|
59
|
+
end
|
60
|
+
|
61
|
+
def attributes_with_index
|
62
|
+
attributes.select { |a| !a.reference? && a.has_index? }
|
63
|
+
end
|
64
|
+
|
65
|
+
# A migration file name can only contain underscores (_), lowercase characters,
|
66
|
+
# and numbers 0-9. Any other file name will raise an IllegalMigrationNameError.
|
67
|
+
def validate_file_name!
|
68
|
+
unless /^[_a-z0-9]+$/.match?(file_name)
|
69
|
+
raise IllegalMigrationNameError.new(file_name)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def normalize_table_name(_table_name)
|
74
|
+
pluralize_table_names? ? _table_name.pluralize : _table_name.singularize
|
75
|
+
end
|
23
76
|
end
|
24
77
|
end
|
25
78
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
2
|
+
def change
|
3
|
+
create_table :<%= table_name %><%= primary_key_type %> do |t|
|
4
|
+
<% attributes.each do |attribute| -%>
|
5
|
+
<% if attribute.password_digest? -%>
|
6
|
+
t.string :password_digest<%= attribute.inject_options %>
|
7
|
+
<% elsif attribute.token? -%>
|
8
|
+
t.string :<%= attribute.name %><%= attribute.inject_options %>
|
9
|
+
<% else -%>
|
10
|
+
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
|
11
|
+
<% end -%>
|
12
|
+
<% end -%>
|
13
|
+
<% if options[:timestamps] %>
|
14
|
+
t.timestamps
|
15
|
+
<% end -%>
|
16
|
+
end
|
17
|
+
<% attributes.select(&:token?).each do |attribute| -%>
|
18
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
|
19
|
+
<% end -%>
|
20
|
+
<% attributes_with_index.each do |attribute| -%>
|
21
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
22
|
+
<% end -%>
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
2
|
+
<%- if migration_action == 'add' -%>
|
3
|
+
def change
|
4
|
+
<% attributes.each do |attribute| -%>
|
5
|
+
<%- if attribute.reference? -%>
|
6
|
+
add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
|
7
|
+
<%- elsif attribute.token? -%>
|
8
|
+
add_column :<%= table_name %>, :<%= attribute.name %>, :string<%= attribute.inject_options %>
|
9
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
|
10
|
+
<%- else -%>
|
11
|
+
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
12
|
+
<%- if attribute.has_index? -%>
|
13
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
14
|
+
<%- end -%>
|
15
|
+
<%- end -%>
|
16
|
+
<%- end -%>
|
17
|
+
end
|
18
|
+
<%- elsif migration_action == 'join' -%>
|
19
|
+
def change
|
20
|
+
create_join_table :<%= join_tables.first %>, :<%= join_tables.second %> do |t|
|
21
|
+
<%- attributes.each do |attribute| -%>
|
22
|
+
<%- if attribute.reference? -%>
|
23
|
+
t.references :<%= attribute.name %><%= attribute.inject_options %>
|
24
|
+
<%- else -%>
|
25
|
+
<%= '# ' unless attribute.has_index? -%>t.index <%= attribute.index_name %><%= attribute.inject_index_options %>
|
26
|
+
<%- end -%>
|
27
|
+
<%- end -%>
|
28
|
+
end
|
29
|
+
end
|
30
|
+
<%- else -%>
|
31
|
+
def change
|
32
|
+
<% attributes.each do |attribute| -%>
|
33
|
+
<%- if migration_action -%>
|
34
|
+
<%- if attribute.reference? -%>
|
35
|
+
remove_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
|
36
|
+
<%- else -%>
|
37
|
+
<%- if attribute.has_index? -%>
|
38
|
+
remove_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
39
|
+
<%- end -%>
|
40
|
+
remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
41
|
+
<%- end -%>
|
42
|
+
<%- end -%>
|
43
|
+
<%- end -%>
|
44
|
+
end
|
45
|
+
<%- end -%>
|
46
|
+
end
|
@@ -1,15 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/migration"
|
4
|
+
|
1
5
|
module ActiveRecord
|
2
|
-
module Generators
|
6
|
+
module Generators # :nodoc:
|
3
7
|
module Migration
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
include Rails::Generators::Migration
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
# Implement the required interface for Rails::Generators::Migration.
|
13
|
+
def next_migration_number(dirname)
|
14
|
+
next_migration_number = current_migration_number(dirname) + 1
|
15
|
+
ActiveRecord::Migration.next_migration_number(next_migration_number)
|
11
16
|
end
|
12
17
|
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def primary_key_type
|
22
|
+
key_type = options[:primary_key_type]
|
23
|
+
", id: :#{key_type}" if key_type
|
24
|
+
end
|
25
|
+
|
26
|
+
def db_migrate_path
|
27
|
+
if defined?(Rails.application) && Rails.application
|
28
|
+
Rails.application.config.paths["db/migrate"].to_ary.first
|
29
|
+
else
|
30
|
+
"db/migrate"
|
31
|
+
end
|
32
|
+
end
|
13
33
|
end
|
14
34
|
end
|
15
35
|
end
|
@@ -1,47 +1,48 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/active_record"
|
2
4
|
|
3
5
|
module ActiveRecord
|
4
|
-
module Generators
|
5
|
-
class ModelGenerator < Base
|
6
|
-
argument :attributes, :
|
6
|
+
module Generators # :nodoc:
|
7
|
+
class ModelGenerator < Base # :nodoc:
|
8
|
+
argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
|
7
9
|
|
8
10
|
check_class_collision
|
9
11
|
|
10
|
-
class_option :migration,
|
11
|
-
class_option :timestamps, :
|
12
|
-
class_option :parent,
|
13
|
-
class_option :indexes,
|
12
|
+
class_option :migration, type: :boolean
|
13
|
+
class_option :timestamps, type: :boolean
|
14
|
+
class_option :parent, type: :string, desc: "The parent class for the generated model"
|
15
|
+
class_option :indexes, type: :boolean, default: true, desc: "Add indexes for references and belongs_to columns"
|
16
|
+
class_option :primary_key_type, type: :string, desc: "The type for primary key"
|
14
17
|
|
18
|
+
# creates the migration file for the model.
|
15
19
|
def create_migration_file
|
16
20
|
return unless options[:migration] && options[:parent].nil?
|
17
|
-
|
21
|
+
attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false
|
22
|
+
migration_template "../../migration/templates/create_table_migration.rb", File.join(db_migrate_path, "create_#{table_name}.rb")
|
18
23
|
end
|
19
24
|
|
20
25
|
def create_model_file
|
21
|
-
template
|
26
|
+
template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
|
22
27
|
end
|
23
28
|
|
24
29
|
def create_module_file
|
25
30
|
return if regular_class_path.empty?
|
26
|
-
template
|
27
|
-
end
|
28
|
-
|
29
|
-
def attributes_with_index
|
30
|
-
attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) }
|
31
|
-
end
|
32
|
-
|
33
|
-
def accessible_attributes
|
34
|
-
attributes.reject(&:reference?)
|
31
|
+
template "module.rb", File.join("app/models", "#{class_path.join('/')}.rb") if behavior == :invoke
|
35
32
|
end
|
36
33
|
|
37
34
|
hook_for :test_framework
|
38
35
|
|
39
|
-
|
36
|
+
private
|
40
37
|
|
41
|
-
def
|
42
|
-
|
38
|
+
def attributes_with_index
|
39
|
+
attributes.select { |a| !a.reference? && a.has_index? }
|
43
40
|
end
|
44
41
|
|
42
|
+
# Used by the migration template to determine the parent name of the model
|
43
|
+
def parent_class_name
|
44
|
+
options[:parent] || "ApplicationRecord"
|
45
|
+
end
|
45
46
|
end
|
46
47
|
end
|
47
48
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<% module_namespacing do -%>
|
2
|
+
class <%= class_name %> < <%= parent_class_name.classify %>
|
3
|
+
<% attributes.select(&:reference?).each do |attribute| -%>
|
4
|
+
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %>
|
5
|
+
<% end -%>
|
6
|
+
<% attributes.select(&:token?).each do |attribute| -%>
|
7
|
+
has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
|
8
|
+
<% end -%>
|
9
|
+
<% if attributes.any?(&:password_digest?) -%>
|
10
|
+
has_secure_password
|
11
|
+
<% end -%>
|
12
|
+
end
|
13
|
+
<% end -%>
|
@@ -1,24 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/named_base"
|
4
|
+
require "rails/generators/active_model"
|
5
|
+
require "rails/generators/active_record/migration"
|
6
|
+
require "active_record"
|
6
7
|
|
7
8
|
module ActiveRecord
|
8
|
-
module Generators
|
9
|
-
class Base < Rails::Generators::NamedBase
|
10
|
-
include
|
11
|
-
extend ActiveRecord::Generators::Migration
|
9
|
+
module Generators # :nodoc:
|
10
|
+
class Base < Rails::Generators::NamedBase # :nodoc:
|
11
|
+
include ActiveRecord::Generators::Migration
|
12
12
|
|
13
13
|
# Set the current directory as base for the inherited generators.
|
14
14
|
def self.base_root
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
# Implement the required interface for Rails::Generators::Migration.
|
19
|
-
def self.next_migration_number(dirname) #:nodoc:
|
20
|
-
next_migration_number = current_migration_number(dirname) + 1
|
21
|
-
ActiveRecord::Migration.next_migration_number(next_migration_number)
|
15
|
+
__dir__
|
22
16
|
end
|
23
17
|
end
|
24
18
|
end
|