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 Apr 4, 2024
1 parent cdd80d1 commit a225ce0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 8 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,18 @@ def create_common_methods
parameters: { args: array_type },
return_type: "T::Enumerable[#{constant_name}]",
),
common_relation_methods_module.create_sig(
parameters: {
args: "NilClass",
block: "T.proc.params(object: #{constant_name}).void)",

This comment has been minimized.

Copy link
@brendo

brendo Apr 15, 2024

Contributor

This line introduces a bug with the trailing ) which produces invalid output:

  sorbet/rbi/dsl/platform_connection.rbi:166: unexpected token ")" (2001)
  sorbet/rbi/dsl/platform_connection.rbi:2385: unexpected token "end" (2001)

Using the example produced in the spec, the output is invalid (formatting changed to highlight bug):

sig { 
params(
  args: NilClass, 
  block: T.proc.params(object: ::Post).void
)
) << extra
.returns(T.nilable(::Post)) 
}

cc @bdewater

This comment has been minimized.

Copy link
@brendo

brendo Apr 15, 2024

Contributor

Raised #1867

},
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
6 changes: 4 additions & 2 deletions 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: NilClass, 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 Expand Up @@ -801,7 +802,8 @@ def fifth!; end
<% else %>
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]) }
<% end %>
def find(args); end
sig { params(args: NilClass, 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 a225ce0

Please sign in to comment.