Skip to content

Fix type signature for ActiveRecord::Base.validate (to better support official Ruby documentation) #859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

yuzisee
Copy link
Contributor

@yuzisee yuzisee commented May 18, 2025

According to https://guides.rubyonrails.org/v6.0/active_record_validations.html#combining-validation-conditions

the official documentation says you can have
if: [Proc.new { |c| c.market.retail? }, :desktop?],
so, RBS signatures should reflect that.

…mbining-validation-conditions

In the official documentation, you can have `if: [Proc.new { |c| c.market.retail? }, :desktop?],` so, RBS signatures should reflect that
Copy link

@yuzisee Thanks for your contribution!

Please follow the instructions below for each change.
See also: https://github.com/ruby/gem_rbs_collection/blob/main/docs/CONTRIBUTING.md

Available commands

You can use the following commands by commenting on this PR.

  • /merge: Merge this PR if CI passes

activerecord

You changed RBS files for an existing gem.
You need to get approval from the reviewers of this gem.

@hibariya, @ksss, @Little-Rubyist, @tk0miya, please review this pull request.
If this change is acceptable, please make a review comment including APPROVE from here.
Screen Shot 2024-03-19 at 14 13 36

After that, the PR author or the reviewers can merge this PR.
Just comment /merge to merge this PR.

@yuzisee yuzisee changed the title Fix type signature for ActiveRecord::Base.validate (to match official Ruby documentation) Fix type signature for ActiveRecord::Base.validate (to better support official Ruby documentation) May 18, 2025
yuzisee added 9 commits May 18, 2025 05:55
…thod

  Error: activerecord.rb:1:6: [error] Cannot find implementation of method `::TestCallbackObject#local_condition2`
  │ Diagnostic ID: Ruby::MethodDefinitionMissing
  │
  └ class TestCallbackObject < ActiveRecord::Base
          ~~~~~~~~~~~~~~~~~~
  
  Error: activerecord.rb:1:6: [error] Cannot find implementation of method `::TestCallbackObject#local_condition3`
  │ Diagnostic ID: Ruby::MethodDefinitionMissing
  │
  └ class TestCallbackObject < ActiveRecord::Base
          ~~~~~~~~~~~~~~~~~~
  
  Detected 3 problems from 1 file
…an `^(T) [self: T] -> boolish`

Is this a **steep 1.10** vs. **steep 1.5** thing?
Copy link
Contributor

@tk0miya tk0miya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Could you check the errors in CI? I'll merge this after CI passed.

[true, false].sample
end

validate :custom_validation, on: :update, unless: [:local_condition1, proc { |this_rec| this_rec.local_condition2 }, ->(my_rec) { my_rec.local_condition3 }]
Copy link
Contributor Author

@yuzisee yuzisee May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: #859 (review)

LGTM. Could you check the errors in CI? I'll merge this after CI passed.

Thanks! The steep check is succeeding, but steep stats is complaining:

⋮
  No type error detected. 🫖
  2 method calls with untyped receiver detected from activerecord.rb. Please assign the expected type to the receivers
⋮

But…

  1. if ->(my_rec) { my_rec.local_condition3 } is matching ^(T) [self: T] -> boolish

  2. if proc { |this_rec| this_rec.local_condition2 } matches Proc

    • then there's no way to set a type for this_rec right?
    • (for example, we can do 312d68d but it defeats the purpose of the test)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tk0miya do you think I should try to use https://github.com/soutaro/steep/blob/master/manual/annotations.md here? I'm not sure how else to set the type of the receiver inside the unless: block while still keeping the syntax from the original Rails documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants