From 4bd699c198c6d76725ff4bbb23d70308e909b069 Mon Sep 17 00:00:00 2001 From: dak2 Date: Sat, 21 Dec 2024 16:14:04 +0900 Subject: [PATCH] activerecord: Add types for strict_loading - strict_loading(value = true) - https://api.rubyonrails.org/v6.1.7.3/classes/ActiveRecord/QueryMethods.html#method-i-strict_loading - strict_loading?() - https://api.rubyonrails.org/v6.1.7.3/classes/ActiveRecord/Core.html#method-i-strict_loading-3F - strict_loading!() - https://api.rubyonrails.org/v6.1.7.3/classes/ActiveRecord/Core.html#method-i-strict_loading-21 * Arguments have been added to strict_loading! since 7.0. - up to 6.1 - https://api.rubyonrails.org/v6.1.7.3/classes/ActiveRecord/Core.html#method-i-strict_loading-21 - since 7.0 - https://api.rubyonrails.org/v7.0/classes/ActiveRecord/Core.html#method-i-strict_loading-21 This methods have been added since 6.1. https://github.com/rails/rails/pull/37400 --- gems/activerecord/6.1/_test/activerecord-6.1.rb | 4 ++++ gems/activerecord/6.1/activerecord-6.1.rbs | 6 ++++++ gems/activerecord/7.0/_test/test.rb | 5 +++++ gems/activerecord/7.0/activerecord-7.0.rbs | 6 ++++++ gems/activerecord/7.1/_test/test.rb | 5 +++++ gems/activerecord/7.1/activerecord-7.1.rbs | 6 ++++++ gems/activerecord/7.2/_test/test.rb | 5 +++++ gems/activerecord/7.2/activerecord-7.2.rbs | 6 ++++++ 8 files changed, 43 insertions(+) diff --git a/gems/activerecord/6.1/_test/activerecord-6.1.rb b/gems/activerecord/6.1/_test/activerecord-6.1.rb index a7a8275c..4d4d3ad3 100644 --- a/gems/activerecord/6.1/_test/activerecord-6.1.rb +++ b/gems/activerecord/6.1/_test/activerecord-6.1.rb @@ -7,3 +7,7 @@ user.articles.upsert_all([{ id: 1, name: 'James' }], returning: %i[id name], unique_by: :id, record_timestamps: true) user.values_at(:name, :age) user.values_at("name", :age) +User.strict_loading +User.strict_loading(false) +user.strict_loading! +user.strict_loading? diff --git a/gems/activerecord/6.1/activerecord-6.1.rbs b/gems/activerecord/6.1/activerecord-6.1.rbs index 9d1cf1ab..41df6913 100644 --- a/gems/activerecord/6.1/activerecord-6.1.rbs +++ b/gems/activerecord/6.1/activerecord-6.1.rbs @@ -4,10 +4,16 @@ module ActiveRecord def delegated_type: (Symbol role, types: Array[String], **untyped options) -> void end extend DelegatedType + + module ClassMethods[Model, Relation, PrimaryKey] + def strict_loading: (?bool value) -> Relation + end end module Core def values_at: (*Symbol | String) -> Array[untyped] + def strict_loading!: () -> bool + def strict_loading?: () -> bool end class AssociationRelation diff --git a/gems/activerecord/7.0/_test/test.rb b/gems/activerecord/7.0/_test/test.rb index d0ca0de2..b5b05728 100644 --- a/gems/activerecord/7.0/_test/test.rb +++ b/gems/activerecord/7.0/_test/test.rb @@ -34,6 +34,8 @@ class Article < ActiveRecord::Base User.insert_all!([{ id: 1, name: 'James' }], returning: %i[id name], record_timestamps: true) User.upsert({ id: 1, name: 'James' }, returning: %i[id name], unique_by: :id, record_timestamps: true) 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 = User.new(secret: 'dummy', key: 'dummy', token: 'dummy', phrase: 'dummy') user.encrypt user.encrypted_attribute?(:secret) @@ -47,4 +49,7 @@ class Article < ActiveRecord::Base user.articles.upsert_all([{ id: 1, name: 'James' }], returning: %i[id name], unique_by: :id, record_timestamps: true) user.values_at(:name, :age) user.values_at("name", :age) + user.strict_loading! + user.strict_loading!(false, mode: :n_plus_one_only) + user.strict_loading? end diff --git a/gems/activerecord/7.0/activerecord-7.0.rbs b/gems/activerecord/7.0/activerecord-7.0.rbs index edfd047c..f5f20277 100644 --- a/gems/activerecord/7.0/activerecord-7.0.rbs +++ b/gems/activerecord/7.0/activerecord-7.0.rbs @@ -2,10 +2,16 @@ module ActiveRecord class Base include Encryption::EncryptableRecord extend Encryption::EncryptableRecord::ClassMethods + + module ClassMethods[Model, Relation, PrimaryKey] + def strict_loading: (?bool value) -> Relation + end end module Core def values_at: (*Symbol | String) -> Array[untyped] + def strict_loading!: (?bool value, ?mode: Symbol) -> bool + def strict_loading?: () -> bool end class AssociationRelation diff --git a/gems/activerecord/7.1/_test/test.rb b/gems/activerecord/7.1/_test/test.rb index d28ea42c..fa6a49ab 100644 --- a/gems/activerecord/7.1/_test/test.rb +++ b/gems/activerecord/7.1/_test/test.rb @@ -42,6 +42,8 @@ class Article < ActiveRecord::Base User.upsert({ id: 1, name: 'James' }, returning: %i[id name], unique_by: :id, record_timestamps: true) User.upsert_all([{ id: 1, name: 'James' }], returning: %i[id name], unique_by: :id, record_timestamps: true) User.with(admin_users: User.where(role: 0)) + User.strict_loading + User.strict_loading(false) user = User.new(secret: 'dummy', key: 'dummy', token: 'dummy', phrase: 'dummy') user.encrypt user.encrypted_attribute?(:secret) @@ -56,6 +58,9 @@ class Article < ActiveRecord::Base user.generate_token_for(:password_reset) user.values_at(:name, :age) user.values_at("name", :age) + user.strict_loading! + user.strict_loading!(false, mode: :n_plus_one_only) + user.strict_loading? 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 2f6dca9b..1ddcc4a0 100644 --- a/gems/activerecord/7.1/activerecord-7.1.rbs +++ b/gems/activerecord/7.1/activerecord-7.1.rbs @@ -8,10 +8,16 @@ module ActiveRecord extend SecurePassword::ClassMethods include TokenFor extend TokenFor::ClassMethods + + module ClassMethods[Model, Relation, PrimaryKey] + def strict_loading: (?bool value) -> Relation + end end module Core def values_at: (*Symbol | String) -> Array[untyped] + def strict_loading!: (?bool value, ?mode: Symbol) -> bool + def strict_loading?: () -> bool end class AssociationRelation diff --git a/gems/activerecord/7.2/_test/test.rb b/gems/activerecord/7.2/_test/test.rb index b47e6fc3..f66b4f9a 100644 --- a/gems/activerecord/7.2/_test/test.rb +++ b/gems/activerecord/7.2/_test/test.rb @@ -40,6 +40,8 @@ class Article < ApplicationRecord User.upsert({ id: 1, name: 'James' }, returning: %i[id name], unique_by: :id, record_timestamps: true) User.upsert_all([{ id: 1, name: 'James' }], returning: %i[id name], unique_by: :id, record_timestamps: true) User.with(admin_users: User.where(role: 0)) + User.strict_loading + User.strict_loading(false) user = User.new(secret: 'dummy', key: 'dummy', token: 'dummy', phrase: 'dummy') user.encrypt user.encrypted_attribute?(:secret) @@ -54,6 +56,9 @@ class Article < ApplicationRecord user.generate_token_for(:password_reset) user.values_at(:name, :age) user.values_at("name", :age) + user.strict_loading! + user.strict_loading!(false, mode: :n_plus_one_only) + user.strict_loading? user = User.new user.normalize_attribute(:email) diff --git a/gems/activerecord/7.2/activerecord-7.2.rbs b/gems/activerecord/7.2/activerecord-7.2.rbs index 04762440..d551ecbd 100644 --- a/gems/activerecord/7.2/activerecord-7.2.rbs +++ b/gems/activerecord/7.2/activerecord-7.2.rbs @@ -8,10 +8,16 @@ module ActiveRecord extend SecurePassword::ClassMethods include TokenFor extend TokenFor::ClassMethods + + module ClassMethods[Model, Relation, PrimaryKey] + def strict_loading: (?bool value) -> Relation + end end module Core def values_at: (*Symbol | String) -> Array[untyped] + def strict_loading!: (?bool value, ?mode: Symbol) -> bool + def strict_loading?: () -> bool end class Relation