activerecord 3.2.6 → 6.0.0

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 (371) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +611 -6417
  3. data/MIT-LICENSE +4 -2
  4. data/README.rdoc +44 -47
  5. data/examples/performance.rb +79 -71
  6. data/examples/simple.rb +6 -5
  7. data/lib/active_record/aggregations.rb +268 -238
  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 +173 -81
  11. data/lib/active_record/associations/association_scope.rb +124 -92
  12. data/lib/active_record/associations/belongs_to_association.rb +83 -38
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +11 -9
  14. data/lib/active_record/associations/builder/association.rb +113 -32
  15. data/lib/active_record/associations/builder/belongs_to.rb +105 -60
  16. data/lib/active_record/associations/builder/collection_association.rb +53 -56
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +98 -41
  18. data/lib/active_record/associations/builder/has_many.rb +11 -63
  19. data/lib/active_record/associations/builder/has_one.rb +47 -45
  20. data/lib/active_record/associations/builder/singular_association.rb +30 -18
  21. data/lib/active_record/associations/collection_association.rb +217 -295
  22. data/lib/active_record/associations/collection_proxy.rb +1074 -77
  23. data/lib/active_record/associations/foreign_association.rb +20 -0
  24. data/lib/active_record/associations/has_many_association.rb +78 -50
  25. data/lib/active_record/associations/has_many_through_association.rb +99 -61
  26. data/lib/active_record/associations/has_one_association.rb +75 -30
  27. data/lib/active_record/associations/has_one_through_association.rb +20 -11
  28. data/lib/active_record/associations/join_dependency/join_association.rb +45 -119
  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 +208 -164
  32. data/lib/active_record/associations/preloader/association.rb +93 -87
  33. data/lib/active_record/associations/preloader/through_association.rb +87 -38
  34. data/lib/active_record/associations/preloader.rb +134 -110
  35. data/lib/active_record/associations/singular_association.rb +19 -24
  36. data/lib/active_record/associations/through_association.rb +61 -27
  37. data/lib/active_record/associations.rb +1766 -1505
  38. data/lib/active_record/attribute_assignment.rb +57 -193
  39. data/lib/active_record/attribute_decorators.rb +90 -0
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +58 -8
  41. data/lib/active_record/attribute_methods/dirty.rb +187 -67
  42. data/lib/active_record/attribute_methods/primary_key.rb +100 -78
  43. data/lib/active_record/attribute_methods/query.rb +10 -8
  44. data/lib/active_record/attribute_methods/read.rb +29 -118
  45. data/lib/active_record/attribute_methods/serialization.rb +60 -72
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -42
  47. data/lib/active_record/attribute_methods/write.rb +36 -44
  48. data/lib/active_record/attribute_methods.rb +306 -161
  49. data/lib/active_record/attributes.rb +279 -0
  50. data/lib/active_record/autosave_association.rb +324 -238
  51. data/lib/active_record/base.rb +114 -507
  52. data/lib/active_record/callbacks.rb +147 -83
  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/connection_adapters/abstract/connection_pool.rb +962 -279
  56. data/lib/active_record/connection_adapters/abstract/database_limits.rb +32 -5
  57. data/lib/active_record/connection_adapters/abstract/database_statements.rb +331 -209
  58. data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -23
  59. data/lib/active_record/connection_adapters/abstract/quoting.rb +201 -65
  60. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  61. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +510 -289
  63. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +93 -0
  64. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1182 -313
  65. data/lib/active_record/connection_adapters/abstract/transaction.rb +323 -0
  66. data/lib/active_record/connection_adapters/abstract_adapter.rb +585 -120
  67. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +610 -463
  68. data/lib/active_record/connection_adapters/column.rb +58 -233
  69. data/lib/active_record/connection_adapters/connection_specification.rb +297 -0
  70. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +29 -0
  71. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  72. data/lib/active_record/connection_adapters/mysql/database_statements.rb +200 -0
  73. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  74. data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
  75. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +72 -0
  76. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +95 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +88 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
  79. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
  80. data/lib/active_record/connection_adapters/mysql2_adapter.rb +75 -207
  81. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -0
  82. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +182 -0
  83. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +53 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +113 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +26 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  107. data/lib/active_record/connection_adapters/postgresql/quoting.rb +205 -0
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +222 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +776 -0
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +695 -1052
  116. data/lib/active_record/connection_adapters/schema_cache.rb +115 -24
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
  118. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +118 -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 +103 -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 +137 -0
  125. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +528 -26
  126. data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
  127. data/lib/active_record/connection_handling.rb +267 -0
  128. data/lib/active_record/core.rb +599 -0
  129. data/lib/active_record/counter_cache.rb +177 -103
  130. data/lib/active_record/database_configurations/database_config.rb +37 -0
  131. data/lib/active_record/database_configurations/hash_config.rb +50 -0
  132. data/lib/active_record/database_configurations/url_config.rb +79 -0
  133. data/lib/active_record/database_configurations.rb +233 -0
  134. data/lib/active_record/define_callbacks.rb +22 -0
  135. data/lib/active_record/dynamic_matchers.rb +107 -64
  136. data/lib/active_record/enum.rb +274 -0
  137. data/lib/active_record/errors.rb +254 -61
  138. data/lib/active_record/explain.rb +35 -70
  139. data/lib/active_record/explain_registry.rb +32 -0
  140. data/lib/active_record/explain_subscriber.rb +18 -8
  141. data/lib/active_record/fixture_set/file.rb +82 -0
  142. data/lib/active_record/fixture_set/model_metadata.rb +33 -0
  143. data/lib/active_record/fixture_set/render_context.rb +17 -0
  144. data/lib/active_record/fixture_set/table_row.rb +153 -0
  145. data/lib/active_record/fixture_set/table_rows.rb +47 -0
  146. data/lib/active_record/fixtures.rb +291 -475
  147. data/lib/active_record/gem_version.rb +17 -0
  148. data/lib/active_record/inheritance.rb +219 -100
  149. data/lib/active_record/insert_all.rb +179 -0
  150. data/lib/active_record/integration.rb +175 -17
  151. data/lib/active_record/internal_metadata.rb +53 -0
  152. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  153. data/lib/active_record/locale/en.yml +9 -1
  154. data/lib/active_record/locking/optimistic.rb +106 -92
  155. data/lib/active_record/locking/pessimistic.rb +23 -11
  156. data/lib/active_record/log_subscriber.rb +80 -30
  157. data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
  158. data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
  159. data/lib/active_record/middleware/database_selector.rb +75 -0
  160. data/lib/active_record/migration/command_recorder.rb +235 -56
  161. data/lib/active_record/migration/compatibility.rb +244 -0
  162. data/lib/active_record/migration/join_table.rb +17 -0
  163. data/lib/active_record/migration.rb +917 -301
  164. data/lib/active_record/model_schema.rb +351 -175
  165. data/lib/active_record/nested_attributes.rb +366 -235
  166. data/lib/active_record/no_touching.rb +65 -0
  167. data/lib/active_record/null_relation.rb +68 -0
  168. data/lib/active_record/persistence.rb +761 -166
  169. data/lib/active_record/query_cache.rb +22 -44
  170. data/lib/active_record/querying.rb +55 -31
  171. data/lib/active_record/railtie.rb +185 -47
  172. data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
  173. data/lib/active_record/railties/console_sandbox.rb +5 -4
  174. data/lib/active_record/railties/controller_runtime.rb +35 -33
  175. data/lib/active_record/railties/databases.rake +366 -463
  176. data/lib/active_record/readonly_attributes.rb +4 -6
  177. data/lib/active_record/reflection.rb +736 -228
  178. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  179. data/lib/active_record/relation/batches.rb +252 -52
  180. data/lib/active_record/relation/calculations.rb +340 -270
  181. data/lib/active_record/relation/delegation.rb +117 -36
  182. data/lib/active_record/relation/finder_methods.rb +439 -286
  183. data/lib/active_record/relation/from_clause.rb +26 -0
  184. data/lib/active_record/relation/merger.rb +184 -0
  185. data/lib/active_record/relation/predicate_builder/array_handler.rb +49 -0
  186. data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
  187. data/lib/active_record/relation/predicate_builder/base_handler.rb +18 -0
  188. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
  189. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
  190. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
  191. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  192. data/lib/active_record/relation/predicate_builder.rb +131 -39
  193. data/lib/active_record/relation/query_attribute.rb +50 -0
  194. data/lib/active_record/relation/query_methods.rb +1163 -221
  195. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  196. data/lib/active_record/relation/spawn_methods.rb +49 -120
  197. data/lib/active_record/relation/where_clause.rb +190 -0
  198. data/lib/active_record/relation/where_clause_factory.rb +33 -0
  199. data/lib/active_record/relation.rb +671 -349
  200. data/lib/active_record/result.rb +149 -15
  201. data/lib/active_record/runtime_registry.rb +24 -0
  202. data/lib/active_record/sanitization.rb +153 -133
  203. data/lib/active_record/schema.rb +22 -19
  204. data/lib/active_record/schema_dumper.rb +178 -112
  205. data/lib/active_record/schema_migration.rb +60 -0
  206. data/lib/active_record/scoping/default.rb +107 -98
  207. data/lib/active_record/scoping/named.rb +130 -115
  208. data/lib/active_record/scoping.rb +77 -123
  209. data/lib/active_record/secure_token.rb +40 -0
  210. data/lib/active_record/serialization.rb +10 -6
  211. data/lib/active_record/statement_cache.rb +148 -0
  212. data/lib/active_record/store.rb +256 -16
  213. data/lib/active_record/suppressor.rb +61 -0
  214. data/lib/active_record/table_metadata.rb +75 -0
  215. data/lib/active_record/tasks/database_tasks.rb +506 -0
  216. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  217. data/lib/active_record/tasks/postgresql_database_tasks.rb +141 -0
  218. data/lib/active_record/tasks/sqlite_database_tasks.rb +77 -0
  219. data/lib/active_record/test_databases.rb +23 -0
  220. data/lib/active_record/test_fixtures.rb +224 -0
  221. data/lib/active_record/timestamp.rb +93 -39
  222. data/lib/active_record/touch_later.rb +66 -0
  223. data/lib/active_record/transactions.rb +260 -129
  224. data/lib/active_record/translation.rb +3 -1
  225. data/lib/active_record/type/adapter_specific_registry.rb +129 -0
  226. data/lib/active_record/type/date.rb +9 -0
  227. data/lib/active_record/type/date_time.rb +9 -0
  228. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  229. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  230. data/lib/active_record/type/internal/timezone.rb +17 -0
  231. data/lib/active_record/type/json.rb +30 -0
  232. data/lib/active_record/type/serialized.rb +71 -0
  233. data/lib/active_record/type/text.rb +11 -0
  234. data/lib/active_record/type/time.rb +21 -0
  235. data/lib/active_record/type/type_map.rb +62 -0
  236. data/lib/active_record/type/unsigned_integer.rb +17 -0
  237. data/lib/active_record/type.rb +78 -0
  238. data/lib/active_record/type_caster/connection.rb +34 -0
  239. data/lib/active_record/type_caster/map.rb +20 -0
  240. data/lib/active_record/type_caster.rb +9 -0
  241. data/lib/active_record/validations/absence.rb +25 -0
  242. data/lib/active_record/validations/associated.rb +35 -18
  243. data/lib/active_record/validations/length.rb +26 -0
  244. data/lib/active_record/validations/presence.rb +68 -0
  245. data/lib/active_record/validations/uniqueness.rb +123 -77
  246. data/lib/active_record/validations.rb +54 -43
  247. data/lib/active_record/version.rb +7 -7
  248. data/lib/active_record.rb +97 -49
  249. data/lib/arel/alias_predication.rb +9 -0
  250. data/lib/arel/attributes/attribute.rb +37 -0
  251. data/lib/arel/attributes.rb +22 -0
  252. data/lib/arel/collectors/bind.rb +24 -0
  253. data/lib/arel/collectors/composite.rb +31 -0
  254. data/lib/arel/collectors/plain_string.rb +20 -0
  255. data/lib/arel/collectors/sql_string.rb +20 -0
  256. data/lib/arel/collectors/substitute_binds.rb +28 -0
  257. data/lib/arel/crud.rb +42 -0
  258. data/lib/arel/delete_manager.rb +18 -0
  259. data/lib/arel/errors.rb +9 -0
  260. data/lib/arel/expressions.rb +29 -0
  261. data/lib/arel/factory_methods.rb +49 -0
  262. data/lib/arel/insert_manager.rb +49 -0
  263. data/lib/arel/math.rb +45 -0
  264. data/lib/arel/nodes/and.rb +32 -0
  265. data/lib/arel/nodes/ascending.rb +23 -0
  266. data/lib/arel/nodes/binary.rb +52 -0
  267. data/lib/arel/nodes/bind_param.rb +36 -0
  268. data/lib/arel/nodes/case.rb +55 -0
  269. data/lib/arel/nodes/casted.rb +50 -0
  270. data/lib/arel/nodes/comment.rb +29 -0
  271. data/lib/arel/nodes/count.rb +12 -0
  272. data/lib/arel/nodes/delete_statement.rb +45 -0
  273. data/lib/arel/nodes/descending.rb +23 -0
  274. data/lib/arel/nodes/equality.rb +18 -0
  275. data/lib/arel/nodes/extract.rb +24 -0
  276. data/lib/arel/nodes/false.rb +16 -0
  277. data/lib/arel/nodes/full_outer_join.rb +8 -0
  278. data/lib/arel/nodes/function.rb +44 -0
  279. data/lib/arel/nodes/grouping.rb +8 -0
  280. data/lib/arel/nodes/in.rb +8 -0
  281. data/lib/arel/nodes/infix_operation.rb +80 -0
  282. data/lib/arel/nodes/inner_join.rb +8 -0
  283. data/lib/arel/nodes/insert_statement.rb +37 -0
  284. data/lib/arel/nodes/join_source.rb +20 -0
  285. data/lib/arel/nodes/matches.rb +18 -0
  286. data/lib/arel/nodes/named_function.rb +23 -0
  287. data/lib/arel/nodes/node.rb +50 -0
  288. data/lib/arel/nodes/node_expression.rb +13 -0
  289. data/lib/arel/nodes/outer_join.rb +8 -0
  290. data/lib/arel/nodes/over.rb +15 -0
  291. data/lib/arel/nodes/regexp.rb +16 -0
  292. data/lib/arel/nodes/right_outer_join.rb +8 -0
  293. data/lib/arel/nodes/select_core.rb +67 -0
  294. data/lib/arel/nodes/select_statement.rb +41 -0
  295. data/lib/arel/nodes/sql_literal.rb +16 -0
  296. data/lib/arel/nodes/string_join.rb +11 -0
  297. data/lib/arel/nodes/table_alias.rb +27 -0
  298. data/lib/arel/nodes/terminal.rb +16 -0
  299. data/lib/arel/nodes/true.rb +16 -0
  300. data/lib/arel/nodes/unary.rb +45 -0
  301. data/lib/arel/nodes/unary_operation.rb +20 -0
  302. data/lib/arel/nodes/unqualified_column.rb +22 -0
  303. data/lib/arel/nodes/update_statement.rb +41 -0
  304. data/lib/arel/nodes/values_list.rb +9 -0
  305. data/lib/arel/nodes/window.rb +126 -0
  306. data/lib/arel/nodes/with.rb +11 -0
  307. data/lib/arel/nodes.rb +68 -0
  308. data/lib/arel/order_predications.rb +13 -0
  309. data/lib/arel/predications.rb +257 -0
  310. data/lib/arel/select_manager.rb +271 -0
  311. data/lib/arel/table.rb +110 -0
  312. data/lib/arel/tree_manager.rb +72 -0
  313. data/lib/arel/update_manager.rb +34 -0
  314. data/lib/arel/visitors/depth_first.rb +204 -0
  315. data/lib/arel/visitors/dot.rb +297 -0
  316. data/lib/arel/visitors/ibm_db.rb +34 -0
  317. data/lib/arel/visitors/informix.rb +62 -0
  318. data/lib/arel/visitors/mssql.rb +157 -0
  319. data/lib/arel/visitors/mysql.rb +83 -0
  320. data/lib/arel/visitors/oracle.rb +159 -0
  321. data/lib/arel/visitors/oracle12.rb +66 -0
  322. data/lib/arel/visitors/postgresql.rb +110 -0
  323. data/lib/arel/visitors/sqlite.rb +39 -0
  324. data/lib/arel/visitors/to_sql.rb +889 -0
  325. data/lib/arel/visitors/visitor.rb +46 -0
  326. data/lib/arel/visitors/where_sql.rb +23 -0
  327. data/lib/arel/visitors.rb +20 -0
  328. data/lib/arel/window_predications.rb +9 -0
  329. data/lib/arel.rb +51 -0
  330. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  331. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  332. data/lib/rails/generators/active_record/migration/migration_generator.rb +59 -9
  333. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  334. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +48 -0
  335. data/lib/rails/generators/active_record/migration.rb +41 -8
  336. data/lib/rails/generators/active_record/model/model_generator.rb +24 -22
  337. data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
  338. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +1 -1
  339. data/lib/rails/generators/active_record.rb +10 -16
  340. metadata +285 -149
  341. data/examples/associations.png +0 -0
  342. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
  343. data/lib/active_record/associations/join_helper.rb +0 -55
  344. data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
  345. data/lib/active_record/associations/preloader/collection_association.rb +0 -24
  346. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
  347. data/lib/active_record/associations/preloader/has_many.rb +0 -17
  348. data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
  349. data/lib/active_record/associations/preloader/has_one.rb +0 -23
  350. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  351. data/lib/active_record/associations/preloader/singular_association.rb +0 -21
  352. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
  353. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -188
  354. data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -426
  355. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -579
  356. data/lib/active_record/dynamic_finder_match.rb +0 -68
  357. data/lib/active_record/dynamic_scope_match.rb +0 -23
  358. data/lib/active_record/fixtures/file.rb +0 -65
  359. data/lib/active_record/identity_map.rb +0 -162
  360. data/lib/active_record/observer.rb +0 -121
  361. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  362. data/lib/active_record/serializers/xml_serializer.rb +0 -203
  363. data/lib/active_record/session_store.rb +0 -358
  364. data/lib/active_record/test_case.rb +0 -73
  365. data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
  366. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
  367. data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
  368. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  369. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
  370. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
  371. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,13 +1,13 @@
1
- require 'active_support/core_ext/array/wrap'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecord
4
- # = Active Record Callbacks
4
+ # = Active Record \Callbacks
5
5
  #
6
- # Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic
6
+ # \Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic
7
7
  # before or after an alteration of the object state. This can be used to make sure that associated and
8
- # dependent objects are deleted when +destroy+ is called (by overwriting +before_destroy+) or to massage attributes
9
- # before they're validated (by overwriting +before_validation+). As an example of the callbacks initiated, consider
10
- # the <tt>Base#save</tt> call for a new record:
8
+ # dependent objects are deleted when {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] is called (by overwriting +before_destroy+) or
9
+ # to massage attributes before they're validated (by overwriting +before_validation+).
10
+ # As an example of the callbacks initiated, consider the {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] call for a new record:
11
11
  #
12
12
  # * (-) <tt>save</tt>
13
13
  # * (-) <tt>valid</tt>
@@ -22,22 +22,25 @@ module ActiveRecord
22
22
  # * (7) <tt>after_commit</tt>
23
23
  #
24
24
  # Also, an <tt>after_rollback</tt> callback can be configured to be triggered whenever a rollback is issued.
25
- # Check out <tt>ActiveRecord::Transactions</tt> for more details about <tt>after_commit</tt> and
25
+ # Check out ActiveRecord::Transactions for more details about <tt>after_commit</tt> and
26
26
  # <tt>after_rollback</tt>.
27
27
  #
28
- # Lastly an <tt>after_find</tt> and <tt>after_initialize</tt> callback is triggered for each object that
28
+ # Additionally, an <tt>after_touch</tt> callback is triggered whenever an
29
+ # object is touched.
30
+ #
31
+ # Lastly an <tt>after_find</tt> and <tt>after_initialize</tt> callback is triggered for each object that
29
32
  # is found and instantiated by a finder, with <tt>after_initialize</tt> being triggered after new objects
30
33
  # are instantiated as well.
31
34
  #
32
- # That's a total of twelve callbacks, which gives you immense power to react and prepare for each state in the
33
- # Active Record life cycle. The sequence for calling <tt>Base#save</tt> for an existing record is similar,
35
+ # There are nineteen callbacks in total, which give you immense power to react and prepare for each state in the
36
+ # Active Record life cycle. The sequence for calling {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] for an existing record is similar,
34
37
  # except that each <tt>_create</tt> callback is replaced by the corresponding <tt>_update</tt> callback.
35
38
  #
36
39
  # Examples:
37
40
  # class CreditCard < ActiveRecord::Base
38
41
  # # Strip everything but digits, so the user can specify "555 234 34" or
39
- # # "5552-3434" or both will mean "55523434"
40
- # before_validation(:on => :create) do
42
+ # # "5552-3434" and both will mean "55523434"
43
+ # before_validation(on: :create) do
41
44
  # self.number = number.gsub(/[^0-9]/, "") if attribute_present?("number")
42
45
  # end
43
46
  # end
@@ -52,9 +55,9 @@ module ActiveRecord
52
55
  # end
53
56
  #
54
57
  # class Firm < ActiveRecord::Base
55
- # # Destroys the associated clients and people when the firm is destroyed
56
- # before_destroy { |record| Person.destroy_all "firm_id = #{record.id}" }
57
- # before_destroy { |record| Client.destroy_all "client_of = #{record.id}" }
58
+ # # Disables access to the system, for associated clients and people when the firm is destroyed
59
+ # before_destroy { |record| Person.where(firm_id: record.id).update_all(access: 'disabled') }
60
+ # before_destroy { |record| Client.where(client_of: record.id).update_all(access: 'disabled') }
58
61
  # end
59
62
  #
60
63
  # == Inheritable callback queues
@@ -72,21 +75,7 @@ module ActiveRecord
72
75
  # end
73
76
  #
74
77
  # Now, when <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is
75
- # run, both +destroy_author+ and +destroy_readers+ are called. Contrast this to the following situation
76
- # where the +before_destroy+ method is overridden:
77
- #
78
- # class Topic < ActiveRecord::Base
79
- # def before_destroy() destroy_author end
80
- # end
81
- #
82
- # class Reply < Topic
83
- # def before_destroy() destroy_readers end
84
- # end
85
- #
86
- # In that case, <tt>Reply#destroy</tt> would only run +destroy_readers+ and _not_ +destroy_author+.
87
- # So, use the callback macros when you want to ensure that a certain callback is called for the entire
88
- # hierarchy, and use the regular overwriteable methods when you want to leave it up to each descendant
89
- # to decide whether they want to call +super+ and trigger the inherited callbacks.
78
+ # run, both +destroy_author+ and +destroy_readers+ are called.
90
79
  #
91
80
  # *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the
92
81
  # callbacks before specifying the associations. Otherwise, you might trigger the loading of a
@@ -95,9 +84,9 @@ module ActiveRecord
95
84
  # == Types of callbacks
96
85
  #
97
86
  # There are four types of callbacks accepted by the callback macros: Method references (symbol), callback objects,
98
- # inline methods (using a proc), and inline eval methods (using a string). Method references and callback objects
87
+ # inline methods (using a proc). Method references and callback objects
99
88
  # are the recommended approaches, inline methods using a proc are sometimes appropriate (such as for
100
- # creating mix-ins), and inline eval methods are deprecated.
89
+ # creating mix-ins).
101
90
  #
102
91
  # The method reference callbacks work by specifying a protected or private method available in the object, like this:
103
92
  #
@@ -106,7 +95,7 @@ module ActiveRecord
106
95
  #
107
96
  # private
108
97
  # def delete_parents
109
- # self.class.delete_all "parent_id = #{id}"
98
+ # self.class.delete_by(parent_id: id)
110
99
  # end
111
100
  # end
112
101
  #
@@ -127,7 +116,7 @@ module ActiveRecord
127
116
  # record.credit_card_number = decrypt(record.credit_card_number)
128
117
  # end
129
118
  #
130
- # alias_method :after_find, :after_save
119
+ # alias_method :after_initialize, :after_save
131
120
  #
132
121
  # private
133
122
  # def encrypt(value)
@@ -139,7 +128,7 @@ module ActiveRecord
139
128
  # end
140
129
  # end
141
130
  #
142
- # So you specify the object you want messaged on a given callback. When that callback is triggered, the object has
131
+ # So you specify the object you want to be messaged on a given callback. When that callback is triggered, the object has
143
132
  # a method by the name of the callback messaged. You can make these callbacks more flexible by passing in other
144
133
  # initialization data such as the name of the attribute to work with:
145
134
  #
@@ -162,7 +151,7 @@ module ActiveRecord
162
151
  # record.send("#{@attribute}=", decrypt(record.send("#{@attribute}")))
163
152
  # end
164
153
  #
165
- # alias_method :after_find, :after_save
154
+ # alias_method :after_initialize, :after_save
166
155
  #
167
156
  # private
168
157
  # def encrypt(value)
@@ -174,64 +163,135 @@ module ActiveRecord
174
163
  # end
175
164
  # end
176
165
  #
177
- # The callback macros usually accept a symbol for the method they're supposed to run, but you can also
178
- # pass a "method string", which will then be evaluated within the binding of the callback. Example:
166
+ # == <tt>before_validation*</tt> returning statements
167
+ #
168
+ # If the +before_validation+ callback throws +:abort+, the process will be
169
+ # aborted and {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] will return +false+.
170
+ # If {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] is called it will raise an ActiveRecord::RecordInvalid exception.
171
+ # Nothing will be appended to the errors object.
172
+ #
173
+ # == Canceling callbacks
174
+ #
175
+ # If a <tt>before_*</tt> callback throws +:abort+, all the later callbacks and
176
+ # the associated action are cancelled.
177
+ # Callbacks are generally run in the order they are defined, with the exception of callbacks defined as
178
+ # methods on the model, which are called last.
179
+ #
180
+ # == Ordering callbacks
181
+ #
182
+ # Sometimes the code needs that the callbacks execute in a specific order. For example, a +before_destroy+
183
+ # callback (+log_children+ in this case) should be executed before the children get destroyed by the
184
+ # <tt>dependent: :destroy</tt> option.
185
+ #
186
+ # Let's look at the code below:
179
187
  #
180
188
  # class Topic < ActiveRecord::Base
181
- # before_destroy 'self.class.delete_all "parent_id = #{id}"'
189
+ # has_many :children, dependent: :destroy
190
+ #
191
+ # before_destroy :log_children
192
+ #
193
+ # private
194
+ # def log_children
195
+ # # Child processing
196
+ # end
182
197
  # end
183
198
  #
184
- # Notice that single quotes (') are used so the <tt>#{id}</tt> part isn't evaluated until the callback
185
- # is triggered. Also note that these inline callbacks can be stacked just like the regular ones:
199
+ # In this case, the problem is that when the +before_destroy+ callback is executed, the children are not available
200
+ # because the {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] callback gets executed first.
201
+ # You can use the +prepend+ option on the +before_destroy+ callback to avoid this.
186
202
  #
187
203
  # class Topic < ActiveRecord::Base
188
- # before_destroy 'self.class.delete_all "parent_id = #{id}"',
189
- # 'puts "Evaluated after parents are destroyed"'
204
+ # has_many :children, dependent: :destroy
205
+ #
206
+ # before_destroy :log_children, prepend: true
207
+ #
208
+ # private
209
+ # def log_children
210
+ # # Child processing
211
+ # end
190
212
  # end
191
213
  #
192
- # == <tt>before_validation*</tt> returning statements
214
+ # This way, the +before_destroy+ gets executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
193
215
  #
194
- # If the returning value of a +before_validation+ callback can be evaluated to +false+, the process will be
195
- # aborted and <tt>Base#save</tt> will return +false+. If Base#save! is called it will raise a
196
- # ActiveRecord::RecordInvalid exception. Nothing will be appended to the errors object.
216
+ # Also, there are cases when you want several callbacks of the same type to
217
+ # be executed in order.
197
218
  #
198
- # == Canceling callbacks
219
+ # For example:
199
220
  #
200
- # If a <tt>before_*</tt> callback returns +false+, all the later callbacks and the associated action are
201
- # cancelled. If an <tt>after_*</tt> callback returns +false+, all the later callbacks are cancelled.
202
- # Callbacks are generally run in the order they are defined, with the exception of callbacks defined as
203
- # methods on the model, which are called last.
221
+ # class Topic < ActiveRecord::Base
222
+ # has_many :children
223
+ #
224
+ # after_save :log_children
225
+ # after_save :do_something_else
226
+ #
227
+ # private
228
+ #
229
+ # def log_children
230
+ # # Child processing
231
+ # end
204
232
  #
205
- # == Transactions
233
+ # def do_something_else
234
+ # # Something else
235
+ # end
236
+ # end
206
237
  #
207
- # The entire callback chain of a +save+, <tt>save!</tt>, or +destroy+ call runs
208
- # within a transaction. That includes <tt>after_*</tt> hooks. If everything
209
- # goes fine a COMMIT is executed once the chain has been completed.
238
+ # In this case the +log_children+ gets executed before +do_something_else+.
239
+ # The same applies to all non-transactional callbacks.
240
+ #
241
+ # In case there are multiple transactional callbacks as seen below, the order
242
+ # is reversed.
243
+ #
244
+ # For example:
245
+ #
246
+ # class Topic < ActiveRecord::Base
247
+ # has_many :children
248
+ #
249
+ # after_commit :log_children
250
+ # after_commit :do_something_else
251
+ #
252
+ # private
253
+ #
254
+ # def log_children
255
+ # # Child processing
256
+ # end
257
+ #
258
+ # def do_something_else
259
+ # # Something else
260
+ # end
261
+ # end
262
+ #
263
+ # In this case the +do_something_else+ gets executed before +log_children+.
264
+ #
265
+ # == \Transactions
266
+ #
267
+ # The entire callback chain of a {#save}[rdoc-ref:Persistence#save], {#save!}[rdoc-ref:Persistence#save!],
268
+ # or {#destroy}[rdoc-ref:Persistence#destroy] call runs within a transaction. That includes <tt>after_*</tt> hooks.
269
+ # If everything goes fine a COMMIT is executed once the chain has been completed.
210
270
  #
211
271
  # If a <tt>before_*</tt> callback cancels the action a ROLLBACK is issued. You
212
272
  # can also trigger a ROLLBACK raising an exception in any of the callbacks,
213
273
  # including <tt>after_*</tt> hooks. Note, however, that in that case the client
214
- # needs to be aware of it because an ordinary +save+ will raise such exception
274
+ # needs to be aware of it because an ordinary {#save}[rdoc-ref:Persistence#save] will raise such exception
215
275
  # instead of quietly returning +false+.
216
276
  #
217
277
  # == Debugging callbacks
218
- #
219
- # The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object. ActiveModel Callbacks support
278
+ #
279
+ # The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object. Active Model \Callbacks support
220
280
  # <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt> property
221
281
  # defines what part of the chain the callback runs in.
222
- #
223
- # To find all callbacks in the before_save callback chain:
224
- #
282
+ #
283
+ # To find all callbacks in the before_save callback chain:
284
+ #
225
285
  # Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }
226
- #
286
+ #
227
287
  # Returns an array of callback objects that form the before_save chain.
228
- #
288
+ #
229
289
  # To further check if the before_save chain contains a proc defined as <tt>rest_when_dead</tt> use the <tt>filter</tt> property of the callback object:
230
- #
290
+ #
231
291
  # Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).include?(:rest_when_dead)
232
- #
292
+ #
233
293
  # Returns true or false depending on whether the proc is contained in the before_save callback chain on a Topic model.
234
- #
294
+ #
235
295
  module Callbacks
236
296
  extend ActiveSupport::Concern
237
297
 
@@ -242,34 +302,38 @@ module ActiveRecord
242
302
  :before_destroy, :around_destroy, :after_destroy, :after_commit, :after_rollback
243
303
  ]
244
304
 
245
- included do
246
- extend ActiveModel::Callbacks
247
- include ActiveModel::Validations::Callbacks
248
-
249
- define_model_callbacks :initialize, :find, :touch, :only => :after
250
- define_model_callbacks :save, :create, :update, :destroy
251
- end
252
-
253
305
  def destroy #:nodoc:
254
- run_callbacks(:destroy) { super }
306
+ @_destroy_callback_already_called ||= false
307
+ return if @_destroy_callback_already_called
308
+ @_destroy_callback_already_called = true
309
+ _run_destroy_callbacks { super }
310
+ rescue RecordNotDestroyed => e
311
+ @_association_destroy_exception = e
312
+ false
313
+ ensure
314
+ @_destroy_callback_already_called = false
255
315
  end
256
316
 
257
317
  def touch(*) #:nodoc:
258
- run_callbacks(:touch) { super }
318
+ _run_touch_callbacks { super }
319
+ end
320
+
321
+ def increment!(attribute, by = 1, touch: nil) # :nodoc:
322
+ touch ? _run_touch_callbacks { super } : super
259
323
  end
260
324
 
261
325
  private
262
326
 
263
- def create_or_update #:nodoc:
264
- run_callbacks(:save) { super }
327
+ def create_or_update(**)
328
+ _run_save_callbacks { super }
265
329
  end
266
330
 
267
- def create #:nodoc:
268
- run_callbacks(:create) { super }
331
+ def _create_record
332
+ _run_create_callbacks { super }
269
333
  end
270
334
 
271
- def update(*) #:nodoc:
272
- run_callbacks(:update) { super }
335
+ def _update_record
336
+ _run_update_callbacks { super }
273
337
  end
274
338
  end
275
339
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Coders # :nodoc:
5
+ class JSON # :nodoc:
6
+ def self.dump(obj)
7
+ ActiveSupport::JSON.encode(obj)
8
+ end
9
+
10
+ def self.load(json)
11
+ ActiveSupport::JSON.decode(json) unless json.blank?
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,41 +1,50 @@
1
- module ActiveRecord
2
- # :stopdoc:
3
- module Coders
4
- class YAMLColumn
5
- RESCUE_ERRORS = [ ArgumentError ]
1
+ # frozen_string_literal: true
6
2
 
7
- if defined?(Psych) && defined?(Psych::SyntaxError)
8
- RESCUE_ERRORS << Psych::SyntaxError
9
- end
3
+ require "yaml"
10
4
 
5
+ module ActiveRecord
6
+ module Coders # :nodoc:
7
+ class YAMLColumn # :nodoc:
11
8
  attr_accessor :object_class
12
9
 
13
- def initialize(object_class = Object)
10
+ def initialize(attr_name, object_class = Object)
11
+ @attr_name = attr_name
14
12
  @object_class = object_class
13
+ check_arity_of_constructor
15
14
  end
16
15
 
17
16
  def dump(obj)
17
+ return if obj.nil?
18
+
19
+ assert_valid_value(obj, action: "dump")
18
20
  YAML.dump obj
19
21
  end
20
22
 
21
23
  def load(yaml)
22
24
  return object_class.new if object_class != Object && yaml.nil?
23
- return yaml unless yaml.is_a?(String) && yaml =~ /^---/
24
- begin
25
- obj = YAML.load(yaml)
26
-
27
- unless obj.is_a?(object_class) || obj.nil?
28
- raise SerializationTypeMismatch,
29
- "Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
30
- end
31
- obj ||= object_class.new if object_class != Object
32
-
33
- obj
34
- rescue *RESCUE_ERRORS
35
- yaml
25
+ return yaml unless yaml.is_a?(String) && /^---/.match?(yaml)
26
+ obj = YAML.load(yaml)
27
+
28
+ assert_valid_value(obj, action: "load")
29
+ obj ||= object_class.new if object_class != Object
30
+
31
+ obj
32
+ end
33
+
34
+ def assert_valid_value(obj, action:)
35
+ unless obj.nil? || obj.is_a?(object_class)
36
+ raise SerializationTypeMismatch,
37
+ "can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
36
38
  end
37
39
  end
40
+
41
+ private
42
+
43
+ def check_arity_of_constructor
44
+ load(nil)
45
+ rescue ArgumentError
46
+ raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
47
+ end
38
48
  end
39
49
  end
40
- # :startdoc
41
50
  end