From 017deacb24494dda243940658e0fc55d3f9a032c Mon Sep 17 00:00:00 2001 From: Dirceu Pereira Tiegs Date: Wed, 8 Jan 2025 14:07:06 -0500 Subject: [PATCH] Allow Active Record `select` to return nilable Follow-up to https://github.com/Shopify/tapioca/pull/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] ``` --- lib/tapioca/dsl/compilers/active_record_relations.rb | 2 +- .../tapioca/dsl/compilers/active_record_relations_spec.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/tapioca/dsl/compilers/active_record_relations.rb b/lib/tapioca/dsl/compilers/active_record_relations.rb index dd56639de..0ef175bb0 100644 --- a/lib/tapioca/dsl/compilers/active_record_relations.rb +++ b/lib/tapioca/dsl/compilers/active_record_relations.rb @@ -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 diff --git a/spec/tapioca/dsl/compilers/active_record_relations_spec.rb b/spec/tapioca/dsl/compilers/active_record_relations_spec.rb index df132c199..9a49e05db 100644 --- a/spec/tapioca/dsl/compilers/active_record_relations_spec.rb +++ b/spec/tapioca/dsl/compilers/active_record_relations_spec.rb @@ -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) } @@ -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) } @@ -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) } @@ -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) }