Skip to content

Commit

Permalink
Fix ActiveRecordRelations Model.new typing
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwise-aiden committed Aug 9, 2024
1 parent 695197a commit d9fd423
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/tapioca/dsl/compilers/active_record_relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,21 @@ def create_common_methods
end
end
end

# We are creating `#new` on the class itself since when called as `Model.new`
# it doesn't allow for an array to be passed. If we kept it as a blanket it
# would mean the passing any `T.untyped` value to the method would assume
# the result is `T::Array` which is not the case majority of the time.
model.create_method("new") do |method|
method.add_opt_param("attributes", "nil")
method.add_block_param("block")

method.add_sig do |sig|
sig.add_param("attributes", "T.untyped")
sig.add_param("block", "T.nilable(T.proc.params(object: #{constant_name}).void)")
sig.return_type = constant_name
end
end
end

sig do
Expand Down
6 changes: 6 additions & 0 deletions spec/tapioca/dsl/compilers/active_record_relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class Post
extend CommonRelationMethods
extend GeneratedRelationMethods
sig { params(attributes: T.untyped, block: T.nilable(T.proc.params(object: ::Post).void)).returns(::Post) }
def new(attributes = nil, &block); end
private
sig { returns(NilClass) }
Expand Down Expand Up @@ -791,6 +794,9 @@ class Post
extend CommonRelationMethods
extend GeneratedRelationMethods
sig { params(attributes: T.untyped, block: T.nilable(T.proc.params(object: ::Post).void)).returns(::Post) }
def new(attributes = nil, &block); end
private
sig { returns(NilClass) }
Expand Down

0 comments on commit d9fd423

Please sign in to comment.