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

Backport #4228 to V3.0 #4232

Merged
merged 1 commit into from
Jan 5, 2022

Conversation

jcsanti
Copy link
Contributor

@jcsanti jcsanti commented Dec 27, 2021

Description

This PR backports #4228 to Solidus 3.0.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • [ ] I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change
  • I have attached screenshots to this PR for visual changes (present in original PR)

Promotion codes are always stored in the database in lowercase. Up to
this commit, the transformation of the in-memory value of a code was
performed in a `before_save` callback.

This implementation led to a weird behavior that can easily be
reproduced with a fresh Solidus install:
- create a first promotion, with a single promotion code (e.g. "test")
- attempt to create another promotion, with the exact same value for the
  single promotion code
    => triggers a flash error messages which states "Codes is invalid"
- attempt to re-save the same form, this time with a single promotion
  code with the value "TEST"
    => the page crashes due to an `ActiveRecord::RecordNotUnique` error

The expected error handling would be that any input that would end up
matching an existing code (after having been stripped of spaces &
downcased) should trigger the same flash error rather than crash the
page.

This behavior can be achieved by normalizing values earlier in a
`before_validation` callback: that way, the validation callback will
query the database using a stripped/downcased value and, should a match
be found, set an error on the model instance as expected.
Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

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

Thanks!

@waiting-for-dev waiting-for-dev merged commit 915f417 into solidusio:v3.0 Jan 5, 2022
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.

3 participants