From 0cdedb4f0b3561f2f182f830d3feea54064b582f Mon Sep 17 00:00:00 2001 From: dak2 Date: Thu, 26 Dec 2024 16:54:02 +0900 Subject: [PATCH] activerecord: Add types for create_with to singleton that inherited activerecord base Singleton that inherited activerecord base can handle create_with method. https://github.com/rails/rails/blob/6df15e25c875e9c1ee39acb0bc161b6c47531331/activerecord/test/cases/scoping/relation_scoping_test.rb#L499-L507 - create_with - https://api.rubyonrails.org/v6.0.6.1/classes/ActiveRecord/QueryMethods.html#method-i-create_with --- gems/activerecord/6.0/_test/activerecord-generated.rb | 2 ++ gems/activerecord/6.0/activerecord.rbs | 1 + gems/activerecord/6.1/_test/activerecord-6.1.rb | 2 ++ gems/activerecord/7.0/_test/test.rb | 2 ++ gems/activerecord/7.1/_test/test.rb | 2 ++ gems/activerecord/7.2/_test/test.rb | 2 ++ 6 files changed, 11 insertions(+) diff --git a/gems/activerecord/6.0/_test/activerecord-generated.rb b/gems/activerecord/6.0/_test/activerecord-generated.rb index dc38c721..aa15fa44 100644 --- a/gems/activerecord/6.0/_test/activerecord-generated.rb +++ b/gems/activerecord/6.0/_test/activerecord-generated.rb @@ -38,6 +38,8 @@ class Article < ActiveRecord::Base User.in_order_of(:id, [1, 5, 3]) User.offset(5).limit(10) User.count +User.create_with(name: 'name', age: 1) +User.create_with(nil) t = User.arel_table User.limit(10).select(:id, "name", t[:age].as("years"), t[:email]) diff --git a/gems/activerecord/6.0/activerecord.rbs b/gems/activerecord/6.0/activerecord.rbs index 1dffc368..16fc9408 100644 --- a/gems/activerecord/6.0/activerecord.rbs +++ b/gems/activerecord/6.0/activerecord.rbs @@ -51,6 +51,7 @@ module ActiveRecord def self.create!: (**untyped) -> instance def self.validate: (*untyped, ?if: condition[instance], ?unless: condition[instance], **untyped) -> void def self.validates: (*untyped, ?if: condition[instance], ?unless: condition[instance], **untyped) -> void + def self.create_with: (nil | Hash[untyped, untyped]) -> Relation # callbacks type callback[T] = Symbol | ^(T) [self: T] -> void diff --git a/gems/activerecord/6.1/_test/activerecord-6.1.rb b/gems/activerecord/6.1/_test/activerecord-6.1.rb index 4d4d3ad3..f6953c92 100644 --- a/gems/activerecord/6.1/_test/activerecord-6.1.rb +++ b/gems/activerecord/6.1/_test/activerecord-6.1.rb @@ -11,3 +11,5 @@ User.strict_loading(false) user.strict_loading! user.strict_loading? +User.create_with(secret: 'secret', key: 'key') +User.create_with(nil) diff --git a/gems/activerecord/7.0/_test/test.rb b/gems/activerecord/7.0/_test/test.rb index b5b05728..7c627a82 100644 --- a/gems/activerecord/7.0/_test/test.rb +++ b/gems/activerecord/7.0/_test/test.rb @@ -36,6 +36,8 @@ class Article < ActiveRecord::Base User.upsert_all([{ id: 1, name: 'James' }], returning: %i[id name], unique_by: :id, record_timestamps: true) User.strict_loading User.strict_loading(false) + User.create_with(name: 'name', age: 1) + User.create_with(nil) user = User.new(secret: 'dummy', key: 'dummy', token: 'dummy', phrase: 'dummy') user.encrypt user.encrypted_attribute?(:secret) diff --git a/gems/activerecord/7.1/_test/test.rb b/gems/activerecord/7.1/_test/test.rb index fa6a49ab..4cbcec91 100644 --- a/gems/activerecord/7.1/_test/test.rb +++ b/gems/activerecord/7.1/_test/test.rb @@ -44,6 +44,8 @@ class Article < ActiveRecord::Base User.with(admin_users: User.where(role: 0)) User.strict_loading User.strict_loading(false) + User.create_with(name: 'name', age: 1) + User.create_with(nil) user = User.new(secret: 'dummy', key: 'dummy', token: 'dummy', phrase: 'dummy') user.encrypt user.encrypted_attribute?(:secret) diff --git a/gems/activerecord/7.2/_test/test.rb b/gems/activerecord/7.2/_test/test.rb index f66b4f9a..a0575219 100644 --- a/gems/activerecord/7.2/_test/test.rb +++ b/gems/activerecord/7.2/_test/test.rb @@ -42,6 +42,8 @@ class Article < ApplicationRecord User.with(admin_users: User.where(role: 0)) User.strict_loading User.strict_loading(false) + User.create_with(name: 'name', age: 1) + User.create_with(nil) user = User.new(secret: 'dummy', key: 'dummy', token: 'dummy', phrase: 'dummy') user.encrypt user.encrypted_attribute?(:secret)