activerecord 6.0.0 → 7.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (376) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +996 -594
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +34 -34
  5. data/examples/performance.rb +2 -2
  6. data/lib/active_record/aggregations.rb +22 -20
  7. data/lib/active_record/association_relation.rb +22 -12
  8. data/lib/active_record/associations/alias_tracker.rb +41 -30
  9. data/lib/active_record/associations/association.rb +106 -41
  10. data/lib/active_record/associations/association_scope.rb +30 -21
  11. data/lib/active_record/associations/belongs_to_association.rb +69 -14
  12. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +20 -6
  13. data/lib/active_record/associations/builder/association.rb +39 -6
  14. data/lib/active_record/associations/builder/belongs_to.rb +47 -17
  15. data/lib/active_record/associations/builder/collection_association.rb +14 -6
  16. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -10
  17. data/lib/active_record/associations/builder/has_many.rb +7 -3
  18. data/lib/active_record/associations/builder/has_one.rb +13 -16
  19. data/lib/active_record/associations/builder/singular_association.rb +7 -3
  20. data/lib/active_record/associations/collection_association.rb +90 -53
  21. data/lib/active_record/associations/collection_proxy.rb +54 -19
  22. data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
  23. data/lib/active_record/associations/errors.rb +265 -0
  24. data/lib/active_record/associations/foreign_association.rb +21 -1
  25. data/lib/active_record/associations/has_many_association.rb +41 -10
  26. data/lib/active_record/associations/has_many_through_association.rb +29 -12
  27. data/lib/active_record/associations/has_one_association.rb +33 -9
  28. data/lib/active_record/associations/has_one_through_association.rb +1 -1
  29. data/lib/active_record/associations/join_dependency/join_association.rb +41 -17
  30. data/lib/active_record/associations/join_dependency/join_part.rb +3 -3
  31. data/lib/active_record/associations/join_dependency.rb +97 -54
  32. data/lib/active_record/associations/nested_error.rb +47 -0
  33. data/lib/active_record/associations/preloader/association.rb +237 -54
  34. data/lib/active_record/associations/preloader/batch.rb +48 -0
  35. data/lib/active_record/associations/preloader/branch.rb +153 -0
  36. data/lib/active_record/associations/preloader/through_association.rb +51 -17
  37. data/lib/active_record/associations/preloader.rb +55 -121
  38. data/lib/active_record/associations/singular_association.rb +16 -4
  39. data/lib/active_record/associations/through_association.rb +26 -15
  40. data/lib/active_record/associations.rb +454 -440
  41. data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
  42. data/lib/active_record/attribute_assignment.rb +11 -14
  43. data/lib/active_record/attribute_methods/before_type_cast.rb +36 -11
  44. data/lib/active_record/attribute_methods/composite_primary_key.rb +84 -0
  45. data/lib/active_record/attribute_methods/dirty.rb +75 -34
  46. data/lib/active_record/attribute_methods/primary_key.rb +53 -31
  47. data/lib/active_record/attribute_methods/query.rb +31 -22
  48. data/lib/active_record/attribute_methods/read.rb +16 -17
  49. data/lib/active_record/attribute_methods/serialization.rb +177 -35
  50. data/lib/active_record/attribute_methods/time_zone_conversion.rb +18 -15
  51. data/lib/active_record/attribute_methods/write.rb +16 -28
  52. data/lib/active_record/attribute_methods.rb +227 -100
  53. data/lib/active_record/attributes.rb +94 -56
  54. data/lib/active_record/autosave_association.rb +119 -73
  55. data/lib/active_record/base.rb +31 -21
  56. data/lib/active_record/callbacks.rb +168 -55
  57. data/lib/active_record/coders/column_serializer.rb +61 -0
  58. data/lib/active_record/coders/json.rb +1 -1
  59. data/lib/active_record/coders/yaml_column.rb +70 -25
  60. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +284 -0
  61. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +211 -0
  62. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +79 -0
  63. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +367 -565
  64. data/lib/active_record/connection_adapters/abstract/database_limits.rb +3 -57
  65. data/lib/active_record/connection_adapters/abstract/database_statements.rb +277 -89
  66. data/lib/active_record/connection_adapters/abstract/query_cache.rb +241 -69
  67. data/lib/active_record/connection_adapters/abstract/quoting.rb +122 -134
  68. data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
  69. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -116
  70. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +324 -72
  71. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +17 -4
  72. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +611 -211
  73. data/lib/active_record/connection_adapters/abstract/transaction.rb +425 -82
  74. data/lib/active_record/connection_adapters/abstract_adapter.rb +698 -211
  75. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +464 -239
  76. data/lib/active_record/connection_adapters/column.rb +28 -1
  77. data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
  78. data/lib/active_record/connection_adapters/mysql/column.rb +2 -1
  79. data/lib/active_record/connection_adapters/mysql/database_statements.rb +32 -137
  80. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +1 -2
  81. data/lib/active_record/connection_adapters/mysql/quoting.rb +90 -43
  82. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +41 -7
  83. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +18 -1
  84. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +13 -4
  85. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +53 -15
  86. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +10 -1
  87. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +152 -0
  88. data/lib/active_record/connection_adapters/mysql2_adapter.rb +127 -63
  89. data/lib/active_record/connection_adapters/pool_config.rb +83 -0
  90. data/lib/active_record/connection_adapters/pool_manager.rb +57 -0
  91. data/lib/active_record/connection_adapters/postgresql/column.rb +54 -2
  92. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +127 -100
  93. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -2
  94. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +9 -5
  95. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +10 -2
  96. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +15 -2
  97. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
  98. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -15
  99. data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -3
  101. data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +5 -4
  103. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
  104. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -3
  105. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +35 -8
  106. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
  107. data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
  109. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
  110. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -4
  111. data/lib/active_record/connection_adapters/postgresql/oid.rb +4 -0
  112. data/lib/active_record/connection_adapters/postgresql/quoting.rb +139 -106
  113. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -2
  114. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +98 -4
  115. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +176 -4
  116. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +78 -1
  117. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +462 -118
  118. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +8 -0
  119. data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -11
  120. data/lib/active_record/connection_adapters/postgresql_adapter.rb +585 -295
  121. data/lib/active_record/connection_adapters/schema_cache.rb +399 -60
  122. data/lib/active_record/connection_adapters/sql_type_metadata.rb +8 -0
  123. data/lib/active_record/connection_adapters/sqlite3/column.rb +62 -0
  124. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +99 -48
  125. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +80 -54
  126. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +27 -1
  127. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +20 -0
  128. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +16 -0
  129. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +102 -24
  130. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +425 -174
  131. data/lib/active_record/connection_adapters/statement_pool.rb +7 -1
  132. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
  133. data/lib/active_record/connection_adapters/trilogy_adapter.rb +229 -0
  134. data/lib/active_record/connection_adapters.rb +176 -0
  135. data/lib/active_record/connection_handling.rb +243 -115
  136. data/lib/active_record/core.rb +481 -199
  137. data/lib/active_record/counter_cache.rb +69 -32
  138. data/lib/active_record/database_configurations/connection_url_resolver.rb +107 -0
  139. data/lib/active_record/database_configurations/database_config.rb +77 -10
  140. data/lib/active_record/database_configurations/hash_config.rb +148 -26
  141. data/lib/active_record/database_configurations/url_config.rb +44 -45
  142. data/lib/active_record/database_configurations.rb +190 -114
  143. data/lib/active_record/delegated_type.rb +279 -0
  144. data/lib/active_record/deprecator.rb +7 -0
  145. data/lib/active_record/destroy_association_async_job.rb +38 -0
  146. data/lib/active_record/disable_joins_association_relation.rb +39 -0
  147. data/lib/active_record/dynamic_matchers.rb +5 -6
  148. data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
  149. data/lib/active_record/encryption/cipher/aes256_gcm.rb +101 -0
  150. data/lib/active_record/encryption/cipher.rb +53 -0
  151. data/lib/active_record/encryption/config.rb +68 -0
  152. data/lib/active_record/encryption/configurable.rb +60 -0
  153. data/lib/active_record/encryption/context.rb +42 -0
  154. data/lib/active_record/encryption/contexts.rb +76 -0
  155. data/lib/active_record/encryption/derived_secret_key_provider.rb +18 -0
  156. data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
  157. data/lib/active_record/encryption/encryptable_record.rb +230 -0
  158. data/lib/active_record/encryption/encrypted_attribute_type.rb +175 -0
  159. data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
  160. data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
  161. data/lib/active_record/encryption/encryptor.rb +171 -0
  162. data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
  163. data/lib/active_record/encryption/errors.rb +15 -0
  164. data/lib/active_record/encryption/extended_deterministic_queries.rb +157 -0
  165. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
  166. data/lib/active_record/encryption/key.rb +28 -0
  167. data/lib/active_record/encryption/key_generator.rb +53 -0
  168. data/lib/active_record/encryption/key_provider.rb +46 -0
  169. data/lib/active_record/encryption/message.rb +33 -0
  170. data/lib/active_record/encryption/message_pack_message_serializer.rb +76 -0
  171. data/lib/active_record/encryption/message_serializer.rb +96 -0
  172. data/lib/active_record/encryption/null_encryptor.rb +25 -0
  173. data/lib/active_record/encryption/properties.rb +76 -0
  174. data/lib/active_record/encryption/read_only_null_encryptor.rb +28 -0
  175. data/lib/active_record/encryption/scheme.rb +100 -0
  176. data/lib/active_record/encryption.rb +58 -0
  177. data/lib/active_record/enum.rb +224 -73
  178. data/lib/active_record/errors.rb +254 -36
  179. data/lib/active_record/explain.rb +30 -17
  180. data/lib/active_record/explain_registry.rb +11 -6
  181. data/lib/active_record/explain_subscriber.rb +2 -2
  182. data/lib/active_record/fixture_set/file.rb +22 -15
  183. data/lib/active_record/fixture_set/model_metadata.rb +15 -6
  184. data/lib/active_record/fixture_set/render_context.rb +3 -1
  185. data/lib/active_record/fixture_set/table_row.rb +88 -16
  186. data/lib/active_record/fixture_set/table_rows.rb +4 -5
  187. data/lib/active_record/fixtures.rb +229 -116
  188. data/lib/active_record/future_result.rb +178 -0
  189. data/lib/active_record/gem_version.rb +4 -4
  190. data/lib/active_record/inheritance.rb +121 -48
  191. data/lib/active_record/insert_all.rb +178 -29
  192. data/lib/active_record/integration.rb +16 -14
  193. data/lib/active_record/internal_metadata.rb +132 -21
  194. data/lib/active_record/legacy_yaml_adapter.rb +3 -36
  195. data/lib/active_record/locking/optimistic.rb +64 -33
  196. data/lib/active_record/locking/pessimistic.rb +21 -8
  197. data/lib/active_record/log_subscriber.rb +61 -30
  198. data/lib/active_record/marshalling.rb +59 -0
  199. data/lib/active_record/message_pack.rb +124 -0
  200. data/lib/active_record/middleware/database_selector/resolver/session.rb +3 -0
  201. data/lib/active_record/middleware/database_selector/resolver.rb +19 -19
  202. data/lib/active_record/middleware/database_selector.rb +25 -13
  203. data/lib/active_record/middleware/shard_selector.rb +62 -0
  204. data/lib/active_record/migration/command_recorder.rb +160 -55
  205. data/lib/active_record/migration/compatibility.rb +286 -43
  206. data/lib/active_record/migration/default_strategy.rb +22 -0
  207. data/lib/active_record/migration/execution_strategy.rb +19 -0
  208. data/lib/active_record/migration/join_table.rb +1 -2
  209. data/lib/active_record/migration/pending_migration_connection.rb +21 -0
  210. data/lib/active_record/migration.rb +421 -193
  211. data/lib/active_record/model_schema.rb +217 -125
  212. data/lib/active_record/nested_attributes.rb +62 -27
  213. data/lib/active_record/no_touching.rb +4 -4
  214. data/lib/active_record/normalization.rb +163 -0
  215. data/lib/active_record/persistence.rb +322 -319
  216. data/lib/active_record/promise.rb +84 -0
  217. data/lib/active_record/query_cache.rb +18 -15
  218. data/lib/active_record/query_logs.rb +193 -0
  219. data/lib/active_record/query_logs_formatter.rb +41 -0
  220. data/lib/active_record/querying.rb +54 -14
  221. data/lib/active_record/railtie.rb +250 -72
  222. data/lib/active_record/railties/console_sandbox.rb +2 -4
  223. data/lib/active_record/railties/controller_runtime.rb +25 -11
  224. data/lib/active_record/railties/databases.rake +312 -197
  225. data/lib/active_record/railties/job_runtime.rb +23 -0
  226. data/lib/active_record/readonly_attributes.rb +45 -3
  227. data/lib/active_record/reflection.rb +389 -146
  228. data/lib/active_record/relation/batches/batch_enumerator.rb +61 -16
  229. data/lib/active_record/relation/batches.rb +214 -73
  230. data/lib/active_record/relation/calculations.rb +379 -124
  231. data/lib/active_record/relation/delegation.rb +36 -23
  232. data/lib/active_record/relation/finder_methods.rb +159 -49
  233. data/lib/active_record/relation/from_clause.rb +5 -1
  234. data/lib/active_record/relation/merger.rb +41 -33
  235. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -11
  236. data/lib/active_record/relation/predicate_builder/association_query_value.rb +42 -7
  237. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +20 -13
  238. data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
  239. data/lib/active_record/relation/predicate_builder.rb +79 -53
  240. data/lib/active_record/relation/query_attribute.rb +30 -12
  241. data/lib/active_record/relation/query_methods.rb +1156 -279
  242. data/lib/active_record/relation/record_fetch_warning.rb +12 -11
  243. data/lib/active_record/relation/spawn_methods.rb +10 -9
  244. data/lib/active_record/relation/where_clause.rb +100 -66
  245. data/lib/active_record/relation.rb +829 -194
  246. data/lib/active_record/result.rb +76 -56
  247. data/lib/active_record/runtime_registry.rb +71 -13
  248. data/lib/active_record/sanitization.rb +86 -47
  249. data/lib/active_record/schema.rb +39 -23
  250. data/lib/active_record/schema_dumper.rb +140 -33
  251. data/lib/active_record/schema_migration.rb +74 -29
  252. data/lib/active_record/scoping/default.rb +73 -19
  253. data/lib/active_record/scoping/named.rb +10 -28
  254. data/lib/active_record/scoping.rb +65 -35
  255. data/lib/active_record/secure_password.rb +60 -0
  256. data/lib/active_record/secure_token.rb +34 -8
  257. data/lib/active_record/serialization.rb +11 -4
  258. data/lib/active_record/signed_id.rb +138 -0
  259. data/lib/active_record/statement_cache.rb +26 -10
  260. data/lib/active_record/store.rb +19 -14
  261. data/lib/active_record/suppressor.rb +15 -17
  262. data/lib/active_record/table_metadata.rb +46 -36
  263. data/lib/active_record/tasks/database_tasks.rb +371 -205
  264. data/lib/active_record/tasks/mysql_database_tasks.rb +43 -36
  265. data/lib/active_record/tasks/postgresql_database_tasks.rb +54 -41
  266. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -13
  267. data/lib/active_record/test_databases.rb +5 -4
  268. data/lib/active_record/test_fixtures.rb +189 -104
  269. data/lib/active_record/testing/query_assertions.rb +121 -0
  270. data/lib/active_record/timestamp.rb +35 -25
  271. data/lib/active_record/token_for.rb +123 -0
  272. data/lib/active_record/touch_later.rb +31 -27
  273. data/lib/active_record/transaction.rb +132 -0
  274. data/lib/active_record/transactions.rb +131 -99
  275. data/lib/active_record/translation.rb +3 -5
  276. data/lib/active_record/type/adapter_specific_registry.rb +33 -18
  277. data/lib/active_record/type/hash_lookup_type_map.rb +34 -2
  278. data/lib/active_record/type/internal/timezone.rb +7 -2
  279. data/lib/active_record/type/serialized.rb +11 -6
  280. data/lib/active_record/type/time.rb +14 -0
  281. data/lib/active_record/type/type_map.rb +17 -21
  282. data/lib/active_record/type/unsigned_integer.rb +0 -1
  283. data/lib/active_record/type.rb +7 -2
  284. data/lib/active_record/type_caster/connection.rb +4 -5
  285. data/lib/active_record/type_caster/map.rb +8 -5
  286. data/lib/active_record/validations/absence.rb +1 -1
  287. data/lib/active_record/validations/associated.rb +13 -8
  288. data/lib/active_record/validations/numericality.rb +36 -0
  289. data/lib/active_record/validations/presence.rb +5 -28
  290. data/lib/active_record/validations/uniqueness.rb +88 -18
  291. data/lib/active_record/validations.rb +15 -8
  292. data/lib/active_record/version.rb +1 -1
  293. data/lib/active_record.rb +446 -40
  294. data/lib/arel/alias_predication.rb +1 -1
  295. data/lib/arel/attributes/attribute.rb +4 -8
  296. data/lib/arel/collectors/bind.rb +8 -1
  297. data/lib/arel/collectors/composite.rb +15 -0
  298. data/lib/arel/collectors/sql_string.rb +7 -0
  299. data/lib/arel/collectors/substitute_binds.rb +7 -0
  300. data/lib/arel/crud.rb +30 -22
  301. data/lib/arel/delete_manager.rb +23 -4
  302. data/lib/arel/errors.rb +10 -0
  303. data/lib/arel/factory_methods.rb +4 -0
  304. data/lib/arel/filter_predications.rb +9 -0
  305. data/lib/arel/insert_manager.rb +2 -3
  306. data/lib/arel/nodes/binary.rb +82 -9
  307. data/lib/arel/nodes/bind_param.rb +8 -0
  308. data/lib/arel/nodes/bound_sql_literal.rb +65 -0
  309. data/lib/arel/nodes/casted.rb +22 -10
  310. data/lib/arel/nodes/cte.rb +36 -0
  311. data/lib/arel/nodes/delete_statement.rb +14 -13
  312. data/lib/arel/nodes/equality.rb +6 -9
  313. data/lib/arel/nodes/filter.rb +10 -0
  314. data/lib/arel/nodes/fragments.rb +35 -0
  315. data/lib/arel/nodes/function.rb +1 -0
  316. data/lib/arel/nodes/grouping.rb +3 -0
  317. data/lib/arel/nodes/homogeneous_in.rb +68 -0
  318. data/lib/arel/nodes/in.rb +8 -1
  319. data/lib/arel/nodes/infix_operation.rb +13 -1
  320. data/lib/arel/nodes/insert_statement.rb +2 -2
  321. data/lib/arel/nodes/join_source.rb +1 -1
  322. data/lib/arel/nodes/leading_join.rb +8 -0
  323. data/lib/arel/nodes/{and.rb → nary.rb} +9 -2
  324. data/lib/arel/nodes/node.rb +122 -11
  325. data/lib/arel/nodes/ordering.rb +27 -0
  326. data/lib/arel/nodes/select_core.rb +2 -2
  327. data/lib/arel/nodes/select_statement.rb +2 -2
  328. data/lib/arel/nodes/sql_literal.rb +16 -0
  329. data/lib/arel/nodes/table_alias.rb +11 -3
  330. data/lib/arel/nodes/unary.rb +0 -1
  331. data/lib/arel/nodes/update_statement.rb +11 -4
  332. data/lib/arel/nodes.rb +10 -3
  333. data/lib/arel/predications.rb +31 -28
  334. data/lib/arel/select_manager.rb +18 -9
  335. data/lib/arel/table.rb +21 -10
  336. data/lib/arel/tree_manager.rb +8 -15
  337. data/lib/arel/update_manager.rb +25 -5
  338. data/lib/arel/visitors/dot.rb +94 -90
  339. data/lib/arel/visitors/mysql.rb +34 -6
  340. data/lib/arel/visitors/postgresql.rb +5 -16
  341. data/lib/arel/visitors/sqlite.rb +25 -1
  342. data/lib/arel/visitors/to_sql.rb +227 -81
  343. data/lib/arel/visitors/visitor.rb +2 -3
  344. data/lib/arel/visitors.rb +0 -7
  345. data/lib/arel.rb +37 -15
  346. data/lib/rails/generators/active_record/application_record/USAGE +8 -0
  347. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
  348. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
  349. data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -0
  350. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +6 -1
  351. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +4 -4
  352. data/lib/rails/generators/active_record/migration.rb +9 -3
  353. data/lib/rails/generators/active_record/model/USAGE +113 -0
  354. data/lib/rails/generators/active_record/model/model_generator.rb +49 -4
  355. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
  356. data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
  357. data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
  358. data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
  359. data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
  360. metadata +117 -30
  361. data/lib/active_record/attribute_decorators.rb +0 -90
  362. data/lib/active_record/connection_adapters/connection_specification.rb +0 -297
  363. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -29
  364. data/lib/active_record/define_callbacks.rb +0 -22
  365. data/lib/active_record/null_relation.rb +0 -68
  366. data/lib/active_record/railties/collection_cache_association_loading.rb +0 -34
  367. data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -18
  368. data/lib/active_record/relation/where_clause_factory.rb +0 -33
  369. data/lib/arel/attributes.rb +0 -22
  370. data/lib/arel/visitors/depth_first.rb +0 -204
  371. data/lib/arel/visitors/ibm_db.rb +0 -34
  372. data/lib/arel/visitors/informix.rb +0 -62
  373. data/lib/arel/visitors/mssql.rb +0 -157
  374. data/lib/arel/visitors/oracle.rb +0 -159
  375. data/lib/arel/visitors/oracle12.rb +0 -66
  376. data/lib/arel/visitors/where_sql.rb +0 -23
@@ -1,22 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "yaml"
4
3
  require "active_support/benchmarkable"
5
4
  require "active_support/dependencies"
6
5
  require "active_support/descendants_tracker"
7
6
  require "active_support/time"
8
- require "active_support/core_ext/module/attribute_accessors"
9
- require "active_support/core_ext/array/extract_options"
10
- require "active_support/core_ext/hash/deep_merge"
11
- require "active_support/core_ext/hash/slice"
12
- require "active_support/core_ext/string/behavior"
13
- require "active_support/core_ext/kernel/singleton_class"
14
- require "active_support/core_ext/module/introspection"
15
- require "active_support/core_ext/object/duplicable"
16
7
  require "active_support/core_ext/class/subclasses"
17
- require "active_record/attribute_decorators"
18
- require "active_record/define_callbacks"
19
- require "active_record/errors"
20
8
  require "active_record/log_subscriber"
21
9
  require "active_record/explain_subscriber"
22
10
  require "active_record/relation/delegation"
@@ -24,7 +12,7 @@ require "active_record/attributes"
24
12
  require "active_record/type_caster"
25
13
  require "active_record/database_configurations"
26
14
 
27
- module ActiveRecord #:nodoc:
15
+ module ActiveRecord # :nodoc:
28
16
  # = Active Record
29
17
  #
30
18
  # Active Record objects don't specify their attributes directly, but rather infer them from
@@ -149,6 +137,23 @@ module ActiveRecord #:nodoc:
149
137
  # anonymous = User.new(name: "")
150
138
  # anonymous.name? # => false
151
139
  #
140
+ # Query methods will also respect any overrides of default accessors:
141
+ #
142
+ # class User
143
+ # # Has admin boolean column
144
+ # def admin
145
+ # false
146
+ # end
147
+ # end
148
+ #
149
+ # user.update(admin: true)
150
+ #
151
+ # user.read_attribute(:admin) # => true, gets the column value
152
+ # user[:admin] # => true, also gets the column value
153
+ #
154
+ # user.admin # => false, due to the getter override
155
+ # user.admin? # => false, due to the getter override
156
+ #
152
157
  # == Accessing attributes before they have been typecasted
153
158
  #
154
159
  # Sometimes you want to be able to read the raw attribute data without having the column-determined
@@ -228,7 +233,7 @@ module ActiveRecord #:nodoc:
228
233
  #
229
234
  # Connections are usually created through
230
235
  # {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] and retrieved
231
- # by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this
236
+ # by ActiveRecord::Base.lease_connection. All classes inheriting from ActiveRecord::Base will use this
232
237
  # connection. But you can also set a class-specific connection. For example, if Course is an
233
238
  # ActiveRecord::Base, but resides in a different database, you can just say <tt>Course.establish_connection</tt>
234
239
  # and Course and all of its subclasses will use this connection instead.
@@ -251,13 +256,13 @@ module ActiveRecord #:nodoc:
251
256
  # * AssociationTypeMismatch - The object assigned to the association wasn't of the type
252
257
  # specified in the association definition.
253
258
  # * AttributeAssignmentError - An error occurred while doing a mass assignment through the
254
- # {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] method.
259
+ # {ActiveRecord::Base#attributes=}[rdoc-ref:ActiveModel::AttributeAssignment#attributes=] method.
255
260
  # You can inspect the +attribute+ property of the exception object to determine which attribute
256
261
  # triggered the error.
257
262
  # * ConnectionNotEstablished - No connection has been established.
258
263
  # Use {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] before querying.
259
264
  # * MultiparameterAssignmentErrors - Collection of errors that occurred during a mass assignment using the
260
- # {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] method.
265
+ # {ActiveRecord::Base#attributes=}[rdoc-ref:ActiveModel::AttributeAssignment#attributes=] method.
261
266
  # The +errors+ property of this exception contains an array of
262
267
  # AttributeAssignmentError
263
268
  # objects that should be inspected to determine which attributes triggered the errors.
@@ -275,7 +280,7 @@ module ActiveRecord #:nodoc:
275
280
  # So it's possible to assign a logger to the class through <tt>Base.logger=</tt> which will then be used by all
276
281
  # instances in the current object space.
277
282
  class Base
278
- extend ActiveModel::Naming
283
+ include ActiveModel::API
279
284
 
280
285
  extend ActiveSupport::Benchmarkable
281
286
  extend ActiveSupport::DescendantsTracker
@@ -285,6 +290,7 @@ module ActiveRecord #:nodoc:
285
290
  extend Querying
286
291
  extend Translation
287
292
  extend DynamicMatchers
293
+ extend DelegatedType
288
294
  extend Explain
289
295
  extend Enum
290
296
  extend Delegation::DelegateCache
@@ -298,20 +304,18 @@ module ActiveRecord #:nodoc:
298
304
  include Scoping
299
305
  include Sanitization
300
306
  include AttributeAssignment
301
- include ActiveModel::Conversion
302
307
  include Integration
303
308
  include Validations
304
309
  include CounterCache
305
310
  include Attributes
306
- include AttributeDecorators
307
311
  include Locking::Optimistic
308
312
  include Locking::Pessimistic
309
- include DefineCallbacks
313
+ include Encryption::EncryptableRecord
310
314
  include AttributeMethods
311
315
  include Callbacks
312
316
  include Timestamp
313
317
  include Associations
314
- include ActiveModel::SecurePassword
318
+ include SecurePassword
315
319
  include AutosaveAssociation
316
320
  include NestedAttributes
317
321
  include Transactions
@@ -321,7 +325,13 @@ module ActiveRecord #:nodoc:
321
325
  include Serialization
322
326
  include Store
323
327
  include SecureToken
328
+ include TokenFor
329
+ include SignedId
324
330
  include Suppressor
331
+ include Normalization
332
+ include Marshalling::Methods
333
+
334
+ self.param_delimiter = "_"
325
335
  end
326
336
 
327
337
  ActiveSupport.run_load_hooks(:active_record, Base)
@@ -4,7 +4,7 @@ module ActiveRecord
4
4
  # = Active Record \Callbacks
5
5
  #
6
6
  # \Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic
7
- # before or after an alteration of the object state. This can be used to make sure that associated and
7
+ # before or after a change in the object state. This can be used to make sure that associated and
8
8
  # dependent objects are deleted when {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] is called (by overwriting +before_destroy+) or
9
9
  # to massage attributes before they're validated (by overwriting +before_validation+).
10
10
  # As an example of the callbacks initiated, consider the {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] call for a new record:
@@ -32,7 +32,7 @@ module ActiveRecord
32
32
  # is found and instantiated by a finder, with <tt>after_initialize</tt> being triggered after new objects
33
33
  # are instantiated as well.
34
34
  #
35
- # There are nineteen callbacks in total, which give you immense power to react and prepare for each state in the
35
+ # There are nineteen callbacks in total, which give a lot of control over how to react and prepare for each state in the
36
36
  # Active Record life cycle. The sequence for calling {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] for an existing record is similar,
37
37
  # except that each <tt>_create</tt> callback is replaced by the corresponding <tt>_update</tt> callback.
38
38
  #
@@ -64,7 +64,7 @@ module ActiveRecord
64
64
  #
65
65
  # Besides the overwritable callback methods, it's also possible to register callbacks through the
66
66
  # use of the callback macros. Their main advantage is that the macros add behavior into a callback
67
- # queue that is kept intact down through an inheritance hierarchy.
67
+ # queue that is kept intact through an inheritance hierarchy.
68
68
  #
69
69
  # class Topic < ActiveRecord::Base
70
70
  # before_destroy :destroy_author
@@ -74,7 +74,7 @@ module ActiveRecord
74
74
  # before_destroy :destroy_readers
75
75
  # end
76
76
  #
77
- # Now, when <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is
77
+ # When <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is
78
78
  # run, both +destroy_author+ and +destroy_readers+ are called.
79
79
  #
80
80
  # *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the
@@ -83,10 +83,9 @@ module ActiveRecord
83
83
  #
84
84
  # == Types of callbacks
85
85
  #
86
- # There are four types of callbacks accepted by the callback macros: Method references (symbol), callback objects,
87
- # inline methods (using a proc). Method references and callback objects
88
- # are the recommended approaches, inline methods using a proc are sometimes appropriate (such as for
89
- # creating mix-ins).
86
+ # There are three types of callbacks accepted by the callback macros: method references (symbol), callback objects,
87
+ # inline methods (using a proc). \Method references and callback objects are the recommended approaches,
88
+ # inline methods using a proc are sometimes appropriate (such as for creating mix-ins).
90
89
  #
91
90
  # The method reference callbacks work by specifying a protected or private method available in the object, like this:
92
91
  #
@@ -174,13 +173,13 @@ module ActiveRecord
174
173
  #
175
174
  # If a <tt>before_*</tt> callback throws +:abort+, all the later callbacks and
176
175
  # the associated action are cancelled.
177
- # Callbacks are generally run in the order they are defined, with the exception of callbacks defined as
176
+ # \Callbacks are generally run in the order they are defined, with the exception of callbacks defined as
178
177
  # methods on the model, which are called last.
179
178
  #
180
179
  # == Ordering callbacks
181
180
  #
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
181
+ # Sometimes application code requires that callbacks execute in a specific order. For example, a +before_destroy+
182
+ # callback (+log_children+ in this case) should be executed before records in the +children+ association are destroyed by the
184
183
  # <tt>dependent: :destroy</tt> option.
185
184
  #
186
185
  # Let's look at the code below:
@@ -196,8 +195,8 @@ module ActiveRecord
196
195
  # end
197
196
  # end
198
197
  #
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.
198
+ # In this case, the problem is that when the +before_destroy+ callback is executed, records in the +children+ association no
199
+ # longer exist because the {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] callback was executed first.
201
200
  # You can use the +prepend+ option on the +before_destroy+ callback to avoid this.
202
201
  #
203
202
  # class Topic < ActiveRecord::Base
@@ -211,7 +210,7 @@ module ActiveRecord
211
210
  # end
212
211
  # end
213
212
  #
214
- # This way, the +before_destroy+ gets executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
213
+ # This way, the +before_destroy+ is executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
215
214
  #
216
215
  # Also, there are cases when you want several callbacks of the same type to
217
216
  # be executed in order.
@@ -225,51 +224,35 @@ module ActiveRecord
225
224
  # after_save :do_something_else
226
225
  #
227
226
  # private
227
+ # def log_children
228
+ # # Child processing
229
+ # end
228
230
  #
229
- # def log_children
230
- # # Child processing
231
- # end
232
- #
233
- # def do_something_else
234
- # # Something else
235
- # end
231
+ # def do_something_else
232
+ # # Something else
233
+ # end
236
234
  # end
237
235
  #
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
236
+ # In this case the +log_children+ is executed before +do_something_else+.
237
+ # This applies to all non-transactional callbacks, and to +before_commit+.
253
238
  #
254
- # def log_children
255
- # # Child processing
256
- # end
239
+ # For transactional +after_+ callbacks (+after_commit+, +after_rollback+, etc), the order
240
+ # can be set via configuration.
257
241
  #
258
- # def do_something_else
259
- # # Something else
260
- # end
261
- # end
242
+ # config.active_record.run_after_transaction_callbacks_in_order_defined = false
262
243
  #
263
- # In this case the +do_something_else+ gets executed before +log_children+.
244
+ # When set to +true+ (the default from \Rails 7.1), callbacks are executed in the order they
245
+ # are defined, just like the example above. When set to +false+, the order is reversed, so
246
+ # +do_something_else+ is executed before +log_children+.
264
247
  #
265
248
  # == \Transactions
266
249
  #
267
250
  # The entire callback chain of a {#save}[rdoc-ref:Persistence#save], {#save!}[rdoc-ref:Persistence#save!],
268
251
  # 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.
252
+ # If everything goes fine a +COMMIT+ is executed once the chain has been completed.
270
253
  #
271
- # If a <tt>before_*</tt> callback cancels the action a ROLLBACK is issued. You
272
- # can also trigger a ROLLBACK raising an exception in any of the callbacks,
254
+ # If a <tt>before_*</tt> callback cancels the action a +ROLLBACK+ is issued. You
255
+ # can also trigger a +ROLLBACK+ raising an exception in any of the callbacks,
273
256
  # including <tt>after_*</tt> hooks. Note, however, that in that case the client
274
257
  # needs to be aware of it because an ordinary {#save}[rdoc-ref:Persistence#save] will raise such exception
275
258
  # instead of quietly returning +false+.
@@ -280,17 +263,17 @@ module ActiveRecord
280
263
  # <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt> property
281
264
  # defines what part of the chain the callback runs in.
282
265
  #
283
- # To find all callbacks in the before_save callback chain:
266
+ # To find all callbacks in the +before_save+ callback chain:
284
267
  #
285
268
  # Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }
286
269
  #
287
- # Returns an array of callback objects that form the before_save chain.
270
+ # Returns an array of callback objects that form the +before_save+ chain.
288
271
  #
289
272
  # 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:
290
273
  #
291
274
  # Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).include?(:rest_when_dead)
292
275
  #
293
- # Returns true or false depending on whether the proc is contained in the before_save callback chain on a Topic model.
276
+ # Returns true or false depending on whether the proc is contained in the +before_save+ callback chain on a Topic model.
294
277
  #
295
278
  module Callbacks
296
279
  extend ActiveSupport::Concern
@@ -302,9 +285,140 @@ module ActiveRecord
302
285
  :before_destroy, :around_destroy, :after_destroy, :after_commit, :after_rollback
303
286
  ]
304
287
 
305
- def destroy #:nodoc:
288
+ module ClassMethods
289
+ include ActiveModel::Callbacks
290
+
291
+ ##
292
+ # :method: after_initialize
293
+ #
294
+ # :call-seq: after_initialize(*args, &block)
295
+ #
296
+ # Registers a callback to be called after a record is instantiated. See
297
+ # ActiveRecord::Callbacks for more information.
298
+
299
+ ##
300
+ # :method: after_find
301
+ #
302
+ # :call-seq: after_find(*args, &block)
303
+ #
304
+ # Registers a callback to be called after a record is instantiated
305
+ # via a finder. See ActiveRecord::Callbacks for more information.
306
+
307
+ ##
308
+ # :method: after_touch
309
+ #
310
+ # :call-seq: after_touch(*args, &block)
311
+ #
312
+ # Registers a callback to be called after a record is touched. See
313
+ # ActiveRecord::Callbacks for more information.
314
+
315
+ ##
316
+ # :method: before_save
317
+ #
318
+ # :call-seq: before_save(*args, &block)
319
+ #
320
+ # Registers a callback to be called before a record is saved. See
321
+ # ActiveRecord::Callbacks for more information.
322
+
323
+ ##
324
+ # :method: around_save
325
+ #
326
+ # :call-seq: around_save(*args, &block)
327
+ #
328
+ # Registers a callback to be called around the save of a record. See
329
+ # ActiveRecord::Callbacks for more information.
330
+
331
+ ##
332
+ # :method: after_save
333
+ #
334
+ # :call-seq: after_save(*args, &block)
335
+ #
336
+ # Registers a callback to be called after a record is saved. See
337
+ # ActiveRecord::Callbacks for more information.
338
+
339
+ ##
340
+ # :method: before_create
341
+ #
342
+ # :call-seq: before_create(*args, &block)
343
+ #
344
+ # Registers a callback to be called before a record is created. See
345
+ # ActiveRecord::Callbacks for more information.
346
+
347
+ ##
348
+ # :method: around_create
349
+ #
350
+ # :call-seq: around_create(*args, &block)
351
+ #
352
+ # Registers a callback to be called around the creation of a record. See
353
+ # ActiveRecord::Callbacks for more information.
354
+
355
+ ##
356
+ # :method: after_create
357
+ #
358
+ # :call-seq: after_create(*args, &block)
359
+ #
360
+ # Registers a callback to be called after a record is created. See
361
+ # ActiveRecord::Callbacks for more information.
362
+
363
+ ##
364
+ # :method: before_update
365
+ #
366
+ # :call-seq: before_update(*args, &block)
367
+ #
368
+ # Registers a callback to be called before a record is updated. See
369
+ # ActiveRecord::Callbacks for more information.
370
+
371
+ ##
372
+ # :method: around_update
373
+ #
374
+ # :call-seq: around_update(*args, &block)
375
+ #
376
+ # Registers a callback to be called around the update of a record. See
377
+ # ActiveRecord::Callbacks for more information.
378
+
379
+ ##
380
+ # :method: after_update
381
+ #
382
+ # :call-seq: after_update(*args, &block)
383
+ #
384
+ # Registers a callback to be called after a record is updated. See
385
+ # ActiveRecord::Callbacks for more information.
386
+
387
+ ##
388
+ # :method: before_destroy
389
+ #
390
+ # :call-seq: before_destroy(*args, &block)
391
+ #
392
+ # Registers a callback to be called before a record is destroyed. See
393
+ # ActiveRecord::Callbacks for more information.
394
+
395
+ ##
396
+ # :method: around_destroy
397
+ #
398
+ # :call-seq: around_destroy(*args, &block)
399
+ #
400
+ # Registers a callback to be called around the destruction of a record.
401
+ # See ActiveRecord::Callbacks for more information.
402
+
403
+ ##
404
+ # :method: after_destroy
405
+ #
406
+ # :call-seq: after_destroy(*args, &block)
407
+ #
408
+ # Registers a callback to be called after a record is destroyed. See
409
+ # ActiveRecord::Callbacks for more information.
410
+ end
411
+
412
+ included do
413
+ include ActiveModel::Validations::Callbacks
414
+
415
+ define_model_callbacks :initialize, :find, :touch, only: :after
416
+ define_model_callbacks :save, :create, :update, :destroy
417
+ end
418
+
419
+ def destroy # :nodoc:
306
420
  @_destroy_callback_already_called ||= false
307
- return if @_destroy_callback_already_called
421
+ return true if @_destroy_callback_already_called
308
422
  @_destroy_callback_already_called = true
309
423
  _run_destroy_callbacks { super }
310
424
  rescue RecordNotDestroyed => e
@@ -314,7 +428,7 @@ module ActiveRecord
314
428
  @_destroy_callback_already_called = false
315
429
  end
316
430
 
317
- def touch(*) #:nodoc:
431
+ def touch(*, **) # :nodoc:
318
432
  _run_touch_callbacks { super }
319
433
  end
320
434
 
@@ -323,7 +437,6 @@ module ActiveRecord
323
437
  end
324
438
 
325
439
  private
326
-
327
440
  def create_or_update(**)
328
441
  _run_save_callbacks { super }
329
442
  end
@@ -333,7 +446,7 @@ module ActiveRecord
333
446
  end
334
447
 
335
448
  def _update_record
336
- _run_update_callbacks { super }
449
+ _run_update_callbacks { record_update_timestamps { super } }
337
450
  end
338
451
  end
339
452
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Coders # :nodoc:
5
+ class ColumnSerializer # :nodoc:
6
+ attr_reader :object_class
7
+ attr_reader :coder
8
+
9
+ def initialize(attr_name, coder, object_class = Object)
10
+ @attr_name = attr_name
11
+ @object_class = object_class
12
+ @coder = coder
13
+ check_arity_of_constructor
14
+ end
15
+
16
+ def init_with(coder) # :nodoc:
17
+ @attr_name = coder["attr_name"]
18
+ @object_class = coder["object_class"]
19
+ @coder = coder["coder"]
20
+ end
21
+
22
+ def dump(object)
23
+ return if object.nil?
24
+
25
+ assert_valid_value(object, action: "dump")
26
+ coder.dump(object)
27
+ end
28
+
29
+ def load(payload)
30
+ if payload.nil?
31
+ if @object_class != ::Object
32
+ return @object_class.new
33
+ end
34
+ return nil
35
+ end
36
+
37
+ object = coder.load(payload)
38
+
39
+ assert_valid_value(object, action: "load")
40
+ object ||= object_class.new if object_class != Object
41
+
42
+ object
43
+ end
44
+
45
+ # Public because it's called by Type::Serialized
46
+ def assert_valid_value(object, action:)
47
+ unless object.nil? || object_class === object
48
+ raise SerializationTypeMismatch,
49
+ "can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{object.class}. -- #{object.inspect}"
50
+ end
51
+ end
52
+
53
+ private
54
+ def check_arity_of_constructor
55
+ load(nil)
56
+ rescue ArgumentError
57
+ raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
58
+ end
59
+ end
60
+ end
61
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Coders # :nodoc:
5
- class JSON # :nodoc:
5
+ module JSON # :nodoc:
6
6
  def self.dump(obj)
7
7
  ActiveSupport::JSON.encode(obj)
8
8
  end
@@ -4,42 +4,87 @@ require "yaml"
4
4
 
5
5
  module ActiveRecord
6
6
  module Coders # :nodoc:
7
- class YAMLColumn # :nodoc:
8
- attr_accessor :object_class
9
-
10
- def initialize(attr_name, object_class = Object)
11
- @attr_name = attr_name
12
- @object_class = object_class
13
- check_arity_of_constructor
14
- end
7
+ class YAMLColumn < ColumnSerializer # :nodoc:
8
+ class SafeCoder
9
+ def initialize(permitted_classes: [], unsafe_load: nil)
10
+ @permitted_classes = permitted_classes
11
+ @unsafe_load = unsafe_load
12
+ end
15
13
 
16
- def dump(obj)
17
- return if obj.nil?
14
+ if Gem::Version.new(Psych::VERSION) >= Gem::Version.new("5.1")
15
+ def dump(object)
16
+ if @unsafe_load.nil? ? ActiveRecord.use_yaml_unsafe_load : @unsafe_load
17
+ ::YAML.dump(object)
18
+ else
19
+ ::YAML.safe_dump(
20
+ object,
21
+ permitted_classes: @permitted_classes + ActiveRecord.yaml_column_permitted_classes,
22
+ aliases: true,
23
+ )
24
+ end
25
+ end
26
+ else
27
+ def dump(object)
28
+ YAML.dump(object)
29
+ end
30
+ end
18
31
 
19
- assert_valid_value(obj, action: "dump")
20
- YAML.dump obj
32
+ if YAML.respond_to?(:unsafe_load)
33
+ def load(payload)
34
+ if @unsafe_load.nil? ? ActiveRecord.use_yaml_unsafe_load : @unsafe_load
35
+ YAML.unsafe_load(payload)
36
+ else
37
+ YAML.safe_load(
38
+ payload,
39
+ permitted_classes: @permitted_classes + ActiveRecord.yaml_column_permitted_classes,
40
+ aliases: true,
41
+ )
42
+ end
43
+ end
44
+ else
45
+ def load(payload)
46
+ if @unsafe_load.nil? ? ActiveRecord.use_yaml_unsafe_load : @unsafe_load
47
+ YAML.load(payload)
48
+ else
49
+ YAML.safe_load(
50
+ payload,
51
+ permitted_classes: @permitted_classes + ActiveRecord.yaml_column_permitted_classes,
52
+ aliases: true,
53
+ )
54
+ end
55
+ end
56
+ end
21
57
  end
22
58
 
23
- def load(yaml)
24
- return object_class.new if object_class != Object && yaml.nil?
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
59
+ def initialize(attr_name, object_class = Object, permitted_classes: [], unsafe_load: nil)
60
+ super(
61
+ attr_name,
62
+ SafeCoder.new(permitted_classes: permitted_classes || [], unsafe_load: unsafe_load),
63
+ object_class,
64
+ )
65
+ check_arity_of_constructor
66
+ end
30
67
 
31
- obj
68
+ def init_with(coder) # :nodoc:
69
+ unless coder["coder"]
70
+ permitted_classes = coder["permitted_classes"] || []
71
+ unsafe_load = coder["unsafe_load"] || false
72
+ coder["coder"] = SafeCoder.new(permitted_classes: permitted_classes, unsafe_load: unsafe_load)
73
+ end
74
+ super(coder)
32
75
  end
33
76
 
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}"
77
+ def coder
78
+ # This is to retain forward compatibility when loading records serialized with Marshal
79
+ # from a previous version of Rails.
80
+ @coder ||= begin
81
+ permitted_classes = defined?(@permitted_classes) ? @permitted_classes : []
82
+ unsafe_load = defined?(@unsafe_load) && @unsafe_load.nil?
83
+ SafeCoder.new(permitted_classes: permitted_classes, unsafe_load: unsafe_load)
38
84
  end
39
85
  end
40
86
 
41
87
  private
42
-
43
88
  def check_arity_of_constructor
44
89
  load(nil)
45
90
  rescue ArgumentError