Skip to content

Commit

Permalink
Allow Active Record select to return nilable
Browse files Browse the repository at this point in the history
Follow-up to #2103

QueryMethods#select:

```ruby
> model.features.class
=> Feature::ActiveRecord_Associations_CollectionProxy
> model.features.select { nil }
=> []
```

Enumerable#select:

```ruby
> a = T.let([1, nil, 3], T::Array[T.nilable(Integer)])
>
> T.reveal_type(a)
>
* a.select do |num|
*   num.to_c
> end
=> [1, nil, 3]
```
  • Loading branch information
dirceu committed Jan 8, 2025
1 parent 4ad2080 commit 017deac
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/tapioca/dsl/compilers/active_record_relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def create_relation_methods
sig.return_type = mod == relation_methods_module ? RelationClassName : AssociationRelationClassName
end
method.add_sig do |sig|
sig.add_param("blk", "T.proc.params(record: #{constant_name}).returns(T::Boolean)")
sig.add_param("blk", "T.proc.params(record: #{constant_name}).returns(T.nilable(T::Boolean))")
sig.return_type = "T::Array[#{constant_name}]"
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/tapioca/dsl/compilers/active_record_relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def reverse_order(*args, &blk); end
def rewhere(*args, &blk); end
sig { params(args: T.untyped).returns(PrivateAssociationRelation) }
sig { params(blk: T.proc.params(record: ::Post).returns(T::Boolean)).returns(T::Array[::Post]) }
sig { params(blk: T.proc.params(record: ::Post).returns(T.nilable(T::Boolean))).returns(T::Array[::Post]) }
def select(*args, &blk); end
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateAssociationRelation) }
Expand Down Expand Up @@ -594,7 +594,7 @@ def reverse_order(*args, &blk); end
def rewhere(*args, &blk); end
sig { params(args: T.untyped).returns(PrivateRelation) }
sig { params(blk: T.proc.params(record: ::Post).returns(T::Boolean)).returns(T::Array[::Post]) }
sig { params(blk: T.proc.params(record: ::Post).returns(T.nilable(T::Boolean))).returns(T::Array[::Post]) }
def select(*args, &blk); end
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateRelation) }
Expand Down Expand Up @@ -1159,7 +1159,7 @@ def reverse_order(*args, &blk); end
def rewhere(*args, &blk); end
sig { params(args: T.untyped).returns(PrivateAssociationRelation) }
sig { params(blk: T.proc.params(record: ::Post).returns(T::Boolean)).returns(T::Array[::Post]) }
sig { params(blk: T.proc.params(record: ::Post).returns(T.nilable(T::Boolean))).returns(T::Array[::Post]) }
def select(*args, &blk); end
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateAssociationRelation) }
Expand Down Expand Up @@ -1320,7 +1320,7 @@ def reverse_order(*args, &blk); end
def rewhere(*args, &blk); end
sig { params(args: T.untyped).returns(PrivateRelation) }
sig { params(blk: T.proc.params(record: ::Post).returns(T::Boolean)).returns(T::Array[::Post]) }
sig { params(blk: T.proc.params(record: ::Post).returns(T.nilable(T::Boolean))).returns(T::Array[::Post]) }
def select(*args, &blk); end
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateRelation) }
Expand Down

0 comments on commit 017deac

Please sign in to comment.