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

Fixes for batch invitations #2371

Merged
merged 4 commits into from
Sep 22, 2023
Merged

Fixes for batch invitations #2371

merged 4 commits into from
Sep 22, 2023

Conversation

floehopper
Copy link
Contributor

Trello: https://trello.com/c/aO4nVCZT

  • Avoid displaying validation errors on successful processing of file. Previously if a validation error was displayed and then corrected, the error was incorrectly displayed again on the success page.
  • Strip leading & trailing whitespace from BatchInvitationUser#email before validation to avoid unnecessarily confusing validation errors.
  • Strip leading & trailing whitespace from BatchInvitationUser#organisation_slug before save to avoid the potential for failing organisation lookups.
  • Improve test coverage of BatchInvitationUser#email validation.

Trello: https://trello.com/c/aO4nVCZT

When setting flash messages prior to *rendering* vs redirecting, it's
important to use `flash.now` vs `flash` to avoid the error potentially
appearing on a later page thus confusing the user.

Although I've changed the controller test to assert against `flash.now`
instead of `flash` this doesn't seem to force me to make the change in
the controller itself. So I've ended up adding an integration test to
force me to make the change.
Trello: https://trello.com/c/aO4nVCZT

Strip leading & trailing whitespace from BatchInvitationUser#email
before validation to avoid unnecessary validation errors confusing
users.

Note that unlike the strip_whitespace_from_name method, we need to call
this from a before_validation callback rather than a before_save
callback, because otherwise the email validation can fail due to the
presence of the whitespace.

As a general principle, I think we should probably implement this
whitespace-stripping normalization in a before_validation callback, but
I'm going to leave that to whoever picks up this other Trello card [1].

[1]: https://trello.com/c/4QiHXdtE
Trello: https://trello.com/c/aO4nVCZT

Strip leading & trailing whitespace from
BatchInvitationUser#organisation_slug before save to avoid unnecessary
failed organisaion lookups.

I decided to add this normalization even though we aren't aware of
failed organisation lookups happening, because the call to
`Organisation.find_by(slug: organisation_slug)` in
`BatchInvitationUser#organisation_from_slug` wouldn't find an
organisation if an organisation_slug had e.g. leading whitespace.

Since there's no validation on organisation_slug, I've elected to
trigger strip_whitespace_from_organisation_slug from a before_save
callback c.f. BatchInvitationUser#email.

Also the lack of organisation_slug presence validation means that
strip_whitespace_from_organisation_slug can't rely on the
organisation_slug not being nil, so we have to use the safe navigation
operator (&.) in this method.

As a general principle, I think we should probably implement this
whitespace-stripping normalization in a before_validation callback, but
I'm going to leave that to whoever picks up this other Trello card [1].

[1]: https://trello.com/c/4QiHXdtE
Trello: https://trello.com/c/aO4nVCZT

Having implemented strip_whitespace_from_organisation_slug I realised
I'd probably missed a scenario for strip_whitespace_from_email. And sure
enough there was no test coverage for the case when
BatchInvitationUser#email is nil.

In this commit I've improved the test coverage for email validation
which for one thing explicitly forces the presence validation to be
enabled for the email attribute, and for another, it forces me to use
the safe navigation operator in strip_whitespace_from_email. This is
because the latter is called from a before_validation callback and so
email could be nil at this point.
@mike29736 mike29736 self-assigned this Sep 22, 2023
Copy link
Contributor

@mike29736 mike29736 left a comment

Choose a reason for hiding this comment

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

👍

I hadn't seen ActiveRecord::Base::normalizes, thanks for dropping that reference in!

I do wonder how much benefit we're getting from re-rendering that view instead of just redirecting back to it, but these flash.nows do fix the issue 🤷

@mike29736 mike29736 merged commit 8c5ddc6 into main Sep 22, 2023
7 checks passed
@mike29736 mike29736 deleted the batch-invitation-fixes branch September 22, 2023 10:07
floehopper added a commit that referenced this pull request Sep 25, 2023
Trello: https://trello.com/c/aO4nVCZT

I forgot to do this as part of #2371.

I've constructed the query in the migration slightly differently to
similar previous migrations, because I noticed the whitespace character
in the regular expression wasn't working as intended.

I've had to use ActiveRecord::Persistence#update_attribute in order to
skip model validation, because some of the existing BatchInvitationUser
records are invalid for other reasons.

I did consider writing the migration as a SQL UPDATE, but generating a
TRIM expression that is exactly equivalent to String#strip isn't as
trivial as it sounds!
floehopper added a commit that referenced this pull request Sep 25, 2023
Trello: https://trello.com/c/aO4nVCZT

I forgot to do this as part of #2371.

I've constructed the query in the migration slightly differently to
similar previous migrations, because I noticed the whitespace character
in the regular expression wasn't working as intended.

I've had to use ActiveRecord::Persistence#update_attribute in order to
skip model validation, because some of the existing BatchInvitationUser
records are invalid for other reasons.

I did consider writing the migration as a SQL UPDATE, but generating a
TRIM expression that is exactly equivalent to String#strip isn't as
trivial as it sounds!
floehopper added a commit that referenced this pull request Sep 25, 2023
Trello: https://trello.com/c/aO4nVCZT

I forgot to do this as part of #2371.

I've constructed the query in the migration slightly differently to
similar previous migrations, because I noticed the whitespace character
in the regular expression wasn't working as intended.

I've had to use ActiveRecord::Persistence#update_attribute in order to
skip model validation, because some of the existing BatchInvitationUser
records are invalid for other reasons.

I did consider writing the migration as a SQL UPDATE, but generating a
TRIM expression that is exactly equivalent to String#strip isn't as
trivial as it sounds!
floehopper added a commit that referenced this pull request Sep 25, 2023
Trello: https://trello.com/c/aO4nVCZT

I forgot to do this as part of #2371.

I've constructed the query in the migration slightly differently to
similar previous migrations, because I noticed the whitespace character
in the regular expression wasn't working as intended.

I've had to use ActiveRecord::Persistence#update_attribute in order to
skip model validation, because some of the existing BatchInvitationUser
records are invalid for other reasons.

I did consider writing the migration as a SQL UPDATE, but generating a
TRIM expression that is exactly equivalent to String#strip isn't as
trivial as it sounds!
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