activerecord 5.2.8 → 7.0.2

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 (364) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1393 -587
  3. data/MIT-LICENSE +3 -1
  4. data/README.rdoc +7 -5
  5. data/examples/performance.rb +1 -1
  6. data/lib/active_record/aggregations.rb +10 -9
  7. data/lib/active_record/association_relation.rb +22 -12
  8. data/lib/active_record/associations/alias_tracker.rb +19 -16
  9. data/lib/active_record/associations/association.rb +122 -47
  10. data/lib/active_record/associations/association_scope.rb +24 -24
  11. data/lib/active_record/associations/belongs_to_association.rb +67 -49
  12. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +16 -7
  13. data/lib/active_record/associations/builder/association.rb +52 -23
  14. data/lib/active_record/associations/builder/belongs_to.rb +44 -61
  15. data/lib/active_record/associations/builder/collection_association.rb +17 -19
  16. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +17 -41
  17. data/lib/active_record/associations/builder/has_many.rb +10 -3
  18. data/lib/active_record/associations/builder/has_one.rb +35 -3
  19. data/lib/active_record/associations/builder/singular_association.rb +5 -3
  20. data/lib/active_record/associations/collection_association.rb +59 -50
  21. data/lib/active_record/associations/collection_proxy.rb +32 -23
  22. data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
  23. data/lib/active_record/associations/foreign_association.rb +20 -0
  24. data/lib/active_record/associations/has_many_association.rb +27 -14
  25. data/lib/active_record/associations/has_many_through_association.rb +26 -19
  26. data/lib/active_record/associations/has_one_association.rb +52 -37
  27. data/lib/active_record/associations/has_one_through_association.rb +6 -6
  28. data/lib/active_record/associations/join_dependency/join_association.rb +44 -22
  29. data/lib/active_record/associations/join_dependency/join_part.rb +5 -5
  30. data/lib/active_record/associations/join_dependency.rb +97 -62
  31. data/lib/active_record/associations/preloader/association.rb +220 -60
  32. data/lib/active_record/associations/preloader/batch.rb +48 -0
  33. data/lib/active_record/associations/preloader/branch.rb +147 -0
  34. data/lib/active_record/associations/preloader/through_association.rb +85 -40
  35. data/lib/active_record/associations/preloader.rb +44 -105
  36. data/lib/active_record/associations/singular_association.rb +9 -17
  37. data/lib/active_record/associations/through_association.rb +4 -4
  38. data/lib/active_record/associations.rb +207 -66
  39. data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
  40. data/lib/active_record/attribute_assignment.rb +17 -19
  41. data/lib/active_record/attribute_methods/before_type_cast.rb +19 -8
  42. data/lib/active_record/attribute_methods/dirty.rb +141 -47
  43. data/lib/active_record/attribute_methods/primary_key.rb +22 -27
  44. data/lib/active_record/attribute_methods/query.rb +6 -10
  45. data/lib/active_record/attribute_methods/read.rb +15 -55
  46. data/lib/active_record/attribute_methods/serialization.rb +77 -18
  47. data/lib/active_record/attribute_methods/time_zone_conversion.rb +16 -18
  48. data/lib/active_record/attribute_methods/write.rb +18 -37
  49. data/lib/active_record/attribute_methods.rb +90 -153
  50. data/lib/active_record/attributes.rb +38 -12
  51. data/lib/active_record/autosave_association.rb +50 -50
  52. data/lib/active_record/base.rb +23 -18
  53. data/lib/active_record/callbacks.rb +159 -44
  54. data/lib/active_record/coders/yaml_column.rb +12 -3
  55. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +292 -0
  56. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +209 -0
  57. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +76 -0
  58. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +92 -464
  59. data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -51
  60. data/lib/active_record/connection_adapters/abstract/database_statements.rb +209 -164
  61. data/lib/active_record/connection_adapters/abstract/query_cache.rb +38 -22
  62. data/lib/active_record/connection_adapters/abstract/quoting.rb +103 -82
  63. data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
  64. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +140 -110
  65. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +236 -94
  66. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +16 -5
  67. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +456 -159
  68. data/lib/active_record/connection_adapters/abstract/transaction.rb +169 -78
  69. data/lib/active_record/connection_adapters/abstract_adapter.rb +367 -162
  70. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +311 -327
  71. data/lib/active_record/connection_adapters/column.rb +33 -11
  72. data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
  73. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
  74. data/lib/active_record/connection_adapters/mysql/column.rb +1 -1
  75. data/lib/active_record/connection_adapters/mysql/database_statements.rb +113 -45
  76. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +1 -2
  77. data/lib/active_record/connection_adapters/mysql/quoting.rb +71 -5
  78. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +34 -10
  79. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +48 -32
  80. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +25 -8
  81. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +143 -19
  82. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +14 -9
  83. data/lib/active_record/connection_adapters/mysql2_adapter.rb +63 -22
  84. data/lib/active_record/connection_adapters/pool_config.rb +73 -0
  85. data/lib/active_record/connection_adapters/pool_manager.rb +47 -0
  86. data/lib/active_record/connection_adapters/postgresql/column.rb +53 -28
  87. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +56 -63
  88. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -2
  89. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +1 -4
  90. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -5
  91. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +10 -2
  92. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +15 -2
  93. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
  94. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +54 -16
  95. data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +3 -4
  97. data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
  99. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +3 -4
  100. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +25 -7
  101. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
  102. data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +26 -12
  105. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +15 -3
  106. data/lib/active_record/connection_adapters/postgresql/oid.rb +4 -0
  107. data/lib/active_record/connection_adapters/postgresql/quoting.rb +89 -52
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +34 -2
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +39 -4
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +128 -91
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +25 -1
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +149 -113
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +31 -26
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +0 -1
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +386 -182
  116. data/lib/active_record/connection_adapters/schema_cache.rb +161 -22
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +17 -6
  118. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +152 -0
  119. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +65 -18
  120. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +5 -1
  121. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +92 -26
  122. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +251 -204
  123. data/lib/active_record/connection_adapters/statement_pool.rb +0 -1
  124. data/lib/active_record/connection_adapters.rb +53 -0
  125. data/lib/active_record/connection_handling.rb +292 -38
  126. data/lib/active_record/core.rb +385 -158
  127. data/lib/active_record/counter_cache.rb +8 -30
  128. data/lib/active_record/database_configurations/connection_url_resolver.rb +100 -0
  129. data/lib/active_record/database_configurations/database_config.rb +83 -0
  130. data/lib/active_record/database_configurations/hash_config.rb +154 -0
  131. data/lib/active_record/database_configurations/url_config.rb +53 -0
  132. data/lib/active_record/database_configurations.rb +256 -0
  133. data/lib/active_record/delegated_type.rb +250 -0
  134. data/lib/active_record/destroy_association_async_job.rb +36 -0
  135. data/lib/active_record/disable_joins_association_relation.rb +39 -0
  136. data/lib/active_record/dynamic_matchers.rb +4 -5
  137. data/lib/active_record/encryption/cipher/aes256_gcm.rb +98 -0
  138. data/lib/active_record/encryption/cipher.rb +53 -0
  139. data/lib/active_record/encryption/config.rb +44 -0
  140. data/lib/active_record/encryption/configurable.rb +61 -0
  141. data/lib/active_record/encryption/context.rb +35 -0
  142. data/lib/active_record/encryption/contexts.rb +72 -0
  143. data/lib/active_record/encryption/derived_secret_key_provider.rb +12 -0
  144. data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
  145. data/lib/active_record/encryption/encryptable_record.rb +208 -0
  146. data/lib/active_record/encryption/encrypted_attribute_type.rb +140 -0
  147. data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
  148. data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
  149. data/lib/active_record/encryption/encryptor.rb +155 -0
  150. data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
  151. data/lib/active_record/encryption/errors.rb +15 -0
  152. data/lib/active_record/encryption/extended_deterministic_queries.rb +160 -0
  153. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
  154. data/lib/active_record/encryption/key.rb +28 -0
  155. data/lib/active_record/encryption/key_generator.rb +42 -0
  156. data/lib/active_record/encryption/key_provider.rb +46 -0
  157. data/lib/active_record/encryption/message.rb +33 -0
  158. data/lib/active_record/encryption/message_serializer.rb +90 -0
  159. data/lib/active_record/encryption/null_encryptor.rb +21 -0
  160. data/lib/active_record/encryption/properties.rb +76 -0
  161. data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
  162. data/lib/active_record/encryption/scheme.rb +99 -0
  163. data/lib/active_record/encryption.rb +55 -0
  164. data/lib/active_record/enum.rb +130 -51
  165. data/lib/active_record/errors.rb +129 -23
  166. data/lib/active_record/explain.rb +10 -6
  167. data/lib/active_record/explain_registry.rb +11 -6
  168. data/lib/active_record/explain_subscriber.rb +1 -1
  169. data/lib/active_record/fixture_set/file.rb +22 -15
  170. data/lib/active_record/fixture_set/model_metadata.rb +32 -0
  171. data/lib/active_record/fixture_set/render_context.rb +17 -0
  172. data/lib/active_record/fixture_set/table_row.rb +187 -0
  173. data/lib/active_record/fixture_set/table_rows.rb +46 -0
  174. data/lib/active_record/fixtures.rb +206 -490
  175. data/lib/active_record/future_result.rb +139 -0
  176. data/lib/active_record/gem_version.rb +3 -3
  177. data/lib/active_record/inheritance.rb +104 -37
  178. data/lib/active_record/insert_all.rb +278 -0
  179. data/lib/active_record/integration.rb +69 -18
  180. data/lib/active_record/internal_metadata.rb +24 -9
  181. data/lib/active_record/legacy_yaml_adapter.rb +3 -36
  182. data/lib/active_record/locking/optimistic.rb +41 -26
  183. data/lib/active_record/locking/pessimistic.rb +18 -8
  184. data/lib/active_record/log_subscriber.rb +46 -35
  185. data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
  186. data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
  187. data/lib/active_record/middleware/database_selector.rb +82 -0
  188. data/lib/active_record/middleware/shard_selector.rb +60 -0
  189. data/lib/active_record/migration/command_recorder.rb +96 -44
  190. data/lib/active_record/migration/compatibility.rb +246 -64
  191. data/lib/active_record/migration/join_table.rb +1 -2
  192. data/lib/active_record/migration.rb +266 -187
  193. data/lib/active_record/model_schema.rb +165 -52
  194. data/lib/active_record/nested_attributes.rb +17 -19
  195. data/lib/active_record/no_touching.rb +11 -4
  196. data/lib/active_record/null_relation.rb +2 -7
  197. data/lib/active_record/persistence.rb +467 -92
  198. data/lib/active_record/query_cache.rb +21 -4
  199. data/lib/active_record/query_logs.rb +138 -0
  200. data/lib/active_record/querying.rb +51 -24
  201. data/lib/active_record/railtie.rb +224 -57
  202. data/lib/active_record/railties/console_sandbox.rb +2 -4
  203. data/lib/active_record/railties/controller_runtime.rb +31 -36
  204. data/lib/active_record/railties/databases.rake +369 -101
  205. data/lib/active_record/readonly_attributes.rb +15 -0
  206. data/lib/active_record/reflection.rb +170 -137
  207. data/lib/active_record/relation/batches/batch_enumerator.rb +44 -14
  208. data/lib/active_record/relation/batches.rb +46 -37
  209. data/lib/active_record/relation/calculations.rb +168 -96
  210. data/lib/active_record/relation/delegation.rb +37 -52
  211. data/lib/active_record/relation/finder_methods.rb +79 -58
  212. data/lib/active_record/relation/from_clause.rb +5 -1
  213. data/lib/active_record/relation/merger.rb +50 -51
  214. data/lib/active_record/relation/predicate_builder/array_handler.rb +13 -13
  215. data/lib/active_record/relation/predicate_builder/association_query_value.rb +5 -9
  216. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +1 -2
  217. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +11 -10
  218. data/lib/active_record/relation/predicate_builder/range_handler.rb +3 -23
  219. data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
  220. data/lib/active_record/relation/predicate_builder.rb +58 -46
  221. data/lib/active_record/relation/query_attribute.rb +9 -10
  222. data/lib/active_record/relation/query_methods.rb +685 -208
  223. data/lib/active_record/relation/record_fetch_warning.rb +9 -11
  224. data/lib/active_record/relation/spawn_methods.rb +10 -10
  225. data/lib/active_record/relation/where_clause.rb +108 -64
  226. data/lib/active_record/relation.rb +515 -151
  227. data/lib/active_record/result.rb +78 -42
  228. data/lib/active_record/runtime_registry.rb +9 -13
  229. data/lib/active_record/sanitization.rb +29 -44
  230. data/lib/active_record/schema.rb +37 -31
  231. data/lib/active_record/schema_dumper.rb +74 -23
  232. data/lib/active_record/schema_migration.rb +7 -9
  233. data/lib/active_record/scoping/default.rb +62 -17
  234. data/lib/active_record/scoping/named.rb +17 -32
  235. data/lib/active_record/scoping.rb +70 -41
  236. data/lib/active_record/secure_token.rb +16 -8
  237. data/lib/active_record/serialization.rb +6 -4
  238. data/lib/active_record/signed_id.rb +116 -0
  239. data/lib/active_record/statement_cache.rb +49 -6
  240. data/lib/active_record/store.rb +88 -9
  241. data/lib/active_record/suppressor.rb +13 -17
  242. data/lib/active_record/table_metadata.rb +42 -43
  243. data/lib/active_record/tasks/database_tasks.rb +352 -94
  244. data/lib/active_record/tasks/mysql_database_tasks.rb +37 -39
  245. data/lib/active_record/tasks/postgresql_database_tasks.rb +41 -39
  246. data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -17
  247. data/lib/active_record/test_databases.rb +24 -0
  248. data/lib/active_record/test_fixtures.rb +287 -0
  249. data/lib/active_record/timestamp.rb +44 -34
  250. data/lib/active_record/touch_later.rb +23 -22
  251. data/lib/active_record/transactions.rb +67 -128
  252. data/lib/active_record/translation.rb +3 -3
  253. data/lib/active_record/type/adapter_specific_registry.rb +34 -19
  254. data/lib/active_record/type/hash_lookup_type_map.rb +34 -2
  255. data/lib/active_record/type/internal/timezone.rb +2 -2
  256. data/lib/active_record/type/serialized.rb +7 -4
  257. data/lib/active_record/type/time.rb +10 -0
  258. data/lib/active_record/type/type_map.rb +17 -21
  259. data/lib/active_record/type/unsigned_integer.rb +0 -1
  260. data/lib/active_record/type.rb +9 -5
  261. data/lib/active_record/type_caster/connection.rb +15 -15
  262. data/lib/active_record/type_caster/map.rb +8 -8
  263. data/lib/active_record/validations/associated.rb +2 -3
  264. data/lib/active_record/validations/numericality.rb +35 -0
  265. data/lib/active_record/validations/uniqueness.rb +39 -31
  266. data/lib/active_record/validations.rb +4 -3
  267. data/lib/active_record.rb +209 -32
  268. data/lib/arel/alias_predication.rb +9 -0
  269. data/lib/arel/attributes/attribute.rb +33 -0
  270. data/lib/arel/collectors/bind.rb +29 -0
  271. data/lib/arel/collectors/composite.rb +39 -0
  272. data/lib/arel/collectors/plain_string.rb +20 -0
  273. data/lib/arel/collectors/sql_string.rb +27 -0
  274. data/lib/arel/collectors/substitute_binds.rb +35 -0
  275. data/lib/arel/crud.rb +48 -0
  276. data/lib/arel/delete_manager.rb +32 -0
  277. data/lib/arel/errors.rb +9 -0
  278. data/lib/arel/expressions.rb +29 -0
  279. data/lib/arel/factory_methods.rb +49 -0
  280. data/lib/arel/filter_predications.rb +9 -0
  281. data/lib/arel/insert_manager.rb +48 -0
  282. data/lib/arel/math.rb +45 -0
  283. data/lib/arel/nodes/and.rb +32 -0
  284. data/lib/arel/nodes/ascending.rb +23 -0
  285. data/lib/arel/nodes/binary.rb +126 -0
  286. data/lib/arel/nodes/bind_param.rb +44 -0
  287. data/lib/arel/nodes/case.rb +55 -0
  288. data/lib/arel/nodes/casted.rb +62 -0
  289. data/lib/arel/nodes/comment.rb +29 -0
  290. data/lib/arel/nodes/count.rb +12 -0
  291. data/lib/arel/nodes/delete_statement.rb +44 -0
  292. data/lib/arel/nodes/descending.rb +23 -0
  293. data/lib/arel/nodes/equality.rb +15 -0
  294. data/lib/arel/nodes/extract.rb +24 -0
  295. data/lib/arel/nodes/false.rb +16 -0
  296. data/lib/arel/nodes/filter.rb +10 -0
  297. data/lib/arel/nodes/full_outer_join.rb +8 -0
  298. data/lib/arel/nodes/function.rb +45 -0
  299. data/lib/arel/nodes/grouping.rb +11 -0
  300. data/lib/arel/nodes/homogeneous_in.rb +76 -0
  301. data/lib/arel/nodes/in.rb +15 -0
  302. data/lib/arel/nodes/infix_operation.rb +92 -0
  303. data/lib/arel/nodes/inner_join.rb +8 -0
  304. data/lib/arel/nodes/insert_statement.rb +37 -0
  305. data/lib/arel/nodes/join_source.rb +20 -0
  306. data/lib/arel/nodes/matches.rb +18 -0
  307. data/lib/arel/nodes/named_function.rb +23 -0
  308. data/lib/arel/nodes/node.rb +51 -0
  309. data/lib/arel/nodes/node_expression.rb +13 -0
  310. data/lib/arel/nodes/ordering.rb +27 -0
  311. data/lib/arel/nodes/outer_join.rb +8 -0
  312. data/lib/arel/nodes/over.rb +15 -0
  313. data/lib/arel/nodes/regexp.rb +16 -0
  314. data/lib/arel/nodes/right_outer_join.rb +8 -0
  315. data/lib/arel/nodes/select_core.rb +67 -0
  316. data/lib/arel/nodes/select_statement.rb +41 -0
  317. data/lib/arel/nodes/sql_literal.rb +19 -0
  318. data/lib/arel/nodes/string_join.rb +11 -0
  319. data/lib/arel/nodes/table_alias.rb +31 -0
  320. data/lib/arel/nodes/terminal.rb +16 -0
  321. data/lib/arel/nodes/true.rb +16 -0
  322. data/lib/arel/nodes/unary.rb +44 -0
  323. data/lib/arel/nodes/unary_operation.rb +20 -0
  324. data/lib/arel/nodes/unqualified_column.rb +22 -0
  325. data/lib/arel/nodes/update_statement.rb +46 -0
  326. data/lib/arel/nodes/values_list.rb +9 -0
  327. data/lib/arel/nodes/window.rb +126 -0
  328. data/lib/arel/nodes/with.rb +11 -0
  329. data/lib/arel/nodes.rb +71 -0
  330. data/lib/arel/order_predications.rb +13 -0
  331. data/lib/arel/predications.rb +258 -0
  332. data/lib/arel/select_manager.rb +276 -0
  333. data/lib/arel/table.rb +117 -0
  334. data/lib/arel/tree_manager.rb +60 -0
  335. data/lib/arel/update_manager.rb +48 -0
  336. data/lib/arel/visitors/dot.rb +298 -0
  337. data/lib/arel/visitors/mysql.rb +99 -0
  338. data/lib/arel/visitors/postgresql.rb +110 -0
  339. data/lib/arel/visitors/sqlite.rb +38 -0
  340. data/lib/arel/visitors/to_sql.rb +955 -0
  341. data/lib/arel/visitors/visitor.rb +45 -0
  342. data/lib/arel/visitors.rb +13 -0
  343. data/lib/arel/window_predications.rb +9 -0
  344. data/lib/arel.rb +55 -0
  345. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
  346. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
  347. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -5
  348. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +3 -1
  349. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +7 -5
  350. data/lib/rails/generators/active_record/migration.rb +19 -2
  351. data/lib/rails/generators/active_record/model/model_generator.rb +39 -2
  352. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
  353. data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
  354. data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
  355. data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
  356. data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
  357. metadata +162 -32
  358. data/lib/active_record/attribute_decorators.rb +0 -90
  359. data/lib/active_record/collection_cache_key.rb +0 -53
  360. data/lib/active_record/connection_adapters/connection_specification.rb +0 -287
  361. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -33
  362. data/lib/active_record/define_callbacks.rb +0 -22
  363. data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -19
  364. data/lib/active_record/relation/where_clause_factory.rb +0 -34
@@ -0,0 +1,256 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "active_record/database_configurations/database_config"
5
+ require "active_record/database_configurations/hash_config"
6
+ require "active_record/database_configurations/url_config"
7
+ require "active_record/database_configurations/connection_url_resolver"
8
+
9
+ module ActiveRecord
10
+ # ActiveRecord::DatabaseConfigurations returns an array of DatabaseConfig
11
+ # objects (either a HashConfig or UrlConfig) that are constructed from the
12
+ # application's database configuration hash or URL string.
13
+ class DatabaseConfigurations
14
+ class InvalidConfigurationError < StandardError; end
15
+
16
+ attr_reader :configurations
17
+ delegate :any?, to: :configurations
18
+
19
+ def initialize(configurations = {})
20
+ @configurations = build_configs(configurations)
21
+ end
22
+
23
+ # Collects the configs for the environment and optionally the specification
24
+ # name passed in. To include replica configurations pass <tt>include_hidden: true</tt>.
25
+ #
26
+ # If a name is provided a single DatabaseConfig object will be
27
+ # returned, otherwise an array of DatabaseConfig objects will be
28
+ # returned that corresponds with the environment and type requested.
29
+ #
30
+ # ==== Options
31
+ #
32
+ # * <tt>env_name:</tt> The environment name. Defaults to +nil+ which will collect
33
+ # configs for all environments.
34
+ # * <tt>name:</tt> The db config name (i.e. primary, animals, etc.). Defaults
35
+ # to +nil+. If no +env_name+ is specified the config for the default env and the
36
+ # passed +name+ will be returned.
37
+ # * <tt>include_replicas:</tt> Deprecated. Determines whether to include replicas in
38
+ # the returned list. Most of the time we're only iterating over the write
39
+ # connection (i.e. migrations don't need to run for the write and read connection).
40
+ # Defaults to +false+.
41
+ # * <tt>include_hidden:</tt> Determines whether to include replicas and configurations
42
+ # hidden by +database_tasks: false+ in the returned list. Most of the time we're only
43
+ # iterating over the primary connections (i.e. migrations don't need to run for the
44
+ # write and read connection). Defaults to +false+.
45
+ def configs_for(env_name: nil, name: nil, include_replicas: false, include_hidden: false)
46
+ if include_replicas
47
+ include_hidden = include_replicas
48
+ ActiveSupport::Deprecation.warn("The kwarg `include_replicas` is deprecated in favor of `include_hidden`. When `include_hidden` is passed, configurations with `replica: true` or `database_tasks: false` will be returned. `include_replicas` will be removed in Rails 7.1.")
49
+ end
50
+
51
+ env_name ||= default_env if name
52
+ configs = env_with_configs(env_name)
53
+
54
+ unless include_hidden
55
+ configs = configs.select do |db_config|
56
+ db_config.database_tasks?
57
+ end
58
+ end
59
+
60
+ if name
61
+ configs.find do |db_config|
62
+ db_config.name == name
63
+ end
64
+ else
65
+ configs
66
+ end
67
+ end
68
+
69
+ # Returns a single DatabaseConfig object based on the requested environment.
70
+ #
71
+ # If the application has multiple databases +find_db_config+ will return
72
+ # the first DatabaseConfig for the environment.
73
+ def find_db_config(env)
74
+ configurations
75
+ .sort_by.with_index { |db_config, i| db_config.for_current_env? ? [0, i] : [1, i] }
76
+ .find do |db_config|
77
+ db_config.env_name == env.to_s ||
78
+ (db_config.for_current_env? && db_config.name == env.to_s)
79
+ end
80
+ end
81
+
82
+ # A primary configuration is one that is named primary or if there is
83
+ # no primary, the first configuration for an environment will be treated
84
+ # as primary. This is used as the "default" configuration and is used
85
+ # when the application needs to treat one configuration differently. For
86
+ # example, when Rails dumps the schema, the primary configuration's schema
87
+ # file will be named `schema.rb` instead of `primary_schema.rb`.
88
+ def primary?(name) # :nodoc:
89
+ return true if name == "primary"
90
+
91
+ first_config = find_db_config(default_env)
92
+ first_config && name == first_config.name
93
+ end
94
+
95
+ # Checks if the application's configurations are empty.
96
+ #
97
+ # Aliased to blank?
98
+ def empty?
99
+ configurations.empty?
100
+ end
101
+ alias :blank? :empty?
102
+
103
+ # Returns fully resolved connection, accepts hash, string or symbol.
104
+ # Always returns a DatabaseConfiguration::DatabaseConfig
105
+ #
106
+ # == Examples
107
+ #
108
+ # Symbol representing current environment.
109
+ #
110
+ # DatabaseConfigurations.new("production" => {}).resolve(:production)
111
+ # # => DatabaseConfigurations::HashConfig.new(env_name: "production", config: {})
112
+ #
113
+ # One layer deep hash of connection values.
114
+ #
115
+ # DatabaseConfigurations.new({}).resolve("adapter" => "sqlite3")
116
+ # # => DatabaseConfigurations::HashConfig.new(config: {"adapter" => "sqlite3"})
117
+ #
118
+ # Connection URL.
119
+ #
120
+ # DatabaseConfigurations.new({}).resolve("postgresql://localhost/foo")
121
+ # # => DatabaseConfigurations::UrlConfig.new(config: {"adapter" => "postgresql", "host" => "localhost", "database" => "foo"})
122
+ def resolve(config) # :nodoc:
123
+ return config if DatabaseConfigurations::DatabaseConfig === config
124
+
125
+ case config
126
+ when Symbol
127
+ resolve_symbol_connection(config)
128
+ when Hash, String
129
+ build_db_config_from_raw_config(default_env, "primary", config)
130
+ else
131
+ raise TypeError, "Invalid type for configuration. Expected Symbol, String, or Hash. Got #{config.inspect}"
132
+ end
133
+ end
134
+
135
+ private
136
+ def default_env
137
+ ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_s
138
+ end
139
+
140
+ def env_with_configs(env = nil)
141
+ if env
142
+ configurations.select { |db_config| db_config.env_name == env }
143
+ else
144
+ configurations
145
+ end
146
+ end
147
+
148
+ def build_configs(configs)
149
+ return configs.configurations if configs.is_a?(DatabaseConfigurations)
150
+ return configs if configs.is_a?(Array)
151
+
152
+ db_configs = configs.flat_map do |env_name, config|
153
+ if config.is_a?(Hash) && config.values.all?(Hash)
154
+ walk_configs(env_name.to_s, config)
155
+ else
156
+ build_db_config_from_raw_config(env_name.to_s, "primary", config)
157
+ end
158
+ end
159
+
160
+ unless db_configs.find(&:for_current_env?)
161
+ db_configs << environment_url_config(default_env, "primary", {})
162
+ end
163
+
164
+ merge_db_environment_variables(default_env, db_configs.compact)
165
+ end
166
+
167
+ def walk_configs(env_name, config)
168
+ config.map do |name, sub_config|
169
+ build_db_config_from_raw_config(env_name, name.to_s, sub_config)
170
+ end
171
+ end
172
+
173
+ def resolve_symbol_connection(name)
174
+ if db_config = find_db_config(name)
175
+ db_config
176
+ else
177
+ raise AdapterNotSpecified, <<~MSG
178
+ The `#{name}` database is not configured for the `#{default_env}` environment.
179
+
180
+ Available database configurations are:
181
+
182
+ #{build_configuration_sentence}
183
+ MSG
184
+ end
185
+ end
186
+
187
+ def build_configuration_sentence
188
+ configs = configs_for(include_hidden: true)
189
+
190
+ configs.group_by(&:env_name).map do |env, config|
191
+ names = config.map(&:name)
192
+ if names.size > 1
193
+ "#{env}: #{names.join(", ")}"
194
+ else
195
+ env
196
+ end
197
+ end.join("\n")
198
+ end
199
+
200
+ def build_db_config_from_raw_config(env_name, name, config)
201
+ case config
202
+ when String
203
+ build_db_config_from_string(env_name, name, config)
204
+ when Hash
205
+ build_db_config_from_hash(env_name, name, config.symbolize_keys)
206
+ else
207
+ raise InvalidConfigurationError, "'{ #{env_name} => #{config} }' is not a valid configuration. Expected '#{config}' to be a URL string or a Hash."
208
+ end
209
+ end
210
+
211
+ def build_db_config_from_string(env_name, name, config)
212
+ url = config
213
+ uri = URI.parse(url)
214
+ if uri.scheme
215
+ UrlConfig.new(env_name, name, url)
216
+ else
217
+ raise InvalidConfigurationError, "'{ #{env_name} => #{config} }' is not a valid configuration. Expected '#{config}' to be a URL string or a Hash."
218
+ end
219
+ end
220
+
221
+ def build_db_config_from_hash(env_name, name, config)
222
+ if config.has_key?(:url)
223
+ url = config[:url]
224
+ config_without_url = config.dup
225
+ config_without_url.delete :url
226
+
227
+ UrlConfig.new(env_name, name, url, config_without_url)
228
+ else
229
+ HashConfig.new(env_name, name, config)
230
+ end
231
+ end
232
+
233
+ def merge_db_environment_variables(current_env, configs)
234
+ configs.map do |config|
235
+ next config if config.is_a?(UrlConfig) || config.env_name != current_env
236
+
237
+ url_config = environment_url_config(current_env, config.name, config.configuration_hash)
238
+ url_config || config
239
+ end
240
+ end
241
+
242
+ def environment_url_config(env, name, config)
243
+ url = environment_value_for(name)
244
+ return unless url
245
+
246
+ UrlConfig.new(env, name, url, config)
247
+ end
248
+
249
+ def environment_value_for(name)
250
+ name_env_key = "#{name.upcase}_DATABASE_URL"
251
+ url = ENV[name_env_key]
252
+ url ||= ENV["DATABASE_URL"] if name == "primary"
253
+ url
254
+ end
255
+ end
256
+ end
@@ -0,0 +1,250 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/string/inquiry"
4
+
5
+ module ActiveRecord
6
+ # == Delegated types
7
+ #
8
+ # Class hierarchies can map to relational database tables in many ways. Active Record, for example, offers
9
+ # purely abstract classes, where the superclass doesn't persist any attributes, and single-table inheritance,
10
+ # where all attributes from all levels of the hierarchy are represented in a single table. Both have their
11
+ # places, but neither are without their drawbacks.
12
+ #
13
+ # The problem with purely abstract classes is that all concrete subclasses must persist all the shared
14
+ # attributes themselves in their own tables (also known as class-table inheritance). This makes it hard to
15
+ # do queries across the hierarchy. For example, imagine you have the following hierarchy:
16
+ #
17
+ # Entry < ApplicationRecord
18
+ # Message < Entry
19
+ # Comment < Entry
20
+ #
21
+ # How do you show a feed that has both +Message+ and +Comment+ records, which can be easily paginated?
22
+ # Well, you can't! Messages are backed by a messages table and comments by a comments table. You can't
23
+ # pull from both tables at once and use a consistent OFFSET/LIMIT scheme.
24
+ #
25
+ # You can get around the pagination problem by using single-table inheritance, but now you're forced into
26
+ # a single mega table with all the attributes from all subclasses. No matter how divergent. If a Message
27
+ # has a subject, but the comment does not, well, now the comment does anyway! So STI works best when there's
28
+ # little divergence between the subclasses and their attributes.
29
+ #
30
+ # But there's a third way: Delegated types. With this approach, the "superclass" is a concrete class
31
+ # that is represented by its own table, where all the superclass attributes that are shared amongst all the
32
+ # "subclasses" are stored. And then each of the subclasses have their own individual tables for additional
33
+ # attributes that are particular to their implementation. This is similar to what's called multi-table
34
+ # inheritance in Django, but instead of actual inheritance, this approach uses delegation to form the
35
+ # hierarchy and share responsibilities.
36
+ #
37
+ # Let's look at that entry/message/comment example using delegated types:
38
+ #
39
+ # # Schema: entries[ id, account_id, creator_id, created_at, updated_at, entryable_type, entryable_id ]
40
+ # class Entry < ApplicationRecord
41
+ # belongs_to :account
42
+ # belongs_to :creator
43
+ # delegated_type :entryable, types: %w[ Message Comment ]
44
+ # end
45
+ #
46
+ # module Entryable
47
+ # extend ActiveSupport::Concern
48
+ #
49
+ # included do
50
+ # has_one :entry, as: :entryable, touch: true
51
+ # end
52
+ # end
53
+ #
54
+ # # Schema: messages[ id, subject, body ]
55
+ # class Message < ApplicationRecord
56
+ # include Entryable
57
+ # end
58
+ #
59
+ # # Schema: comments[ id, content ]
60
+ # class Comment < ApplicationRecord
61
+ # include Entryable
62
+ # end
63
+ #
64
+ # As you can see, neither +Message+ nor +Comment+ are meant to stand alone. Crucial metadata for both classes
65
+ # resides in the +Entry+ "superclass". But the +Entry+ absolutely can stand alone in terms of querying capacity
66
+ # in particular. You can now easily do things like:
67
+ #
68
+ # Account.find(1).entries.order(created_at: :desc).limit(50)
69
+ #
70
+ # Which is exactly what you want when displaying both comments and messages together. The entry itself can
71
+ # be rendered as its delegated type easily, like so:
72
+ #
73
+ # # entries/_entry.html.erb
74
+ # <%= render "entries/entryables/#{entry.entryable_name}", entry: entry %>
75
+ #
76
+ # # entries/entryables/_message.html.erb
77
+ # <div class="message">
78
+ # <div class="subject"><%= entry.message.subject %></div>
79
+ # <p><%= entry.message.body %></p>
80
+ # <i>Posted on <%= entry.created_at %> by <%= entry.creator.name %></i>
81
+ # </div>
82
+ #
83
+ # # entries/entryables/_comment.html.erb
84
+ # <div class="comment">
85
+ # <%= entry.creator.name %> said: <%= entry.comment.content %>
86
+ # </div>
87
+ #
88
+ # == Sharing behavior with concerns and controllers
89
+ #
90
+ # The entry "superclass" also serves as a perfect place to put all that shared logic that applies to both
91
+ # messages and comments, and which acts primarily on the shared attributes. Imagine:
92
+ #
93
+ # class Entry < ApplicationRecord
94
+ # include Eventable, Forwardable, Redeliverable
95
+ # end
96
+ #
97
+ # Which allows you to have controllers for things like +ForwardsController+ and +RedeliverableController+
98
+ # that both act on entries, and thus provide the shared functionality to both messages and comments.
99
+ #
100
+ # == Creating new records
101
+ #
102
+ # You create a new record that uses delegated typing by creating the delegator and delegatee at the same time,
103
+ # like so:
104
+ #
105
+ # Entry.create! entryable: Comment.new(content: "Hello!"), creator: Current.user
106
+ #
107
+ # If you need more complicated composition, or you need to perform dependent validation, you should build a factory
108
+ # method or class to take care of the complicated needs. This could be as simple as:
109
+ #
110
+ # class Entry < ApplicationRecord
111
+ # def self.create_with_comment(content, creator: Current.user)
112
+ # create! entryable: Comment.new(content: content), creator: creator
113
+ # end
114
+ # end
115
+ #
116
+ # == Adding further delegation
117
+ #
118
+ # The delegated type shouldn't just answer the question of what the underlying class is called. In fact, that's
119
+ # an anti-pattern most of the time. The reason you're building this hierarchy is to take advantage of polymorphism.
120
+ # So here's a simple example of that:
121
+ #
122
+ # class Entry < ApplicationRecord
123
+ # delegated_type :entryable, types: %w[ Message Comment ]
124
+ # delegate :title, to: :entryable
125
+ # end
126
+ #
127
+ # class Message < ApplicationRecord
128
+ # def title
129
+ # subject
130
+ # end
131
+ # end
132
+ #
133
+ # class Comment < ApplicationRecord
134
+ # def title
135
+ # content.truncate(20)
136
+ # end
137
+ # end
138
+ #
139
+ # Now you can list a bunch of entries, call +Entry#title+, and polymorphism will provide you with the answer.
140
+ #
141
+ # == Nested Attributes
142
+ #
143
+ # Enabling nested attributes on a delegated_type association allows you to
144
+ # create the entry and message in one go:
145
+ #
146
+ # class Entry < ApplicationRecord
147
+ # delegated_type :entryable, types: %w[ Message Comment ]
148
+ # accepts_nested_attributes_for :entryable
149
+ # end
150
+ #
151
+ # params = { entry: { entryable_type: 'Message', entryable_attributes: { subject: 'Smiling' } } }
152
+ # entry = Entry.create(params[:entry])
153
+ # entry.entryable.id # => 2
154
+ # entry.entryable.subject # => 'Smiling'
155
+ module DelegatedType
156
+ # Defines this as a class that'll delegate its type for the passed +role+ to the class references in +types+.
157
+ # That'll create a polymorphic +belongs_to+ relationship to that +role+, and it'll add all the delegated
158
+ # type convenience methods:
159
+ #
160
+ # class Entry < ApplicationRecord
161
+ # delegated_type :entryable, types: %w[ Message Comment ], dependent: :destroy
162
+ # end
163
+ #
164
+ # Entry#entryable_class # => +Message+ or +Comment+
165
+ # Entry#entryable_name # => "message" or "comment"
166
+ # Entry.messages # => Entry.where(entryable_type: "Message")
167
+ # Entry#message? # => true when entryable_type == "Message"
168
+ # Entry#message # => returns the message record, when entryable_type == "Message", otherwise nil
169
+ # Entry#message_id # => returns entryable_id, when entryable_type == "Message", otherwise nil
170
+ # Entry.comments # => Entry.where(entryable_type: "Comment")
171
+ # Entry#comment? # => true when entryable_type == "Comment"
172
+ # Entry#comment # => returns the comment record, when entryable_type == "Comment", otherwise nil
173
+ # Entry#comment_id # => returns entryable_id, when entryable_type == "Comment", otherwise nil
174
+ #
175
+ # You can also declare namespaced types:
176
+ #
177
+ # class Entry < ApplicationRecord
178
+ # delegated_type :entryable, types: %w[ Message Comment Access::NoticeMessage ], dependent: :destroy
179
+ # end
180
+ #
181
+ # Entry.access_notice_messages
182
+ # entry.access_notice_message
183
+ # entry.access_notice_message?
184
+ #
185
+ # === Options
186
+ #
187
+ # The +options+ are passed directly to the +belongs_to+ call, so this is where you declare +dependent+ etc.
188
+ # The following options can be included to specialize the behavior of the delegated type convenience methods.
189
+ #
190
+ # [:foreign_key]
191
+ # Specify the foreign key used for the convenience methods. By default this is guessed to be the passed
192
+ # +role+ with an "_id" suffix. So a class that defines a
193
+ # <tt>delegated_type :entryable, types: %w[ Message Comment ]</tt> association will use "entryable_id" as
194
+ # the default <tt>:foreign_key</tt>.
195
+ # [:primary_key]
196
+ # Specify the method that returns the primary key of associated object used for the convenience methods.
197
+ # By default this is +id+.
198
+ #
199
+ # Option examples:
200
+ # class Entry < ApplicationRecord
201
+ # delegated_type :entryable, types: %w[ Message Comment ], primary_key: :uuid, foreign_key: :entryable_uuid
202
+ # end
203
+ #
204
+ # Entry#message_uuid # => returns entryable_uuid, when entryable_type == "Message", otherwise nil
205
+ # Entry#comment_uuid # => returns entryable_uuid, when entryable_type == "Comment", otherwise nil
206
+ def delegated_type(role, types:, **options)
207
+ belongs_to role, options.delete(:scope), **options.merge(polymorphic: true)
208
+ define_delegated_type_methods role, types: types, options: options
209
+ end
210
+
211
+ private
212
+ def define_delegated_type_methods(role, types:, options:)
213
+ primary_key = options[:primary_key] || "id"
214
+ role_type = "#{role}_type"
215
+ role_id = options[:foreign_key] || "#{role}_id"
216
+
217
+ define_method "#{role}_class" do
218
+ public_send("#{role}_type").constantize
219
+ end
220
+
221
+ define_method "#{role}_name" do
222
+ public_send("#{role}_class").model_name.singular.inquiry
223
+ end
224
+
225
+ define_method "build_#{role}" do |*params|
226
+ public_send("#{role}=", public_send("#{role}_class").new(*params))
227
+ end
228
+
229
+ types.each do |type|
230
+ scope_name = type.tableize.tr("/", "_")
231
+ singular = scope_name.singularize
232
+ query = "#{singular}?"
233
+
234
+ scope scope_name, -> { where(role_type => type) }
235
+
236
+ define_method query do
237
+ public_send(role_type) == type
238
+ end
239
+
240
+ define_method singular do
241
+ public_send(role) if public_send(query)
242
+ end
243
+
244
+ define_method "#{singular}_#{primary_key}" do
245
+ public_send(role_id) if public_send(query)
246
+ end
247
+ end
248
+ end
249
+ end
250
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ class DestroyAssociationAsyncError < StandardError
5
+ end
6
+
7
+ # Job to destroy the records associated with a destroyed record in background.
8
+ class DestroyAssociationAsyncJob < ActiveJob::Base
9
+ queue_as { ActiveRecord.queues[:destroy] }
10
+
11
+ discard_on ActiveJob::DeserializationError
12
+
13
+ def perform(
14
+ owner_model_name: nil, owner_id: nil,
15
+ association_class: nil, association_ids: nil, association_primary_key_column: nil,
16
+ ensuring_owner_was_method: nil
17
+ )
18
+ association_model = association_class.constantize
19
+ owner_class = owner_model_name.constantize
20
+ owner = owner_class.find_by(owner_class.primary_key.to_sym => owner_id)
21
+
22
+ if !owner_destroyed?(owner, ensuring_owner_was_method)
23
+ raise DestroyAssociationAsyncError, "owner record not destroyed"
24
+ end
25
+
26
+ association_model.where(association_primary_key_column => association_ids).find_each do |r|
27
+ r.destroy
28
+ end
29
+ end
30
+
31
+ private
32
+ def owner_destroyed?(owner, ensuring_owner_was_method)
33
+ !owner || (ensuring_owner_was_method && owner.public_send(ensuring_owner_was_method))
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ class DisableJoinsAssociationRelation < Relation # :nodoc:
5
+ attr_reader :ids, :key
6
+
7
+ def initialize(klass, key, ids)
8
+ @ids = ids.uniq
9
+ @key = key
10
+ super(klass)
11
+ end
12
+
13
+ def limit(value)
14
+ records.take(value)
15
+ end
16
+
17
+ def first(limit = nil)
18
+ if limit
19
+ records.limit(limit).first
20
+ else
21
+ records.first
22
+ end
23
+ end
24
+
25
+ def load
26
+ super
27
+ records = @records
28
+
29
+ records_by_id = records.group_by do |record|
30
+ record[key]
31
+ end
32
+
33
+ records = ids.flat_map { |id| records_by_id[id.to_i] }
34
+ records.compact!
35
+
36
+ @records = records
37
+ end
38
+ end
39
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecord
4
- module DynamicMatchers #:nodoc:
4
+ module DynamicMatchers # :nodoc:
5
5
  private
6
6
  def respond_to_missing?(name, _)
7
7
  if self == Base
@@ -49,11 +49,11 @@ module ActiveRecord
49
49
 
50
50
  attr_reader :model, :name, :attribute_names
51
51
 
52
- def initialize(model, name)
52
+ def initialize(model, method_name)
53
53
  @model = model
54
- @name = name.to_s
54
+ @name = method_name.to_s
55
55
  @attribute_names = @name.match(self.class.pattern)[1].split("_and_")
56
- @attribute_names.map! { |n| @model.attribute_aliases[n] || n }
56
+ @attribute_names.map! { |name| @model.attribute_aliases[name] || name }
57
57
  end
58
58
 
59
59
  def valid?
@@ -69,7 +69,6 @@ module ActiveRecord
69
69
  end
70
70
 
71
71
  private
72
-
73
72
  def body
74
73
  "#{finder}(#{attributes_hash})"
75
74
  end