Skip to content
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

Document ActiveModel::Validations#validation_context #46391

Closed
oljfte opened this issue Oct 31, 2022 · 2 comments · Fixed by #46397
Closed

Document ActiveModel::Validations#validation_context #46391

oljfte opened this issue Oct 31, 2022 · 2 comments · Fixed by #46397

Comments

@oljfte
Copy link
Contributor

oljfte commented Oct 31, 2022

The ActiveModel::Validations#validation_context seems to be a public method, but it is not documented on the https://api.rubyonrails.org/. If it is correct to use this publicly, shouldn't it be documented?

I was trying to do something like this comment but could not find the documentation...

Thanks.

@yahonda
Copy link
Member

yahonda commented Oct 31, 2022

Hi, Thanks for opening an issue here. I think validation_context is documented at https://api.rubyonrails.org/classes/ActiveRecord/Validations.html#method-i-save via #36863

@oljfte
Copy link
Contributor Author

oljfte commented Oct 31, 2022

Thank you for the response:)
Let me explain a little further.

Yes, I found the documentation for the context: option of the ActiveRecord::Validations#save method,
but I could not find any documentation on the accessor to the attribute called validation_context that is used internally to handle that context option.

Looking at this part of the code, I see that only the setter is made private, and the getter could still be called publicly.

attr_accessor :validation_context
private :validation_context=

And this method could be used in situations like this #21957 (comment) (see "How to do" section), because there's still no except: option for validate method, like the one proposed in the linked comment.

but this looks kinda workaround-ish to me so I wasn't sure if it should be documented as a public method or not.

shouichi added a commit to shouichi/rails that referenced this issue Nov 29, 2022
Background:

To run validations except in a certain context, we need to either

1. explicitly pass all validation contexts to the `on` option
2. access undocumented `validation_context`

Option 1 is error-prone. Additionally, adding the opposite behavior of
`on` was rejected rails#30710.

Solution:

Document `ActiveModel#validation_context` to make it clear that the API is
public.

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

Successfully merging a pull request may close this issue.

2 participants