Skip to content

Commit

Permalink
Update Active Record find sig to allow a block
Browse files Browse the repository at this point in the history
With a block it behaves like Enumerable#find
  • Loading branch information
bdewater committed Mar 26, 2024
1 parent 2665a04 commit d7b3426
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/tapioca/dsl/compilers/active_record_relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,21 @@ def create_common_methods
parameters: [create_param("args", type: array_type)],
return_type: "T::Enumerable[#{constant_name}]",
),
common_relation_methods_module.create_sig(
parameters: [
create_param("args", type: "T.nilable(#{id_types})"),
create_block_param(
"block",
type: "T.proc.params(object: #{constant_name}).void)",
),
],
return_type: as_nilable_type(constant_name),
),
]
common_relation_methods_module.create_method_with_sigs(
"find",
sigs: sigs,
parameters: [RBI::ReqParam.new("args")],
parameters: [RBI::OptParam.new("args", "nil"), RBI::BlockParam.new("block")],
)
when :find_by
create_common_method(
Expand Down
3 changes: 2 additions & 1 deletion spec/tapioca/dsl/compilers/active_record_relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def fifth!; end
sig { params(args: T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything])).returns(::Post) }
sig { params(args: T::Array[T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything])]).returns(T::Enumerable[::Post]) }
def find(args); end
sig { params(args: T.nilable(T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything])), block: T.proc.params(object: ::Post).void)).returns(T.nilable(::Post)) }
def find(args = nil, &block); end
sig { params(args: T.untyped).returns(T.nilable(::Post)) }
def find_by(*args); end
Expand Down

0 comments on commit d7b3426

Please sign in to comment.