activerecord 5.2.8.1 → 6.1.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (316) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1347 -624
  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 +9 -8
  7. data/lib/active_record/association_relation.rb +30 -10
  8. data/lib/active_record/associations/alias_tracker.rb +19 -16
  9. data/lib/active_record/associations/association.rb +100 -41
  10. data/lib/active_record/associations/association_scope.rb +23 -21
  11. data/lib/active_record/associations/belongs_to_association.rb +55 -48
  12. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -6
  13. data/lib/active_record/associations/builder/association.rb +45 -22
  14. data/lib/active_record/associations/builder/belongs_to.rb +29 -59
  15. data/lib/active_record/associations/builder/collection_association.rb +8 -17
  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 +8 -2
  18. data/lib/active_record/associations/builder/has_one.rb +33 -2
  19. data/lib/active_record/associations/builder/singular_association.rb +3 -1
  20. data/lib/active_record/associations/collection_association.rb +44 -34
  21. data/lib/active_record/associations/collection_proxy.rb +25 -21
  22. data/lib/active_record/associations/foreign_association.rb +20 -0
  23. data/lib/active_record/associations/has_many_association.rb +26 -13
  24. data/lib/active_record/associations/has_many_through_association.rb +24 -18
  25. data/lib/active_record/associations/has_one_association.rb +43 -31
  26. data/lib/active_record/associations/has_one_through_association.rb +5 -5
  27. data/lib/active_record/associations/join_dependency/join_association.rb +44 -22
  28. data/lib/active_record/associations/join_dependency/join_part.rb +5 -5
  29. data/lib/active_record/associations/join_dependency.rb +91 -60
  30. data/lib/active_record/associations/preloader/association.rb +69 -43
  31. data/lib/active_record/associations/preloader/through_association.rb +49 -40
  32. data/lib/active_record/associations/preloader.rb +47 -34
  33. data/lib/active_record/associations/singular_association.rb +3 -17
  34. data/lib/active_record/associations/through_association.rb +1 -1
  35. data/lib/active_record/associations.rb +137 -25
  36. data/lib/active_record/attribute_assignment.rb +17 -19
  37. data/lib/active_record/attribute_methods/before_type_cast.rb +13 -7
  38. data/lib/active_record/attribute_methods/dirty.rb +101 -40
  39. data/lib/active_record/attribute_methods/primary_key.rb +20 -25
  40. data/lib/active_record/attribute_methods/query.rb +4 -8
  41. data/lib/active_record/attribute_methods/read.rb +14 -56
  42. data/lib/active_record/attribute_methods/serialization.rb +12 -7
  43. data/lib/active_record/attribute_methods/time_zone_conversion.rb +12 -15
  44. data/lib/active_record/attribute_methods/write.rb +18 -34
  45. data/lib/active_record/attribute_methods.rb +81 -143
  46. data/lib/active_record/attributes.rb +46 -9
  47. data/lib/active_record/autosave_association.rb +57 -42
  48. data/lib/active_record/base.rb +4 -17
  49. data/lib/active_record/callbacks.rb +158 -43
  50. data/lib/active_record/coders/yaml_column.rb +16 -7
  51. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +272 -130
  52. data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -36
  53. data/lib/active_record/connection_adapters/abstract/database_statements.rb +167 -146
  54. data/lib/active_record/connection_adapters/abstract/query_cache.rb +18 -14
  55. data/lib/active_record/connection_adapters/abstract/quoting.rb +107 -47
  56. data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
  57. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -110
  58. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +211 -90
  59. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +2 -4
  60. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +385 -144
  61. data/lib/active_record/connection_adapters/abstract/transaction.rb +167 -69
  62. data/lib/active_record/connection_adapters/abstract_adapter.rb +229 -99
  63. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +243 -275
  64. data/lib/active_record/connection_adapters/column.rb +30 -12
  65. data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
  66. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
  67. data/lib/active_record/connection_adapters/mysql/column.rb +1 -1
  68. data/lib/active_record/connection_adapters/mysql/database_statements.rb +88 -32
  69. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +1 -2
  70. data/lib/active_record/connection_adapters/mysql/quoting.rb +59 -7
  71. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +34 -10
  72. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +48 -32
  73. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +18 -7
  74. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +142 -19
  75. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +14 -9
  76. data/lib/active_record/connection_adapters/mysql2_adapter.rb +53 -18
  77. data/lib/active_record/connection_adapters/pool_config.rb +73 -0
  78. data/lib/active_record/connection_adapters/pool_manager.rb +47 -0
  79. data/lib/active_record/connection_adapters/postgresql/column.rb +37 -28
  80. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +40 -54
  81. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -2
  82. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +1 -4
  83. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -5
  84. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +2 -2
  85. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +10 -2
  86. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
  87. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +1 -2
  88. data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +3 -4
  90. data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
  92. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +3 -4
  93. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +25 -7
  94. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
  95. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +9 -7
  96. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +15 -3
  97. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
  98. data/lib/active_record/connection_adapters/postgresql/quoting.rb +73 -10
  99. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +2 -2
  100. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +19 -4
  101. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +107 -91
  102. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +0 -1
  103. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +120 -100
  104. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +31 -26
  105. data/lib/active_record/connection_adapters/postgresql/utils.rb +0 -1
  106. data/lib/active_record/connection_adapters/postgresql_adapter.rb +225 -121
  107. data/lib/active_record/connection_adapters/schema_cache.rb +159 -21
  108. data/lib/active_record/connection_adapters/sql_type_metadata.rb +17 -6
  109. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +146 -0
  110. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +42 -7
  111. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +5 -1
  112. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +77 -13
  113. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +174 -186
  114. data/lib/active_record/connection_adapters/statement_pool.rb +0 -1
  115. data/lib/active_record/connection_adapters.rb +52 -0
  116. data/lib/active_record/connection_handling.rb +293 -33
  117. data/lib/active_record/core.rb +341 -99
  118. data/lib/active_record/counter_cache.rb +8 -30
  119. data/lib/active_record/database_configurations/connection_url_resolver.rb +99 -0
  120. data/lib/active_record/database_configurations/database_config.rb +80 -0
  121. data/lib/active_record/database_configurations/hash_config.rb +96 -0
  122. data/lib/active_record/database_configurations/url_config.rb +53 -0
  123. data/lib/active_record/database_configurations.rb +273 -0
  124. data/lib/active_record/delegated_type.rb +209 -0
  125. data/lib/active_record/destroy_association_async_job.rb +36 -0
  126. data/lib/active_record/dynamic_matchers.rb +3 -4
  127. data/lib/active_record/enum.rb +108 -36
  128. data/lib/active_record/errors.rb +62 -19
  129. data/lib/active_record/explain.rb +10 -6
  130. data/lib/active_record/explain_subscriber.rb +1 -1
  131. data/lib/active_record/fixture_set/file.rb +10 -17
  132. data/lib/active_record/fixture_set/model_metadata.rb +32 -0
  133. data/lib/active_record/fixture_set/render_context.rb +17 -0
  134. data/lib/active_record/fixture_set/table_row.rb +152 -0
  135. data/lib/active_record/fixture_set/table_rows.rb +46 -0
  136. data/lib/active_record/fixtures.rb +200 -481
  137. data/lib/active_record/gem_version.rb +4 -4
  138. data/lib/active_record/inheritance.rb +53 -24
  139. data/lib/active_record/insert_all.rb +212 -0
  140. data/lib/active_record/integration.rb +67 -17
  141. data/lib/active_record/internal_metadata.rb +28 -9
  142. data/lib/active_record/legacy_yaml_adapter.rb +7 -3
  143. data/lib/active_record/locking/optimistic.rb +37 -23
  144. data/lib/active_record/locking/pessimistic.rb +9 -5
  145. data/lib/active_record/log_subscriber.rb +35 -35
  146. data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
  147. data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
  148. data/lib/active_record/middleware/database_selector.rb +77 -0
  149. data/lib/active_record/migration/command_recorder.rb +96 -44
  150. data/lib/active_record/migration/compatibility.rb +145 -64
  151. data/lib/active_record/migration/join_table.rb +0 -1
  152. data/lib/active_record/migration.rb +206 -157
  153. data/lib/active_record/model_schema.rb +148 -22
  154. data/lib/active_record/nested_attributes.rb +4 -7
  155. data/lib/active_record/no_touching.rb +8 -1
  156. data/lib/active_record/null_relation.rb +0 -1
  157. data/lib/active_record/persistence.rb +267 -59
  158. data/lib/active_record/query_cache.rb +21 -4
  159. data/lib/active_record/querying.rb +40 -23
  160. data/lib/active_record/railtie.rb +113 -74
  161. data/lib/active_record/railties/console_sandbox.rb +2 -4
  162. data/lib/active_record/railties/controller_runtime.rb +30 -35
  163. data/lib/active_record/railties/databases.rake +411 -80
  164. data/lib/active_record/readonly_attributes.rb +4 -0
  165. data/lib/active_record/reflection.rb +109 -93
  166. data/lib/active_record/relation/batches/batch_enumerator.rb +25 -9
  167. data/lib/active_record/relation/batches.rb +44 -35
  168. data/lib/active_record/relation/calculations.rb +157 -90
  169. data/lib/active_record/relation/delegation.rb +35 -50
  170. data/lib/active_record/relation/finder_methods.rb +64 -39
  171. data/lib/active_record/relation/from_clause.rb +5 -1
  172. data/lib/active_record/relation/merger.rb +32 -40
  173. data/lib/active_record/relation/predicate_builder/array_handler.rb +13 -13
  174. data/lib/active_record/relation/predicate_builder/association_query_value.rb +5 -9
  175. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +1 -2
  176. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +11 -10
  177. data/lib/active_record/relation/predicate_builder/range_handler.rb +3 -23
  178. data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
  179. data/lib/active_record/relation/predicate_builder.rb +62 -45
  180. data/lib/active_record/relation/query_attribute.rb +13 -8
  181. data/lib/active_record/relation/query_methods.rb +478 -187
  182. data/lib/active_record/relation/record_fetch_warning.rb +3 -3
  183. data/lib/active_record/relation/spawn_methods.rb +9 -9
  184. data/lib/active_record/relation/where_clause.rb +115 -62
  185. data/lib/active_record/relation.rb +379 -115
  186. data/lib/active_record/result.rb +64 -38
  187. data/lib/active_record/runtime_registry.rb +2 -2
  188. data/lib/active_record/sanitization.rb +22 -41
  189. data/lib/active_record/schema.rb +2 -11
  190. data/lib/active_record/schema_dumper.rb +54 -9
  191. data/lib/active_record/schema_migration.rb +7 -9
  192. data/lib/active_record/scoping/default.rb +4 -8
  193. data/lib/active_record/scoping/named.rb +17 -24
  194. data/lib/active_record/scoping.rb +8 -9
  195. data/lib/active_record/secure_token.rb +16 -8
  196. data/lib/active_record/serialization.rb +5 -3
  197. data/lib/active_record/signed_id.rb +116 -0
  198. data/lib/active_record/statement_cache.rb +49 -6
  199. data/lib/active_record/store.rb +94 -10
  200. data/lib/active_record/suppressor.rb +2 -2
  201. data/lib/active_record/table_metadata.rb +42 -43
  202. data/lib/active_record/tasks/database_tasks.rb +277 -81
  203. data/lib/active_record/tasks/mysql_database_tasks.rb +37 -39
  204. data/lib/active_record/tasks/postgresql_database_tasks.rb +27 -32
  205. data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -17
  206. data/lib/active_record/test_databases.rb +24 -0
  207. data/lib/active_record/test_fixtures.rb +291 -0
  208. data/lib/active_record/timestamp.rb +43 -32
  209. data/lib/active_record/touch_later.rb +23 -22
  210. data/lib/active_record/transactions.rb +62 -118
  211. data/lib/active_record/translation.rb +1 -1
  212. data/lib/active_record/type/adapter_specific_registry.rb +3 -13
  213. data/lib/active_record/type/hash_lookup_type_map.rb +0 -1
  214. data/lib/active_record/type/serialized.rb +6 -3
  215. data/lib/active_record/type/time.rb +10 -0
  216. data/lib/active_record/type/type_map.rb +0 -1
  217. data/lib/active_record/type/unsigned_integer.rb +0 -1
  218. data/lib/active_record/type.rb +10 -5
  219. data/lib/active_record/type_caster/connection.rb +15 -15
  220. data/lib/active_record/type_caster/map.rb +8 -8
  221. data/lib/active_record/validations/associated.rb +1 -2
  222. data/lib/active_record/validations/numericality.rb +35 -0
  223. data/lib/active_record/validations/uniqueness.rb +38 -30
  224. data/lib/active_record/validations.rb +4 -3
  225. data/lib/active_record.rb +13 -12
  226. data/lib/arel/alias_predication.rb +9 -0
  227. data/lib/arel/attributes/attribute.rb +41 -0
  228. data/lib/arel/collectors/bind.rb +29 -0
  229. data/lib/arel/collectors/composite.rb +39 -0
  230. data/lib/arel/collectors/plain_string.rb +20 -0
  231. data/lib/arel/collectors/sql_string.rb +27 -0
  232. data/lib/arel/collectors/substitute_binds.rb +35 -0
  233. data/lib/arel/crud.rb +42 -0
  234. data/lib/arel/delete_manager.rb +18 -0
  235. data/lib/arel/errors.rb +9 -0
  236. data/lib/arel/expressions.rb +29 -0
  237. data/lib/arel/factory_methods.rb +49 -0
  238. data/lib/arel/insert_manager.rb +49 -0
  239. data/lib/arel/math.rb +45 -0
  240. data/lib/arel/nodes/and.rb +32 -0
  241. data/lib/arel/nodes/ascending.rb +23 -0
  242. data/lib/arel/nodes/binary.rb +126 -0
  243. data/lib/arel/nodes/bind_param.rb +44 -0
  244. data/lib/arel/nodes/case.rb +55 -0
  245. data/lib/arel/nodes/casted.rb +62 -0
  246. data/lib/arel/nodes/comment.rb +29 -0
  247. data/lib/arel/nodes/count.rb +12 -0
  248. data/lib/arel/nodes/delete_statement.rb +45 -0
  249. data/lib/arel/nodes/descending.rb +23 -0
  250. data/lib/arel/nodes/equality.rb +15 -0
  251. data/lib/arel/nodes/extract.rb +24 -0
  252. data/lib/arel/nodes/false.rb +16 -0
  253. data/lib/arel/nodes/full_outer_join.rb +8 -0
  254. data/lib/arel/nodes/function.rb +44 -0
  255. data/lib/arel/nodes/grouping.rb +11 -0
  256. data/lib/arel/nodes/homogeneous_in.rb +76 -0
  257. data/lib/arel/nodes/in.rb +15 -0
  258. data/lib/arel/nodes/infix_operation.rb +92 -0
  259. data/lib/arel/nodes/inner_join.rb +8 -0
  260. data/lib/arel/nodes/insert_statement.rb +37 -0
  261. data/lib/arel/nodes/join_source.rb +20 -0
  262. data/lib/arel/nodes/matches.rb +18 -0
  263. data/lib/arel/nodes/named_function.rb +23 -0
  264. data/lib/arel/nodes/node.rb +51 -0
  265. data/lib/arel/nodes/node_expression.rb +13 -0
  266. data/lib/arel/nodes/ordering.rb +27 -0
  267. data/lib/arel/nodes/outer_join.rb +8 -0
  268. data/lib/arel/nodes/over.rb +15 -0
  269. data/lib/arel/nodes/regexp.rb +16 -0
  270. data/lib/arel/nodes/right_outer_join.rb +8 -0
  271. data/lib/arel/nodes/select_core.rb +67 -0
  272. data/lib/arel/nodes/select_statement.rb +41 -0
  273. data/lib/arel/nodes/sql_literal.rb +19 -0
  274. data/lib/arel/nodes/string_join.rb +11 -0
  275. data/lib/arel/nodes/table_alias.rb +31 -0
  276. data/lib/arel/nodes/terminal.rb +16 -0
  277. data/lib/arel/nodes/true.rb +16 -0
  278. data/lib/arel/nodes/unary.rb +44 -0
  279. data/lib/arel/nodes/unary_operation.rb +20 -0
  280. data/lib/arel/nodes/unqualified_column.rb +22 -0
  281. data/lib/arel/nodes/update_statement.rb +41 -0
  282. data/lib/arel/nodes/values_list.rb +9 -0
  283. data/lib/arel/nodes/window.rb +126 -0
  284. data/lib/arel/nodes/with.rb +11 -0
  285. data/lib/arel/nodes.rb +70 -0
  286. data/lib/arel/order_predications.rb +13 -0
  287. data/lib/arel/predications.rb +250 -0
  288. data/lib/arel/select_manager.rb +270 -0
  289. data/lib/arel/table.rb +118 -0
  290. data/lib/arel/tree_manager.rb +72 -0
  291. data/lib/arel/update_manager.rb +34 -0
  292. data/lib/arel/visitors/dot.rb +308 -0
  293. data/lib/arel/visitors/mysql.rb +93 -0
  294. data/lib/arel/visitors/postgresql.rb +120 -0
  295. data/lib/arel/visitors/sqlite.rb +38 -0
  296. data/lib/arel/visitors/to_sql.rb +899 -0
  297. data/lib/arel/visitors/visitor.rb +45 -0
  298. data/lib/arel/visitors.rb +13 -0
  299. data/lib/arel/window_predications.rb +9 -0
  300. data/lib/arel.rb +54 -0
  301. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
  302. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -5
  303. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +3 -1
  304. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +7 -5
  305. data/lib/rails/generators/active_record/migration.rb +19 -2
  306. data/lib/rails/generators/active_record/model/model_generator.rb +39 -2
  307. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
  308. data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
  309. metadata +118 -32
  310. data/lib/active_record/attribute_decorators.rb +0 -90
  311. data/lib/active_record/collection_cache_key.rb +0 -53
  312. data/lib/active_record/connection_adapters/connection_specification.rb +0 -287
  313. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -33
  314. data/lib/active_record/define_callbacks.rb +0 -22
  315. data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -19
  316. data/lib/active_record/relation/where_clause_factory.rb +0 -34
@@ -2,6 +2,10 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module SecureToken
5
+ class MinimumLengthError < StandardError; end
6
+
7
+ MINIMUM_TOKEN_LENGTH = 24
8
+
5
9
  extend ActiveSupport::Concern
6
10
 
7
11
  module ClassMethods
@@ -10,30 +14,34 @@ module ActiveRecord
10
14
  # # Schema: User(token:string, auth_token:string)
11
15
  # class User < ActiveRecord::Base
12
16
  # has_secure_token
13
- # has_secure_token :auth_token
17
+ # has_secure_token :auth_token, length: 36
14
18
  # end
15
19
  #
16
20
  # user = User.new
17
21
  # user.save
18
22
  # user.token # => "pX27zsMN2ViQKta1bGfLmVJE"
19
- # user.auth_token # => "77TMHrHJFvFDwodq8w7Ev2m7"
23
+ # user.auth_token # => "tU9bLuZseefXQ4yQxQo8wjtBvsAfPc78os6R"
20
24
  # user.regenerate_token # => true
21
25
  # user.regenerate_auth_token # => true
22
26
  #
23
- # <tt>SecureRandom::base58</tt> is used to generate the 24-character unique token, so collisions are highly unlikely.
27
+ # <tt>SecureRandom::base58</tt> is used to generate at minimum a 24-character unique token, so collisions are highly unlikely.
24
28
  #
25
29
  # Note that it's still possible to generate a race condition in the database in the same way that
26
30
  # {validates_uniqueness_of}[rdoc-ref:Validations::ClassMethods#validates_uniqueness_of] can.
27
31
  # You're encouraged to add a unique index in the database to deal with this even more unlikely scenario.
28
- def has_secure_token(attribute = :token)
32
+ def has_secure_token(attribute = :token, length: MINIMUM_TOKEN_LENGTH)
33
+ if length < MINIMUM_TOKEN_LENGTH
34
+ raise MinimumLengthError, "Token requires a minimum length of #{MINIMUM_TOKEN_LENGTH} characters."
35
+ end
36
+
29
37
  # Load securerandom only when has_secure_token is used.
30
38
  require "active_support/core_ext/securerandom"
31
- define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_secure_token }
32
- before_create { send("#{attribute}=", self.class.generate_unique_secure_token) unless send("#{attribute}?") }
39
+ define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_secure_token(length: length) }
40
+ before_create { send("#{attribute}=", self.class.generate_unique_secure_token(length: length)) unless send("#{attribute}?") }
33
41
  end
34
42
 
35
- def generate_unique_secure_token
36
- SecureRandom.base58(24)
43
+ def generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH)
44
+ SecureRandom.base58(length)
37
45
  end
38
46
  end
39
47
  end
@@ -11,10 +11,12 @@ module ActiveRecord #:nodoc:
11
11
  end
12
12
 
13
13
  def serializable_hash(options = nil)
14
- options = options.try(:dup) || {}
14
+ if self.class._has_attribute?(self.class.inheritance_column)
15
+ options = options ? options.dup : {}
15
16
 
16
- options[:except] = Array(options[:except]).map(&:to_s)
17
- options[:except] |= Array(self.class.inheritance_column)
17
+ options[:except] = Array(options[:except]).map(&:to_s)
18
+ options[:except] |= Array(self.class.inheritance_column)
19
+ end
18
20
 
19
21
  super(options)
20
22
  end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ # = Active Record Signed Id
5
+ module SignedId
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ ##
10
+ # :singleton-method:
11
+ # Set the secret used for the signed id verifier instance when using Active Record outside of Rails.
12
+ # Within Rails, this is automatically set using the Rails application key generator.
13
+ mattr_accessor :signed_id_verifier_secret, instance_writer: false
14
+ end
15
+
16
+ module ClassMethods
17
+ # Lets you find a record based on a signed id that's safe to put into the world without risk of tampering.
18
+ # This is particularly useful for things like password reset or email verification, where you want
19
+ # the bearer of the signed id to be able to interact with the underlying record, but usually only within
20
+ # a certain time period.
21
+ #
22
+ # You set the time period that the signed id is valid for during generation, using the instance method
23
+ # <tt>signed_id(expires_in: 15.minutes)</tt>. If the time has elapsed before a signed find is attempted,
24
+ # the signed id will no longer be valid, and nil is returned.
25
+ #
26
+ # It's possible to further restrict the use of a signed id with a purpose. This helps when you have a
27
+ # general base model, like a User, which might have signed ids for several things, like password reset
28
+ # or email verification. The purpose that was set during generation must match the purpose set when
29
+ # finding. If there's a mismatch, nil is again returned.
30
+ #
31
+ # ==== Examples
32
+ #
33
+ # signed_id = User.first.signed_id expires_in: 15.minutes, purpose: :password_reset
34
+ #
35
+ # User.find_signed signed_id # => nil, since the purpose does not match
36
+ #
37
+ # travel 16.minutes
38
+ # User.find_signed signed_id, purpose: :password_reset # => nil, since the signed id has expired
39
+ #
40
+ # travel_back
41
+ # User.find_signed signed_id, purpose: :password_reset # => User.first
42
+ def find_signed(signed_id, purpose: nil)
43
+ raise UnknownPrimaryKey.new(self) if primary_key.nil?
44
+
45
+ if id = signed_id_verifier.verified(signed_id, purpose: combine_signed_id_purposes(purpose))
46
+ find_by primary_key => id
47
+ end
48
+ end
49
+
50
+ # Works like +find_signed+, but will raise an +ActiveSupport::MessageVerifier::InvalidSignature+
51
+ # exception if the +signed_id+ has either expired, has a purpose mismatch, is for another record,
52
+ # or has been tampered with. It will also raise an +ActiveRecord::RecordNotFound+ exception if
53
+ # the valid signed id can't find a record.
54
+ #
55
+ # === Examples
56
+ #
57
+ # User.find_signed! "bad data" # => ActiveSupport::MessageVerifier::InvalidSignature
58
+ #
59
+ # signed_id = User.first.signed_id
60
+ # User.first.destroy
61
+ # User.find_signed! signed_id # => ActiveRecord::RecordNotFound
62
+ def find_signed!(signed_id, purpose: nil)
63
+ if id = signed_id_verifier.verify(signed_id, purpose: combine_signed_id_purposes(purpose))
64
+ find(id)
65
+ end
66
+ end
67
+
68
+ # The verifier instance that all signed ids are generated and verified from. By default, it'll be initialized
69
+ # with the class-level +signed_id_verifier_secret+, which within Rails comes from the
70
+ # Rails.application.key_generator. By default, it's SHA256 for the digest and JSON for the serialization.
71
+ def signed_id_verifier
72
+ @signed_id_verifier ||= begin
73
+ secret = signed_id_verifier_secret
74
+ secret = secret.call if secret.respond_to?(:call)
75
+
76
+ if secret.nil?
77
+ raise ArgumentError, "You must set ActiveRecord::Base.signed_id_verifier_secret to use signed ids"
78
+ else
79
+ ActiveSupport::MessageVerifier.new secret, digest: "SHA256", serializer: JSON
80
+ end
81
+ end
82
+ end
83
+
84
+ # Allows you to pass in a custom verifier used for the signed ids. This also allows you to use different
85
+ # verifiers for different classes. This is also helpful if you need to rotate keys, as you can prepare
86
+ # your custom verifier for that in advance. See +ActiveSupport::MessageVerifier+ for details.
87
+ def signed_id_verifier=(verifier)
88
+ @signed_id_verifier = verifier
89
+ end
90
+
91
+ # :nodoc:
92
+ def combine_signed_id_purposes(purpose)
93
+ [ base_class.name.underscore, purpose.to_s ].compact_blank.join("/")
94
+ end
95
+ end
96
+
97
+
98
+ # Returns a signed id that's generated using a preconfigured +ActiveSupport::MessageVerifier+ instance.
99
+ # This signed id is tamper proof, so it's safe to send in an email or otherwise share with the outside world.
100
+ # It can further more be set to expire (the default is not to expire), and scoped down with a specific purpose.
101
+ # If the expiration date has been exceeded before +find_signed+ is called, the id won't find the designated
102
+ # record. If a purpose is set, this too must match.
103
+ #
104
+ # If you accidentally let a signed id out in the wild that you wish to retract sooner than its expiration date
105
+ # (or maybe you forgot to set an expiration date while meaning to!), you can use the purpose to essentially
106
+ # version the signed_id, like so:
107
+ #
108
+ # user.signed_id purpose: :v2
109
+ #
110
+ # And you then change your +find_signed+ calls to require this new purpose. Any old signed ids that were not
111
+ # created with the purpose will no longer find the record.
112
+ def signed_id(expires_in: nil, purpose: nil)
113
+ self.class.signed_id_verifier.generate id, expires_in: expires_in, purpose: self.class.combine_signed_id_purposes(purpose)
114
+ end
115
+ end
116
+ end
@@ -44,18 +44,56 @@ module ActiveRecord
44
44
  def initialize(values)
45
45
  @values = values
46
46
  @indexes = values.each_with_index.find_all { |thing, i|
47
- Arel::Nodes::BindParam === thing
47
+ Substitute === thing
48
48
  }.map(&:last)
49
49
  end
50
50
 
51
51
  def sql_for(binds, connection)
52
52
  val = @values.dup
53
- casted_binds = binds.map(&:value_for_database)
54
- @indexes.each { |i| val[i] = connection.quote(casted_binds.shift) }
53
+ @indexes.each do |i|
54
+ value = binds.shift
55
+ if ActiveModel::Attribute === value
56
+ value = value.value_for_database
57
+ end
58
+ val[i] = connection.quote(value)
59
+ end
55
60
  val.join
56
61
  end
57
62
  end
58
63
 
64
+ class PartialQueryCollector
65
+ attr_accessor :preparable
66
+
67
+ def initialize
68
+ @parts = []
69
+ @binds = []
70
+ end
71
+
72
+ def <<(str)
73
+ @parts << str
74
+ self
75
+ end
76
+
77
+ def add_bind(obj)
78
+ @binds << obj
79
+ @parts << Substitute.new
80
+ self
81
+ end
82
+
83
+ def add_binds(binds, proc_for_binds = nil)
84
+ @binds.concat proc_for_binds ? binds.map(&proc_for_binds) : binds
85
+ binds.size.times do |i|
86
+ @parts << ", " unless i == 0
87
+ @parts << Substitute.new
88
+ end
89
+ self
90
+ end
91
+
92
+ def value
93
+ [@parts, @binds]
94
+ end
95
+ end
96
+
59
97
  def self.query(sql)
60
98
  Query.new(sql)
61
99
  end
@@ -64,6 +102,10 @@ module ActiveRecord
64
102
  PartialQuery.new(values)
65
103
  end
66
104
 
105
+ def self.partial_query_collector
106
+ PartialQueryCollector.new
107
+ end
108
+
67
109
  class Params # :nodoc:
68
110
  def bind; Substitute.new; end
69
111
  end
@@ -74,7 +116,7 @@ module ActiveRecord
74
116
  @bound_attributes = bound_attributes
75
117
 
76
118
  bound_attributes.each_with_index do |attr, i|
77
- if Substitute === attr.value
119
+ if ActiveModel::Attribute === attr && Substitute === attr.value
78
120
  @indexes << i
79
121
  end
80
122
  end
@@ -106,6 +148,8 @@ module ActiveRecord
106
148
  sql = query_builder.sql_for bind_values, connection
107
149
 
108
150
  klass.find_by_sql(sql, bind_values, preparable: true, &block)
151
+ rescue ::RangeError
152
+ []
109
153
  end
110
154
 
111
155
  def self.unsupported_value?(value)
@@ -114,8 +158,7 @@ module ActiveRecord
114
158
  end
115
159
  end
116
160
 
117
- protected
118
-
161
+ private
119
162
  attr_reader :query_builder, :bind_map, :klass
120
163
  end
121
164
  end
@@ -11,14 +11,20 @@ module ActiveRecord
11
11
  # of the model. This is very helpful for easily exposing store keys to a form or elsewhere that's
12
12
  # already built around just accessing attributes on the model.
13
13
  #
14
+ # Every accessor comes with dirty tracking methods (+key_changed?+, +key_was+ and +key_change+) and
15
+ # methods to access the changes made during the last save (+saved_change_to_key?+, +saved_change_to_key+ and
16
+ # +key_before_last_save+).
17
+ #
18
+ # NOTE: There is no +key_will_change!+ method for accessors, use +store_will_change!+ instead.
19
+ #
14
20
  # Make sure that you declare the database column used for the serialized store as a text, so there's
15
21
  # plenty of room.
16
22
  #
17
23
  # You can set custom coder to encode/decode your serialized attributes to/from different formats.
18
24
  # JSON, YAML, Marshal are supported out of the box. Generally it can be any wrapper that provides +load+ and +dump+.
19
25
  #
20
- # NOTE: If you are using PostgreSQL specific columns like +hstore+ or +json+ there is no need for
21
- # the serialization provided by {.store}[rdoc-ref:rdoc-ref:ClassMethods#store].
26
+ # NOTE: If you are using structured database data types (e.g. PostgreSQL +hstore+/+json+, or MySQL 5.7+
27
+ # +json+) there is no need for the serialization provided by {.store}[rdoc-ref:rdoc-ref:ClassMethods#store].
22
28
  # Simply use {.store_accessor}[rdoc-ref:ClassMethods#store_accessor] instead to generate
23
29
  # the accessor methods. Be aware that these columns use a string keyed hash and do not allow access
24
30
  # using a symbol.
@@ -31,24 +37,40 @@ module ActiveRecord
31
37
  #
32
38
  # class User < ActiveRecord::Base
33
39
  # store :settings, accessors: [ :color, :homepage ], coder: JSON
40
+ # store :parent, accessors: [ :name ], coder: JSON, prefix: true
41
+ # store :spouse, accessors: [ :name ], coder: JSON, prefix: :partner
42
+ # store :settings, accessors: [ :two_factor_auth ], suffix: true
43
+ # store :settings, accessors: [ :login_retry ], suffix: :config
34
44
  # end
35
45
  #
36
- # u = User.new(color: 'black', homepage: '37signals.com')
46
+ # u = User.new(color: 'black', homepage: '37signals.com', parent_name: 'Mary', partner_name: 'Lily')
37
47
  # u.color # Accessor stored attribute
48
+ # u.parent_name # Accessor stored attribute with prefix
49
+ # u.partner_name # Accessor stored attribute with custom prefix
50
+ # u.two_factor_auth_settings # Accessor stored attribute with suffix
51
+ # u.login_retry_config # Accessor stored attribute with custom suffix
38
52
  # u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
39
53
  #
40
54
  # # There is no difference between strings and symbols for accessing custom attributes
41
55
  # u.settings[:country] # => 'Denmark'
42
56
  # u.settings['country'] # => 'Denmark'
43
57
  #
58
+ # # Dirty tracking
59
+ # u.color = 'green'
60
+ # u.color_changed? # => true
61
+ # u.color_was # => 'black'
62
+ # u.color_change # => ['black', 'red']
63
+ #
44
64
  # # Add additional accessors to an existing store through store_accessor
45
65
  # class SuperUser < User
46
66
  # store_accessor :settings, :privileges, :servants
67
+ # store_accessor :parent, :birthday, prefix: true
68
+ # store_accessor :settings, :secret_question, suffix: :config
47
69
  # end
48
70
  #
49
71
  # The stored attribute names can be retrieved using {.stored_attributes}[rdoc-ref:rdoc-ref:ClassMethods#stored_attributes].
50
72
  #
51
- # User.stored_attributes[:settings] # [:color, :homepage]
73
+ # User.stored_attributes[:settings] # [:color, :homepage, :two_factor_auth, :login_retry]
52
74
  #
53
75
  # == Overwriting default accessors
54
76
  #
@@ -81,21 +103,78 @@ module ActiveRecord
81
103
  module ClassMethods
82
104
  def store(store_attribute, options = {})
83
105
  serialize store_attribute, IndifferentCoder.new(store_attribute, options[:coder])
84
- store_accessor(store_attribute, options[:accessors]) if options.has_key? :accessors
106
+ store_accessor(store_attribute, options[:accessors], **options.slice(:prefix, :suffix)) if options.has_key? :accessors
85
107
  end
86
108
 
87
- def store_accessor(store_attribute, *keys)
109
+ def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
88
110
  keys = keys.flatten
89
111
 
112
+ accessor_prefix =
113
+ case prefix
114
+ when String, Symbol
115
+ "#{prefix}_"
116
+ when TrueClass
117
+ "#{store_attribute}_"
118
+ else
119
+ ""
120
+ end
121
+ accessor_suffix =
122
+ case suffix
123
+ when String, Symbol
124
+ "_#{suffix}"
125
+ when TrueClass
126
+ "_#{store_attribute}"
127
+ else
128
+ ""
129
+ end
130
+
90
131
  _store_accessors_module.module_eval do
91
132
  keys.each do |key|
92
- define_method("#{key}=") do |value|
133
+ accessor_key = "#{accessor_prefix}#{key}#{accessor_suffix}"
134
+
135
+ define_method("#{accessor_key}=") do |value|
93
136
  write_store_attribute(store_attribute, key, value)
94
137
  end
95
138
 
96
- define_method(key) do
139
+ define_method(accessor_key) do
97
140
  read_store_attribute(store_attribute, key)
98
141
  end
142
+
143
+ define_method("#{accessor_key}_changed?") do
144
+ return false unless attribute_changed?(store_attribute)
145
+ prev_store, new_store = changes[store_attribute]
146
+ prev_store&.dig(key) != new_store&.dig(key)
147
+ end
148
+
149
+ define_method("#{accessor_key}_change") do
150
+ return unless attribute_changed?(store_attribute)
151
+ prev_store, new_store = changes[store_attribute]
152
+ [prev_store&.dig(key), new_store&.dig(key)]
153
+ end
154
+
155
+ define_method("#{accessor_key}_was") do
156
+ return unless attribute_changed?(store_attribute)
157
+ prev_store, _new_store = changes[store_attribute]
158
+ prev_store&.dig(key)
159
+ end
160
+
161
+ define_method("saved_change_to_#{accessor_key}?") do
162
+ return false unless saved_change_to_attribute?(store_attribute)
163
+ prev_store, new_store = saved_change_to_attribute(store_attribute)
164
+ prev_store&.dig(key) != new_store&.dig(key)
165
+ end
166
+
167
+ define_method("saved_change_to_#{accessor_key}") do
168
+ return unless saved_change_to_attribute?(store_attribute)
169
+ prev_store, new_store = saved_change_to_attribute(store_attribute)
170
+ [prev_store&.dig(key), new_store&.dig(key)]
171
+ end
172
+
173
+ define_method("#{accessor_key}_before_last_save") do
174
+ return unless saved_change_to_attribute?(store_attribute)
175
+ prev_store, _new_store = saved_change_to_attribute(store_attribute)
176
+ prev_store&.dig(key)
177
+ end
99
178
  end
100
179
  end
101
180
 
@@ -172,7 +251,7 @@ module ActiveRecord
172
251
  attribute = object.send(store_attribute)
173
252
  unless attribute.is_a?(ActiveSupport::HashWithIndifferentAccess)
174
253
  attribute = IndifferentCoder.as_indifferent_hash(attribute)
175
- object.send :"#{store_attribute}=", attribute
254
+ object.public_send :"#{store_attribute}=", attribute
176
255
  end
177
256
  attribute
178
257
  end
@@ -189,7 +268,7 @@ module ActiveRecord
189
268
  end
190
269
 
191
270
  def dump(obj)
192
- @coder.dump self.class.as_indifferent_hash(obj)
271
+ @coder.dump as_regular_hash(obj)
193
272
  end
194
273
 
195
274
  def load(yaml)
@@ -206,6 +285,11 @@ module ActiveRecord
206
285
  ActiveSupport::HashWithIndifferentAccess.new
207
286
  end
208
287
  end
288
+
289
+ private
290
+ def as_regular_hash(obj)
291
+ obj.respond_to?(:to_hash) ? obj.to_hash : {}
292
+ end
209
293
  end
210
294
  end
211
295
  end
@@ -40,11 +40,11 @@ module ActiveRecord
40
40
  end
41
41
  end
42
42
 
43
- def save(*) # :nodoc:
43
+ def save(**) # :nodoc:
44
44
  SuppressorRegistry.suppressed[self.class.name] ? true : super
45
45
  end
46
46
 
47
- def save!(*) # :nodoc:
47
+ def save!(**) # :nodoc:
48
48
  SuppressorRegistry.suppressed[self.class.name] ? true : super
49
49
  end
50
50
  end
@@ -2,81 +2,80 @@
2
2
 
3
3
  module ActiveRecord
4
4
  class TableMetadata # :nodoc:
5
- delegate :foreign_type, :foreign_key, :join_primary_key, :join_foreign_key, to: :association, prefix: true
5
+ delegate :join_primary_key, :join_foreign_key, :join_foreign_type, to: :reflection
6
6
 
7
- def initialize(klass, arel_table, association = nil)
7
+ def initialize(klass, arel_table, reflection = nil)
8
8
  @klass = klass
9
9
  @arel_table = arel_table
10
- @association = association
10
+ @reflection = reflection
11
11
  end
12
12
 
13
- def resolve_column_aliases(hash)
14
- new_hash = hash.dup
15
- hash.each do |key, _|
16
- if (key.is_a?(Symbol)) && klass.attribute_alias?(key)
17
- new_hash[klass.attribute_alias(key)] = new_hash.delete(key)
18
- end
19
- end
20
- new_hash
21
- end
22
-
23
- def arel_attribute(column_name)
24
- if klass
25
- klass.arel_attribute(column_name, arel_table)
26
- else
27
- arel_table[column_name]
28
- end
13
+ def primary_key
14
+ klass&.primary_key
29
15
  end
30
16
 
31
17
  def type(column_name)
32
- if klass
33
- klass.type_for_attribute(column_name)
34
- else
35
- Type.default_value
36
- end
18
+ arel_table.type_for_attribute(column_name)
37
19
  end
38
20
 
39
21
  def has_column?(column_name)
40
- klass && klass.columns_hash.key?(column_name.to_s)
22
+ klass&.columns_hash.key?(column_name)
41
23
  end
42
24
 
43
- def associated_with?(association_name)
44
- klass && klass._reflect_on_association(association_name)
25
+ def associated_with?(table_name)
26
+ klass&._reflect_on_association(table_name) || klass&._reflect_on_association(table_name.singularize)
45
27
  end
46
28
 
47
29
  def associated_table(table_name)
48
- association = klass._reflect_on_association(table_name) || klass._reflect_on_association(table_name.to_s.singularize)
30
+ reflection = klass._reflect_on_association(table_name) || klass._reflect_on_association(table_name.singularize)
49
31
 
50
- if !association && table_name == arel_table.name
32
+ if !reflection && table_name == arel_table.name
51
33
  return self
52
- elsif association && !association.polymorphic?
53
- association_klass = association.klass
54
- arel_table = association_klass.arel_table.alias(table_name)
34
+ end
35
+
36
+ if reflection
37
+ association_klass = reflection.klass unless reflection.polymorphic?
38
+ elsif block_given?
39
+ association_klass = yield table_name
40
+ end
41
+
42
+ if association_klass
43
+ arel_table = association_klass.arel_table
44
+ arel_table = arel_table.alias(table_name) if arel_table.name != table_name
45
+ TableMetadata.new(association_klass, arel_table, reflection)
55
46
  else
56
47
  type_caster = TypeCaster::Connection.new(klass, table_name)
57
- association_klass = nil
58
48
  arel_table = Arel::Table.new(table_name, type_caster: type_caster)
49
+ TableMetadata.new(nil, arel_table, reflection)
59
50
  end
60
-
61
- TableMetadata.new(association_klass, arel_table, association)
62
51
  end
63
52
 
64
53
  def polymorphic_association?
65
- association && association.polymorphic?
54
+ reflection&.polymorphic?
66
55
  end
67
56
 
68
- def aggregated_with?(aggregation_name)
69
- klass && reflect_on_aggregation(aggregation_name)
57
+ def through_association?
58
+ reflection&.through_reflection?
70
59
  end
71
60
 
72
61
  def reflect_on_aggregation(aggregation_name)
73
- klass.reflect_on_aggregation(aggregation_name)
62
+ klass&.reflect_on_aggregation(aggregation_name)
63
+ end
64
+ alias :aggregated_with? :reflect_on_aggregation
65
+
66
+ def predicate_builder
67
+ if klass
68
+ predicate_builder = klass.predicate_builder.dup
69
+ predicate_builder.instance_variable_set(:@table, self)
70
+ predicate_builder
71
+ else
72
+ PredicateBuilder.new(self)
73
+ end
74
74
  end
75
75
 
76
- # TODO Change this to private once we've dropped Ruby 2.2 support.
77
- # Workaround for Ruby 2.2 "private attribute?" warning.
78
- protected
76
+ attr_reader :arel_table
79
77
 
80
- attr_reader :klass, :arel_table, :association
78
+ private
79
+ attr_reader :klass, :reflection
81
80
  end
82
81
  end