Skip to content

Commit

Permalink
activerecord: #select and #pluck suppors Arel
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Oct 4, 2024
1 parent 7ae9e3c commit 15e2956
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
3 changes: 3 additions & 0 deletions gems/activerecord/6.0/_test/activerecord-generated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
1 change: 1 addition & 0 deletions gems/activerecord/6.0/_test/activerecord-generated.rbs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions gems/activerecord/6.0/activerecord-generated.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 30 additions & 16 deletions gems/activerecord/6.0/activerecord.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 15e2956

Please sign in to comment.