Skip to content

Commit

Permalink
Override types of CollectionProxy
Browse files Browse the repository at this point in the history
To precise the types of CollectionProxy, this overrides their types.

refs: https://api.rubyonrails.org/v8.0/classes/ActiveRecord/Associations/CollectionProxy.html
  • Loading branch information
tk0miya committed Nov 16, 2024
1 parent 5f5c8f8 commit 30189b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/rbs_rails/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ class #{relation_class_name(abs: false)} < ::ActiveRecord::Relation
class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy
include #{generated_relation_methods_name}
include ::_ActiveRecord_Relation[#{klass_name}, #{pk_type}]
def build: (Array[ActiveRecord::Associations::CollectionProxy::_EachPair] attributes) ?{ () -> untyped } -> Array[#{klass_name}]
| (?ActiveRecord::Associations::CollectionProxy::_EachPair attributes) ?{ () -> untyped } -> #{klass_name}
def create: (Array[ActiveRecord::Associations::CollectionProxy::_EachPair] attributes) ?{ () -> untyped } -> Array[#{klass_name}]
| (?ActiveRecord::Associations::CollectionProxy::_EachPair attributes) ?{ () -> untyped } -> #{klass_name}
def create!: (Array[ActiveRecord::Associations::CollectionProxy::_EachPair] attributes) ?{ () -> untyped } -> Array[#{klass_name}]
| (?ActiveRecord::Associations::CollectionProxy::_EachPair attributes) ?{ () -> untyped } -> #{klass_name}
def reload: () -> Array[#{klass_name}]
def replace: (Array[#{klass_name}]) -> void
def delete: (*#{klass_name} | #{pk_type}) -> Array[#{klass_name}]
def destroy: (*#{klass_name} | #{pk_type}) -> Array[#{klass_name}]
def <<: (*#{klass_name} | Array[#{klass_name}]) -> self
def prepend: (*#{klass_name} | Array[#{klass_name}]) -> self
end
RBS
end
Expand Down
14 changes: 14 additions & 0 deletions test/expectations/user.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,19 @@ class User < ::ApplicationRecord
class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy
include ::User::GeneratedRelationMethods
include ::_ActiveRecord_Relation[::User, ::Integer]

def build: (Array[ActiveRecord::Associations::CollectionProxy::_EachPair] attributes) ?{ () -> untyped } -> Array[::User]
| (?ActiveRecord::Associations::CollectionProxy::_EachPair attributes) ?{ () -> untyped } -> ::User
def create: (Array[ActiveRecord::Associations::CollectionProxy::_EachPair] attributes) ?{ () -> untyped } -> Array[::User]
| (?ActiveRecord::Associations::CollectionProxy::_EachPair attributes) ?{ () -> untyped } -> ::User
def create!: (Array[ActiveRecord::Associations::CollectionProxy::_EachPair] attributes) ?{ () -> untyped } -> Array[::User]
| (?ActiveRecord::Associations::CollectionProxy::_EachPair attributes) ?{ () -> untyped } -> ::User
def reload: () -> Array[::User]

def replace: (Array[::User]) -> void
def delete: (*::User | ::Integer) -> Array[::User]
def destroy: (*::User | ::Integer) -> Array[::User]
def <<: (*::User | Array[::User]) -> self
def prepend: (*::User | Array[::User]) -> self
end
end

0 comments on commit 30189b4

Please sign in to comment.