From dcc5e3511b9637fcf112244182f52b03d1ebc28e Mon Sep 17 00:00:00 2001 From: masatoshi_moritsuka Date: Thu, 1 Jan 2026 18:06:33 +0900 Subject: [PATCH] feat(activerecord): Add types for ActiveRecord::Relation#reorder and ActiveRecord::Relation#unscope Similar to https://github.com/ruby/gem_rbs_collection/pull/858, I add types to both interfaces and modules. Because interfaces are still used by some applications and gems. e.g. - https://github.com/ksss/orthoses-rails/blob/v1.13.2/lib/orthoses/active_record/relation.rb#L40 - https://github.com/ksss/orthoses-rails/blob/v1.13.2/lib/orthoses/active_record/relation.rb#L52 - https://github.com/kaigionrails/conference-app/blob/331d122645f4c046702998ddec4b9f2e194fa5e1/sig/rbs_rails/app/models/speakers_talk.rbs#L2 added method implementations are below - https://github.com/rails/rails/blob/v8.1.1/activerecord/lib/active_record/relation/query_methods.rb#L743-L757 - https://github.com/rails/rails/blob/v8.1.1/activerecord/lib/active_record/relation/query_methods.rb#L773-L809 --- gems/activerecord/7.2/_test/activerecord-7.2.rb | 5 +++++ gems/activerecord/7.2/activerecord-7.2.rbs | 8 ++++++++ gems/activerecord/8.0/_test/activerecord-8.0.rb | 5 +++++ gems/activerecord/8.0/activerecord-8.0.rbs | 8 ++++++++ 4 files changed, 26 insertions(+) diff --git a/gems/activerecord/7.2/_test/activerecord-7.2.rb b/gems/activerecord/7.2/_test/activerecord-7.2.rb index 5c1253fb..2354a85b 100644 --- a/gems/activerecord/7.2/_test/activerecord-7.2.rb +++ b/gems/activerecord/7.2/_test/activerecord-7.2.rb @@ -84,4 +84,9 @@ class Article < ApplicationRecord User.with_recursive(admin_users: User.where(role: 0)) ActiveRecord::Base.lease_connection.execute('SELECT * FROM users LIMIT 100;') + + User.unscope(:order) + User.order(:id).unscope(:order) + User.reorder(id: :asc) + User.order(id: :asc).reorder(id: :desc) end diff --git a/gems/activerecord/7.2/activerecord-7.2.rbs b/gems/activerecord/7.2/activerecord-7.2.rbs index 02ab0111..0cf3d42f 100644 --- a/gems/activerecord/7.2/activerecord-7.2.rbs +++ b/gems/activerecord/7.2/activerecord-7.2.rbs @@ -228,6 +228,8 @@ module ActiveRecord def with: (*untyped args) -> self def with_recursive: (*untyped) -> self def with_recursive!: (*untyped) -> self + def reorder: (*untyped args) -> self + def unscope: (*untyped args) -> self def sole: () -> Model end end @@ -236,6 +238,8 @@ module ActiveRecord module ClassMethods[Model, Relation, PrimaryKey] def with: (*untyped args) -> Relation def with_recursive: (*untyped) -> Relation + def reorder: (*untyped args) -> Relation + def unscope: (*untyped args) -> Relation end end end @@ -252,6 +256,8 @@ interface _ActiveRecord_Relation[Model, PrimaryKey] # rubocop:disable RBS/Lint/T def order: (*untyped) -> self def group: (*Symbol | String) -> untyped def in_order_of: (Symbol, Array[untyped]) -> self + def reorder: (*untyped args) -> self + def unscope: (*untyped args) -> self def distinct: () -> self def or: (::ActiveRecord::Relation) -> self def merge: (Array[untyped] | Hash[untyped, untyped] | ActiveRecord::Relation | Proc) -> self @@ -350,6 +356,8 @@ interface _ActiveRecord_Relation_ClassMethods[Model, Relation, PrimaryKey] # rub def order: (*untyped) -> Relation def group: (*Symbol | String) -> untyped def in_order_of: (Symbol, Array[untyped]) -> Relation + def reorder: (*untyped args) -> Relation + def unscope: (*untyped args) -> Relation def distinct: () -> Relation def or: (::ActiveRecord::Relation) -> Relation def merge: (Array[untyped] | Hash[untyped, untyped] | ActiveRecord::Relation | Proc) -> Relation diff --git a/gems/activerecord/8.0/_test/activerecord-8.0.rb b/gems/activerecord/8.0/_test/activerecord-8.0.rb index 5c1253fb..2354a85b 100644 --- a/gems/activerecord/8.0/_test/activerecord-8.0.rb +++ b/gems/activerecord/8.0/_test/activerecord-8.0.rb @@ -84,4 +84,9 @@ class Article < ApplicationRecord User.with_recursive(admin_users: User.where(role: 0)) ActiveRecord::Base.lease_connection.execute('SELECT * FROM users LIMIT 100;') + + User.unscope(:order) + User.order(:id).unscope(:order) + User.reorder(id: :asc) + User.order(id: :asc).reorder(id: :desc) end diff --git a/gems/activerecord/8.0/activerecord-8.0.rbs b/gems/activerecord/8.0/activerecord-8.0.rbs index 02ab0111..0cf3d42f 100644 --- a/gems/activerecord/8.0/activerecord-8.0.rbs +++ b/gems/activerecord/8.0/activerecord-8.0.rbs @@ -228,6 +228,8 @@ module ActiveRecord def with: (*untyped args) -> self def with_recursive: (*untyped) -> self def with_recursive!: (*untyped) -> self + def reorder: (*untyped args) -> self + def unscope: (*untyped args) -> self def sole: () -> Model end end @@ -236,6 +238,8 @@ module ActiveRecord module ClassMethods[Model, Relation, PrimaryKey] def with: (*untyped args) -> Relation def with_recursive: (*untyped) -> Relation + def reorder: (*untyped args) -> Relation + def unscope: (*untyped args) -> Relation end end end @@ -252,6 +256,8 @@ interface _ActiveRecord_Relation[Model, PrimaryKey] # rubocop:disable RBS/Lint/T def order: (*untyped) -> self def group: (*Symbol | String) -> untyped def in_order_of: (Symbol, Array[untyped]) -> self + def reorder: (*untyped args) -> self + def unscope: (*untyped args) -> self def distinct: () -> self def or: (::ActiveRecord::Relation) -> self def merge: (Array[untyped] | Hash[untyped, untyped] | ActiveRecord::Relation | Proc) -> self @@ -350,6 +356,8 @@ interface _ActiveRecord_Relation_ClassMethods[Model, Relation, PrimaryKey] # rub def order: (*untyped) -> Relation def group: (*Symbol | String) -> untyped def in_order_of: (Symbol, Array[untyped]) -> Relation + def reorder: (*untyped args) -> Relation + def unscope: (*untyped args) -> Relation def distinct: () -> Relation def or: (::ActiveRecord::Relation) -> Relation def merge: (Array[untyped] | Hash[untyped, untyped] | ActiveRecord::Relation | Proc) -> Relation