From 15e29560c171e2c1ba66e0f929e11ac8a53e40d4 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 4 Oct 2024 01:54:28 +0900 Subject: [PATCH] activerecord: #select and #pluck suppors Arel --- .../6.0/_test/activerecord-generated.rb | 3 ++ .../6.0/_test/activerecord-generated.rbs | 1 + .../6.0/activerecord-generated.rbs | 2 - gems/activerecord/6.0/activerecord.rbs | 46 ++++++++++++------- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/gems/activerecord/6.0/_test/activerecord-generated.rb b/gems/activerecord/6.0/_test/activerecord-generated.rb index 000a1bfc5..fe03a1d6f 100644 --- a/gems/activerecord/6.0/_test/activerecord-generated.rb +++ b/gems/activerecord/6.0/_test/activerecord-generated.rb @@ -29,3 +29,6 @@ class Article < ActiveRecord::Base User.preload(:address, friends: [:address, :followers]) User.in_order_of(:id, [1, 5, 3]) User.offset(5).limit(10) + +t = User.arel_table +User.limit(10).select(:id, "name", t[:age].as("years"), t[:email]) diff --git a/gems/activerecord/6.0/_test/activerecord-generated.rbs b/gems/activerecord/6.0/_test/activerecord-generated.rbs index 4cd6dc228..5ee206ac8 100644 --- a/gems/activerecord/6.0/_test/activerecord-generated.rbs +++ b/gems/activerecord/6.0/_test/activerecord-generated.rbs @@ -1,5 +1,6 @@ class User < ActiveRecord::Base class ActiveRecord_Relation < ActiveRecord::Relation + include ActiveRecord::Relation::Methods[User, Integer] end class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy diff --git a/gems/activerecord/6.0/activerecord-generated.rbs b/gems/activerecord/6.0/activerecord-generated.rbs index e6dd466a0..1d8f722d2 100644 --- a/gems/activerecord/6.0/activerecord-generated.rbs +++ b/gems/activerecord/6.0/activerecord-generated.rbs @@ -11970,8 +11970,6 @@ module ActiveRecord def ===: (untyped object) -> untyped - def arel_table: () -> untyped - def arel_attribute: (untyped name, ?untyped table) -> untyped def predicate_builder: () -> untyped diff --git a/gems/activerecord/6.0/activerecord.rbs b/gems/activerecord/6.0/activerecord.rbs index fb219be43..6859c6b99 100644 --- a/gems/activerecord/6.0/activerecord.rbs +++ b/gems/activerecord/6.0/activerecord.rbs @@ -289,6 +289,12 @@ module ActiveRecord def self.default_scope: (?untyped? scope) -> untyped | ... end + module Core + module ClassMethods + def arel_table: () -> Arel::Table + end + end + module QueryMethods class WhereChain[Relation] include ActiveModel::ForbiddenAttributesProtection @@ -330,8 +336,8 @@ module ActiveRecord def all: () -> self def ids: () -> Array[PrimaryKey] def none: () -> self - def pluck: (Symbol | String column) -> Array[untyped] - | (*Symbol | String columns) -> Array[Array[untyped]] + def pluck: (Symbol | String | Arel::Nodes::t column) -> Array[untyped] + | (*Symbol | String | Arel::Nodes::t columns) -> Array[Array[untyped]] def where: () -> ::ActiveRecord::QueryMethods::WhereChain[self] | (*untyped) -> self def exists?: (*untyped) -> bool @@ -401,9 +407,9 @@ module ActiveRecord def destroy_by: (*untyped) -> untyped def delete_by: (*untyped) -> untyped def each: () { (Model) -> void } -> self - def select: (*Symbol | String) -> self + def select: (*Symbol | String | Arel::Nodes::t) -> self | () { (Model) -> boolish } -> Array[Model] - def reselect: (*Symbol | String) -> self + def reselect: (*Symbol | String | Arel::Nodes::t) -> self end end @@ -412,8 +418,8 @@ module ActiveRecord def all: () -> Relation def ids: () -> Array[PrimaryKey] def none: () -> Relation - def pluck: (Symbol | String column) -> Array[untyped] - | (*Symbol | String columns) -> Array[Array[untyped]] + def pluck: (Symbol | String | Arel::Nodes::t column) -> Array[untyped] + | (*Symbol | String | Arel::Nodes::t columns) -> Array[Array[untyped]] def where: () -> ::ActiveRecord::QueryMethods::WhereChain[Relation] | (*untyped) -> Relation def exists?: (*untyped) -> bool @@ -488,9 +494,9 @@ module ActiveRecord def touch_all: (*untyped, ?time: untyped) -> untyped def destroy_by: (*untyped) -> untyped def delete_by: (*untyped) -> untyped - def select: (*Symbol | String) -> Relation + def select: (*Symbol | String | Arel::Nodes::t) -> Relation | () { (Model) -> boolish } -> Array[Model] - def reselect: (*Symbol | String) -> Relation + def reselect: (*Symbol | String | Arel::Nodes::t) -> Relation def scope: (Symbol, ^(*untyped, **untyped) [self: Relation] -> void) ?{ (Module extention) [self: Relation] -> void } -> void end end @@ -500,8 +506,8 @@ interface _ActiveRecord_Relation[Model, PrimaryKey] def all: () -> self def ids: () -> Array[PrimaryKey] def none: () -> self - def pluck: (Symbol | String column) -> Array[untyped] - | (*Symbol | String columns) -> Array[Array[untyped]] + def pluck: (Symbol | String | Arel::Nodes::t column) -> Array[untyped] + | (*Symbol | String | Arel::Nodes::t columns) -> Array[Array[untyped]] def where: () -> ::ActiveRecord::QueryMethods::WhereChain[self] | (*untyped) -> self def exists?: (*untyped) -> bool @@ -571,17 +577,17 @@ interface _ActiveRecord_Relation[Model, PrimaryKey] def destroy_by: (*untyped) -> untyped def delete_by: (*untyped) -> untyped def each: () { (Model) -> void } -> self - def select: (*Symbol | String) -> self + def select: (*Symbol | String | Arel::Nodes::t) -> self | () { (Model) -> boolish } -> Array[Model] - def reselect: (*Symbol | String) -> self + def reselect: (*Symbol | String | Arel::Nodes::t) -> self end interface _ActiveRecord_Relation_ClassMethods[Model, Relation, PrimaryKey] def all: () -> Relation def ids: () -> Array[PrimaryKey] def none: () -> Relation - def pluck: (Symbol | String column) -> Array[untyped] - | (*Symbol | String columns) -> Array[Array[untyped]] + def pluck: (Symbol | String | Arel::Nodes::t column) -> Array[untyped] + | (*Symbol | String | Arel::Nodes::t columns) -> Array[Array[untyped]] def where: () -> ::ActiveRecord::QueryMethods::WhereChain[Relation] | (*untyped) -> Relation def exists?: (*untyped) -> bool @@ -656,9 +662,9 @@ interface _ActiveRecord_Relation_ClassMethods[Model, Relation, PrimaryKey] def touch_all: (*untyped, ?time: untyped) -> untyped def destroy_by: (*untyped) -> untyped def delete_by: (*untyped) -> untyped - def select: (*Symbol | String) -> Relation + def select: (*Symbol | String | Arel::Nodes::t) -> Relation | () { (Model) -> boolish } -> Array[Model] - def reselect: (*Symbol | String) -> Relation + def reselect: (*Symbol | String | Arel::Nodes::t) -> Relation def scope: (Symbol, ^(*untyped, **untyped) [self: Relation] -> void) ?{ (Module extention) [self: Relation] -> void } -> void end @@ -668,6 +674,14 @@ module Arel end end +module Arel + module Nodes + # https://github.com/rails/rails/blob/v6.0.6.1/activerecord/lib/arel.rb#L45-L47 + + type t = Node | Attributes::Attribute | SqlLiteral + end +end + # https://github.com/rails/rails/blob/fbe2433be6e052a1acac63c7faf287c52ed3c5ba/activerecord/lib/arel/nodes/unary.rb module Arel module Nodes