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

Correct "required" asterisk when using validation option :on #1872

Merged
merged 3 commits into from
Feb 26, 2021

Conversation

pablobm
Copy link
Collaborator

@pablobm pablobm commented Jan 21, 2021

Fixes #1860

We show a "required" asterisk when fields are required. However there are some edge cases, like when the requirement has an if/unless. We don't consider those actually required and we don't show the asterisk there.

But there are other edge cases. Issue #1860 shows how the validation option :on can also create cases where an apparent requirement is not actually a requirement. Normally this option has the values :create or :update, but there are other possibilities.

This PR tries to solve the issue. There are three commits, each an incremental improvement:

  1. Consider :on to be the same as :if and :unless, and hide the asterisk.
  2. Refactor specs to avoid the mystery guests involved in the examples for :if, :unless, and :on. This also allows for more scalable specs, should we decide to add more fine-grained handling of these options.
  3. Consider the special (and most common) cases of on: :create and on: :update, and hide the asterisk for any other :on value.

So we could go just with the first commit, or the first two, or the whole thing. Or more than that! Opinions?

I want to add some new cases here, and I find that the current
form of these specs makes things difficult:

* They depend on artificial validations ("unless false") in the
  example app.
* These artificial validations are mystery guests.
* Adding new variants is not scalable, as the example app only has so
  many models.

So I'm moving specs to a new location where they can be expressed in a
more low-level setting, making them easier to setup and work with.
v.class == ActiveRecord::Validations::PresenceValidator &&
!v.options.include?(:if) &&
!v.options.include?(:unless)
next false unless v.class == ActiveRecord::Validations::PresenceValidator
Copy link

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [83/80]

@pablobm pablobm changed the title Fix required on Correct "required" asterisk when using validation option :on Jan 21, 2021
@nickcharlton
Copy link
Member

I like this change! I think we should — and so I am going to — merge it in as is.

@nickcharlton nickcharlton merged commit 255167d into thoughtbot:master Feb 26, 2021
@pablobm pablobm deleted the fix-required-on branch June 24, 2021 10:36
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.

Administrate doesn't read context on Model when load form for mandatory field
2 participants