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

Refactor address state validation #3129

Merged
merged 3 commits into from
Oct 8, 2020

Conversation

cedum
Copy link
Contributor

@cedum cedum commented Mar 1, 2019

Description

Deprecates and refactors state validation by extracting out the normalization and validation logic into a separate service-like object, which might be also user-provided, for example:

Spree::Address.state_validator_class = UserProvided::Address::StateValidator

The following private methods have been deprecated from Spree::Address model:

  • state_validate
  • validate_state_matches_country

This PR introduces the following configuration flag Spree::Config.use_legacy_address_state_validator that enables to switch from the old state validation behavior to the new in a smooth way.
For newly generated applications, this flag is set to false, which means it will use the new state validator class.
For existing applications upgrading to Solidus v2.11 it is set to true, hence keeping the same behavior as in the previous Solidus versions.

Checklist:

  • Write a recap in this PR description with the normalization and validation rules
  • 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 (if needed)

@cedum cedum force-pushed the cedum/refactor-state-validation branch 5 times, most recently from 7532102 to 47b207c Compare March 1, 2019 18:19
@cedum cedum marked this pull request as ready for review March 1, 2019 18:20
Copy link
Contributor

@aitbw aitbw left a comment

Choose a reason for hiding this comment

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

I like this approach and I can understand the reasoning behind it 👌

That said, this is definitely a breaking change and should be included for Solidus v3.0+

Copy link
Contributor

@ericsaupe ericsaupe left a comment

Choose a reason for hiding this comment

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

I really like this! Definitely a breaking change as @aitbw mentioned. Thanks!

@cedum cedum changed the title Refactor address state validation [WIP] Refactor address state validation Mar 19, 2019
@cedum
Copy link
Contributor Author

cedum commented Mar 20, 2019

Moved the PR back to WIP 🚧. It requires some more work. I'll try to make it less breaking. Besides this, by digging deeper into it I see I misinterpreted some state behaviours and also introduced a few bugs 😅

@kennyadsl kennyadsl added the WIP label Apr 3, 2019
@cedum cedum force-pushed the cedum/refactor-state-validation branch 2 times, most recently from 6fc26f2 to b15ae65 Compare April 5, 2019 15:17
@cedum cedum force-pushed the cedum/refactor-state-validation branch from b15ae65 to f136357 Compare May 17, 2019 14:56
@cedum cedum force-pushed the cedum/refactor-state-validation branch 2 times, most recently from 8b49693 to 2d0c586 Compare May 17, 2019 16:26
@cedum cedum force-pushed the cedum/refactor-state-validation branch 2 times, most recently from 46cdfb3 to 104af58 Compare June 13, 2019 11:54
@cedum cedum force-pushed the cedum/refactor-state-validation branch from 80e110a to a771b33 Compare June 13, 2019 13:01
@cedum cedum changed the title [WIP] Refactor address state validation Refactor address state validation Jun 13, 2019
@cedum cedum force-pushed the cedum/refactor-state-validation branch from a771b33 to c7aded8 Compare June 13, 2019 13:03
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

I like how this is coming along and I left a couple of comments. I'm also curious how and if it makes sense to deprecate the old validation methods instead of removing them.

core/app/models/spree/address.rb Outdated Show resolved Hide resolved
core/app/models/spree/address/state_validator.rb Outdated Show resolved Hide resolved
@address = address
end

def perform!
Copy link
Member

Choose a reason for hiding this comment

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

Personal preference, but I'd use def call here. Did you have a specific reason to use perform! instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have followed the convention of other service-like objects in Solidus that are using perform as their main action method. I have no preferences though.
Probably it's worth defining a project-wise convention for this kind of objects

@cedum cedum force-pushed the cedum/refactor-state-validation branch from c7aded8 to 1e3eb5f Compare June 13, 2019 16:08
@cedum
Copy link
Contributor Author

cedum commented Jun 13, 2019

Ready for a second review 🙏

@kennyadsl I've researched a few methods to deprecate them by showing a warning message only if they have been monkey patched, but the final solution was quite hacky and not working in all cases since there're multiple ways to patch them.
Being both private methods, you think it's worth the effort to deprecate them before removing? I've invested some time into it but I wasn't able to find a +/- clean way to deprecate them.

@cedum cedum force-pushed the cedum/refactor-state-validation branch 3 times, most recently from 1727830 to ea4a79b Compare July 10, 2020 15:30
@kennyadsl kennyadsl removed the release:major Breaking change on hold until next major release label Aug 28, 2020
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

👍

@kennyadsl
Copy link
Member

@cedum we discussed this in the core meeting and we agreed that it would be much better if we have the fix for #2417 and #3098 in a different commit, just for documentation in case someone cannot update and wants to just understand what changed to backport that in their own application. Do you think it's doable?

@cedum
Copy link
Contributor Author

cedum commented Sep 11, 2020

@cedum we discussed this in the core meeting and we agreed that it would be much better if we have the fix for #2417 and #3098 in a different commit, just for documentation in case someone cannot update and wants to just understand what changed to backport that in their own application. Do you think it's doable?

Yep, makes sense! I'm on it

@kennyadsl kennyadsl added Needs Work changelog:solidus_core Changes to the solidus_core gem labels Sep 30, 2020
@cedum cedum force-pushed the cedum/refactor-state-validation branch from ea4a79b to 5ff6913 Compare October 2, 2020 09:48
The Address custom state validation methods are being extracted and
refactored into a separate, configurable class.
@cedum cedum force-pushed the cedum/refactor-state-validation branch from 5ff6913 to 6c89b24 Compare October 2, 2020 09:59
@cedum
Copy link
Contributor Author

cedum commented Oct 2, 2020

I've extracted the fix for #3098 into a separate PR (merged into master), this way it should be easier to backport the fix on stores unable to upgrade.
I've just rebased also this PR and I think it's ready for another review 🙏

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

👍 Thanks @cedum !

Copy link
Member

@aldesantis aldesantis left a comment

Choose a reason for hiding this comment

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

@cedum just left a comment on a deprecation message, but other than that it's good to go!

core/lib/spree/core/engine.rb Outdated Show resolved Hide resolved
Refactors and extract Address state normalization and validation logic
into a separate and configurable service object.
This enables the developers to customize the state validation in a more
clean way by instead of monkey patching private methods.
When saving a credit card, pre-create the country and state as address
attributes instead of passing the id "1".
By having the objects pre-created explicitly, it will cause less
surprises.
@cedum cedum force-pushed the cedum/refactor-state-validation branch from 6c89b24 to ada6fa3 Compare October 2, 2020 13:42
@spaghetticode spaghetticode self-requested a review October 7, 2020 16:25
Copy link
Member

@spaghetticode spaghetticode left a comment

Choose a reason for hiding this comment

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

@cedum thank you 👍

@kennyadsl kennyadsl merged commit 3a04398 into solidusio:master Oct 8, 2020
@kennyadsl kennyadsl deleted the cedum/refactor-state-validation branch October 8, 2020 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants