From eff8e5ea6c9b10ada3ae7daa85bad29a4e6a8648 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Thu, 12 Sep 2024 00:22:48 +0900 Subject: [PATCH 1/2] activerecord: Add ActiveRecord::Base.generates_tokens_for (#659) `ActiveRecord::Base.generates_token_for` has been added since v7.1. refs: * https://github.com/rails/rails/pull/44189 * https://github.com/rails/rails/blob/v7.1.4/activerecord/CHANGELOG.md --- gems/activerecord/7.1/_test/test.rb | 3 +++ gems/activerecord/7.1/activerecord-7.1.rbs | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/gems/activerecord/7.1/_test/test.rb b/gems/activerecord/7.1/_test/test.rb index 6aeb877d..f489f151 100644 --- a/gems/activerecord/7.1/_test/test.rb +++ b/gems/activerecord/7.1/_test/test.rb @@ -21,6 +21,8 @@ class User < ApplicationRecord encrypts :token, deterministic: true encrypts :phrase, ignore_case: true + generates_token_for :password_reset, expires_in: 15.minutes + normalizes :email, with: -> email { # @type var email: String email.strip.downcase @@ -50,6 +52,7 @@ class Article < ActiveRecord::Base user.articles.insert_all!([{ id: 1, name: 'James' }], returning: %i[id name], record_timestamps: true) user.articles.upsert({ id: 1, name: 'James' }, returning: %i[id name], unique_by: :id, record_timestamps: true) user.articles.upsert_all([{ id: 1, name: 'James' }], returning: %i[id name], unique_by: :id, record_timestamps: true) + user.generate_token_for(:password_reset) user = User.new user.normalize_attribute(:email) diff --git a/gems/activerecord/7.1/activerecord-7.1.rbs b/gems/activerecord/7.1/activerecord-7.1.rbs index 35fa1590..7aa5dc90 100644 --- a/gems/activerecord/7.1/activerecord-7.1.rbs +++ b/gems/activerecord/7.1/activerecord-7.1.rbs @@ -6,6 +6,8 @@ module ActiveRecord extend Normalization::ClassMethods include SecurePassword extend SecurePassword::ClassMethods + include TokenFor + extend TokenFor::ClassMethods end class AssociationRelation @@ -145,6 +147,16 @@ module ActiveRecord def authenticate_by: (untyped attributes) -> instance? end end + + module TokenFor + module ClassMethods + def generates_token_for: (Symbol purpose, ?expires_in: ActiveSupport::Duration) ? { () [self: instance] -> untyped } -> untyped + def find_by_token_for: (Symbol purpose, String token) -> untyped + def find_by_token_for!: (Symbol purpose, String token) -> untyped + end + + def generate_token_for: (Symbol purpose) -> String + end end module ActiveRecord From 2275d263564ec90f749ea9ff8fce8160b9ed58e9 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Thu, 12 Sep 2024 17:20:53 +0900 Subject: [PATCH 2/2] activerecord: Add ActiveRecord::Querying#with_recursive (#662) refs: * https://github.com/rails/rails/pull/51601 * https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-with_recursive --- gems/activerecord/7.2/_test/test.rb | 1 + gems/activerecord/7.2/activerecord-7.2.rbs | 32 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/gems/activerecord/7.2/_test/test.rb b/gems/activerecord/7.2/_test/test.rb index c4533a8e..0c2b1dc2 100644 --- a/gems/activerecord/7.2/_test/test.rb +++ b/gems/activerecord/7.2/_test/test.rb @@ -42,4 +42,5 @@ class User < ApplicationRecord user = User.new user.normalize_attribute(:email) User.normalize_value_for(:email, ' CRUISE-CONTROL@EXAMPLE.COM\n') + User.with_recursive(admin_users: User.where(role: 0)) end diff --git a/gems/activerecord/7.2/activerecord-7.2.rbs b/gems/activerecord/7.2/activerecord-7.2.rbs index ca09f305..adcd6f13 100644 --- a/gems/activerecord/7.2/activerecord-7.2.rbs +++ b/gems/activerecord/7.2/activerecord-7.2.rbs @@ -6,6 +6,13 @@ module ActiveRecord extend Normalization::ClassMethods end + class Relation + module QueryMethods + def with_recursive: (*untyped) -> untyped + def with_recursive!: (*untyped) -> untyped + end + end + module Inheritance module ClassMethods def primary_abstract_class: () -> void @@ -83,8 +90,8 @@ module ActiveRecord class MismatchedForeignKey < StatementInvalid def initialize: (?message: untyped?, ?sql: untyped?, ?binds: untyped?, ?table: untyped?, - ?foreign_key: untyped?, ?target_table: untyped?, ?primary_key: untyped?, - ?primary_key_column: untyped?, ?query_parser: untyped?, + ?foreign_key: untyped?, ?target_table: untyped?, ?primary_key: untyped?, + ?primary_key_column: untyped?, ?query_parser: untyped?, ?connection_pool: ConnectionAdapters::ConnectionPool?) -> void end @@ -106,11 +113,15 @@ module ActiveRecord end end + module Querying + def with_recursive: (*untyped) -> untyped + end + class InsertAll @record_timestamps: bool def initialize: (untyped model, untyped inserts, on_duplicate: untyped, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped - + def record_timestamps?: () -> bool def keys_including_timestamps: () -> Set[String] @@ -118,3 +129,18 @@ module ActiveRecord def timestamps_for_create: () -> Hash[String, String] end end + +module ActiveRecord + class Relation + module Methods[Model, PrimaryKey] + def with_recursive: (*untyped) -> self + def with_recursive!: (*untyped) -> self + end + end + + class Base + module ClassMethods[Model, Relation, PrimaryKey] + def with_recursive: (*untyped) -> Relation + end + end +end