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.

Files changed (275) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +657 -621
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +41 -46
  5. data/examples/performance.rb +55 -42
  6. data/examples/simple.rb +6 -5
  7. data/lib/active_record/aggregations.rb +264 -236
  8. data/lib/active_record/association_relation.rb +40 -0
  9. data/lib/active_record/associations/alias_tracker.rb +47 -42
  10. data/lib/active_record/associations/association.rb +127 -75
  11. data/lib/active_record/associations/association_scope.rb +126 -92
  12. data/lib/active_record/associations/belongs_to_association.rb +78 -27
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +9 -4
  14. data/lib/active_record/associations/builder/association.rb +117 -32
  15. data/lib/active_record/associations/builder/belongs_to.rb +135 -60
  16. data/lib/active_record/associations/builder/collection_association.rb +61 -54
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +120 -42
  18. data/lib/active_record/associations/builder/has_many.rb +10 -64
  19. data/lib/active_record/associations/builder/has_one.rb +19 -51
  20. data/lib/active_record/associations/builder/singular_association.rb +28 -18
  21. data/lib/active_record/associations/collection_association.rb +226 -293
  22. data/lib/active_record/associations/collection_proxy.rb +1067 -69
  23. data/lib/active_record/associations/foreign_association.rb +13 -0
  24. data/lib/active_record/associations/has_many_association.rb +83 -47
  25. data/lib/active_record/associations/has_many_through_association.rb +98 -65
  26. data/lib/active_record/associations/has_one_association.rb +57 -20
  27. data/lib/active_record/associations/has_one_through_association.rb +18 -9
  28. data/lib/active_record/associations/join_dependency/join_association.rb +48 -126
  29. data/lib/active_record/associations/join_dependency/join_base.rb +11 -12
  30. data/lib/active_record/associations/join_dependency/join_part.rb +35 -42
  31. data/lib/active_record/associations/join_dependency.rb +212 -164
  32. data/lib/active_record/associations/preloader/association.rb +95 -89
  33. data/lib/active_record/associations/preloader/through_association.rb +84 -44
  34. data/lib/active_record/associations/preloader.rb +123 -111
  35. data/lib/active_record/associations/singular_association.rb +33 -24
  36. data/lib/active_record/associations/through_association.rb +60 -26
  37. data/lib/active_record/associations.rb +1759 -1506
  38. data/lib/active_record/attribute_assignment.rb +60 -193
  39. data/lib/active_record/attribute_decorators.rb +90 -0
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +55 -8
  41. data/lib/active_record/attribute_methods/dirty.rb +113 -74
  42. data/lib/active_record/attribute_methods/primary_key.rb +106 -77
  43. data/lib/active_record/attribute_methods/query.rb +8 -5
  44. data/lib/active_record/attribute_methods/read.rb +63 -114
  45. data/lib/active_record/attribute_methods/serialization.rb +60 -90
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -43
  47. data/lib/active_record/attribute_methods/write.rb +43 -45
  48. data/lib/active_record/attribute_methods.rb +366 -149
  49. data/lib/active_record/attributes.rb +266 -0
  50. data/lib/active_record/autosave_association.rb +312 -225
  51. data/lib/active_record/base.rb +114 -505
  52. data/lib/active_record/callbacks.rb +145 -67
  53. data/lib/active_record/coders/json.rb +15 -0
  54. data/lib/active_record/coders/yaml_column.rb +32 -23
  55. data/lib/active_record/collection_cache_key.rb +53 -0
  56. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +883 -284
  57. data/lib/active_record/connection_adapters/abstract/database_limits.rb +16 -2
  58. data/lib/active_record/connection_adapters/abstract/database_statements.rb +350 -200
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +82 -27
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +150 -65
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +146 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +477 -284
  64. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +95 -0
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1100 -310
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +283 -0
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +450 -118
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +657 -446
  69. data/lib/active_record/connection_adapters/column.rb +50 -255
  70. data/lib/active_record/connection_adapters/connection_specification.rb +287 -0
  71. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
  72. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  73. data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
  74. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  75. data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
  76. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
  79. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
  80. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
  81. data/lib/active_record/connection_adapters/mysql2_adapter.rb +59 -210
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +44 -0
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +163 -0
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +56 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +111 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  108. data/lib/active_record/connection_adapters/postgresql/quoting.rb +168 -0
  109. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +206 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  113. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +774 -0
  114. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
  115. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  116. data/lib/active_record/connection_adapters/postgresql_adapter.rb +620 -1080
  117. data/lib/active_record/connection_adapters/schema_cache.rb +85 -36
  118. data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
  119. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
  120. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -0
  121. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
  122. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +106 -0
  125. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +545 -27
  126. data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
  127. data/lib/active_record/connection_handling.rb +145 -0
  128. data/lib/active_record/core.rb +559 -0
  129. data/lib/active_record/counter_cache.rb +200 -105
  130. data/lib/active_record/define_callbacks.rb +22 -0
  131. data/lib/active_record/dynamic_matchers.rb +107 -69
  132. data/lib/active_record/enum.rb +244 -0
  133. data/lib/active_record/errors.rb +245 -60
  134. data/lib/active_record/explain.rb +35 -71
  135. data/lib/active_record/explain_registry.rb +32 -0
  136. data/lib/active_record/explain_subscriber.rb +18 -9
  137. data/lib/active_record/fixture_set/file.rb +82 -0
  138. data/lib/active_record/fixtures.rb +418 -275
  139. data/lib/active_record/gem_version.rb +17 -0
  140. data/lib/active_record/inheritance.rb +209 -100
  141. data/lib/active_record/integration.rb +116 -21
  142. data/lib/active_record/internal_metadata.rb +45 -0
  143. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  144. data/lib/active_record/locale/en.yml +9 -1
  145. data/lib/active_record/locking/optimistic.rb +107 -94
  146. data/lib/active_record/locking/pessimistic.rb +20 -8
  147. data/lib/active_record/log_subscriber.rb +99 -34
  148. data/lib/active_record/migration/command_recorder.rb +199 -64
  149. data/lib/active_record/migration/compatibility.rb +217 -0
  150. data/lib/active_record/migration/join_table.rb +17 -0
  151. data/lib/active_record/migration.rb +893 -296
  152. data/lib/active_record/model_schema.rb +328 -175
  153. data/lib/active_record/nested_attributes.rb +338 -242
  154. data/lib/active_record/no_touching.rb +58 -0
  155. data/lib/active_record/null_relation.rb +68 -0
  156. data/lib/active_record/persistence.rb +557 -170
  157. data/lib/active_record/query_cache.rb +14 -43
  158. data/lib/active_record/querying.rb +36 -24
  159. data/lib/active_record/railtie.rb +147 -52
  160. data/lib/active_record/railties/console_sandbox.rb +5 -4
  161. data/lib/active_record/railties/controller_runtime.rb +13 -6
  162. data/lib/active_record/railties/databases.rake +206 -488
  163. data/lib/active_record/readonly_attributes.rb +4 -6
  164. data/lib/active_record/reflection.rb +734 -228
  165. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  166. data/lib/active_record/relation/batches.rb +249 -52
  167. data/lib/active_record/relation/calculations.rb +330 -284
  168. data/lib/active_record/relation/delegation.rb +135 -37
  169. data/lib/active_record/relation/finder_methods.rb +450 -287
  170. data/lib/active_record/relation/from_clause.rb +26 -0
  171. data/lib/active_record/relation/merger.rb +193 -0
  172. data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
  173. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  174. data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
  175. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
  176. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  177. data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
  178. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  179. data/lib/active_record/relation/predicate_builder.rb +132 -43
  180. data/lib/active_record/relation/query_attribute.rb +45 -0
  181. data/lib/active_record/relation/query_methods.rb +1037 -221
  182. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  183. data/lib/active_record/relation/spawn_methods.rb +48 -151
  184. data/lib/active_record/relation/where_clause.rb +186 -0
  185. data/lib/active_record/relation/where_clause_factory.rb +34 -0
  186. data/lib/active_record/relation.rb +451 -359
  187. data/lib/active_record/result.rb +129 -20
  188. data/lib/active_record/runtime_registry.rb +24 -0
  189. data/lib/active_record/sanitization.rb +164 -136
  190. data/lib/active_record/schema.rb +31 -19
  191. data/lib/active_record/schema_dumper.rb +154 -107
  192. data/lib/active_record/schema_migration.rb +56 -0
  193. data/lib/active_record/scoping/default.rb +108 -98
  194. data/lib/active_record/scoping/named.rb +125 -112
  195. data/lib/active_record/scoping.rb +77 -123
  196. data/lib/active_record/secure_token.rb +40 -0
  197. data/lib/active_record/serialization.rb +10 -6
  198. data/lib/active_record/statement_cache.rb +121 -0
  199. data/lib/active_record/store.rb +175 -16
  200. data/lib/active_record/suppressor.rb +61 -0
  201. data/lib/active_record/table_metadata.rb +82 -0
  202. data/lib/active_record/tasks/database_tasks.rb +337 -0
  203. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  204. data/lib/active_record/tasks/postgresql_database_tasks.rb +143 -0
  205. data/lib/active_record/tasks/sqlite_database_tasks.rb +83 -0
  206. data/lib/active_record/timestamp.rb +80 -41
  207. data/lib/active_record/touch_later.rb +64 -0
  208. data/lib/active_record/transactions.rb +240 -119
  209. data/lib/active_record/translation.rb +2 -0
  210. data/lib/active_record/type/adapter_specific_registry.rb +136 -0
  211. data/lib/active_record/type/date.rb +9 -0
  212. data/lib/active_record/type/date_time.rb +9 -0
  213. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  214. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  215. data/lib/active_record/type/internal/timezone.rb +17 -0
  216. data/lib/active_record/type/json.rb +30 -0
  217. data/lib/active_record/type/serialized.rb +71 -0
  218. data/lib/active_record/type/text.rb +11 -0
  219. data/lib/active_record/type/time.rb +21 -0
  220. data/lib/active_record/type/type_map.rb +62 -0
  221. data/lib/active_record/type/unsigned_integer.rb +17 -0
  222. data/lib/active_record/type.rb +79 -0
  223. data/lib/active_record/type_caster/connection.rb +33 -0
  224. data/lib/active_record/type_caster/map.rb +23 -0
  225. data/lib/active_record/type_caster.rb +9 -0
  226. data/lib/active_record/validations/absence.rb +25 -0
  227. data/lib/active_record/validations/associated.rb +35 -18
  228. data/lib/active_record/validations/length.rb +26 -0
  229. data/lib/active_record/validations/presence.rb +68 -0
  230. data/lib/active_record/validations/uniqueness.rb +133 -75
  231. data/lib/active_record/validations.rb +53 -43
  232. data/lib/active_record/version.rb +7 -7
  233. data/lib/active_record.rb +89 -57
  234. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  235. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  236. data/lib/rails/generators/active_record/migration/migration_generator.rb +61 -8
  237. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  238. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +46 -0
  239. data/lib/rails/generators/active_record/migration.rb +28 -8
  240. data/lib/rails/generators/active_record/model/model_generator.rb +23 -22
  241. data/lib/rails/generators/active_record/model/templates/model.rb.tt +13 -0
  242. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +1 -1
  243. data/lib/rails/generators/active_record.rb +10 -16
  244. metadata +141 -62
  245. data/examples/associations.png +0 -0
  246. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
  247. data/lib/active_record/associations/join_helper.rb +0 -55
  248. data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
  249. data/lib/active_record/associations/preloader/collection_association.rb +0 -24
  250. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
  251. data/lib/active_record/associations/preloader/has_many.rb +0 -17
  252. data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
  253. data/lib/active_record/associations/preloader/has_one.rb +0 -23
  254. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  255. data/lib/active_record/associations/preloader/singular_association.rb +0 -21
  256. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
  257. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -191
  258. data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -441
  259. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -583
  260. data/lib/active_record/dynamic_finder_match.rb +0 -68
  261. data/lib/active_record/dynamic_scope_match.rb +0 -23
  262. data/lib/active_record/fixtures/file.rb +0 -65
  263. data/lib/active_record/identity_map.rb +0 -162
  264. data/lib/active_record/observer.rb +0 -121
  265. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  266. data/lib/active_record/serializers/xml_serializer.rb +0 -203
  267. data/lib/active_record/session_store.rb +0 -360
  268. data/lib/active_record/test_case.rb +0 -73
  269. data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
  270. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
  271. data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
  272. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  273. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
  274. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
  275. 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-2011 David Heinemeier Hansson
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 'active_support'
25
- require 'active_support/i18n'
26
- require 'active_model'
27
- require 'arel'
26
+ require "active_support"
27
+ require "active_support/rails"
28
+ require "active_model"
29
+ require "arel"
30
+ require "yaml"
28
31
 
29
- require 'active_record/version'
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
- # ActiveRecord::SessionStore depends on the abstract store in Action Pack.
35
- # Eager loading this class would break client code that eager loads Active
36
- # Record standalone.
37
- #
38
- # Note that the Rails application generator creates an initializer specific
39
- # for setting the session store. Thus, albeit in theory this autoload would
40
- # not be thread-safe, in practice it is because if the application uses this
41
- # session store its autoload happens at boot time.
42
- autoload :SessionStore
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, 'active_record/errors'
46
- autoload :ConnectionNotEstablished, 'active_record/errors'
47
- autoload :ConnectionAdapters, 'active_record/connection_adapters/abstract_adapter'
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 'relation' do
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 :Sanitization
90
- autoload :Schema
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, 'active_record/coders/yaml_column'
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
- autoload :TestCase
149
- autoload :TestFixtures, 'active_record/fixtures'
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
- I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'
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
@@ -0,0 +1,5 @@
1
+ <% module_namespacing do -%>
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ <% end -%>
@@ -1,25 +1,78 @@
1
- require 'rails/generators/active_record'
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, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
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
- migration_template "migration.rb", "db/migrate/#{file_name}.rb"
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
- if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
29
+ @migration_template = "migration.rb"
30
+ case file_name
31
+ when /^(add)_.*_to_(.*)/, /^(remove)_.*?_from_(.*)/
18
32
  @migration_action = $1
19
- @table_name = $2.pluralize
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
- # Implement the required interface for Rails::Generators::Migration.
5
- def next_migration_number(dirname) #:nodoc:
6
- next_migration_number = current_migration_number(dirname) + 1
7
- if ActiveRecord::Base.timestamped_migrations
8
- [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
9
- else
10
- "%.3d" % next_migration_number
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
- require 'rails/generators/active_record'
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, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
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, :type => :boolean
11
- class_option :timestamps, :type => :boolean
12
- class_option :parent, :type => :string, :desc => "The parent class for the generated model"
13
- class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns"
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
- migration_template "migration.rb", "db/migrate/create_#{table_name}.rb"
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 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
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 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
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
- protected
36
+ private
40
37
 
41
- def parent_class_name
42
- options[:parent] || "ActiveRecord::Base"
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,7 +1,7 @@
1
1
  <% module_namespacing do -%>
2
2
  module <%= class_path.map(&:camelize).join('::') %>
3
3
  def self.table_name_prefix
4
- '<%= class_path.join('_') %>_'
4
+ '<%= namespaced? ? namespaced_class_path.join('_') : class_path.join('_') %>_'
5
5
  end
6
6
  end
7
7
  <% end -%>
@@ -1,24 +1,18 @@
1
- require 'rails/generators/named_base'
2
- require 'rails/generators/migration'
3
- require 'rails/generators/active_model'
4
- require 'rails/generators/active_record/migration'
5
- require 'active_record'
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 #:nodoc:
10
- include Rails::Generators::Migration
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
- File.dirname(__FILE__)
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