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

Improve Taxon validations and factory #4851

Merged

Conversation

RyanofWoods
Copy link
Contributor

@RyanofWoods RyanofWoods commented Jan 12, 2023

Summary

Fixes #4849

To resolve the two main issues outlined in #4849. This will help prevent stores from creating problematic Taxons when creating them manually as well as making it easier to setup Taxons correctly via its factory.

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

  • I have written a thorough PR description.
  • I have kept my commits small and atomic.
  • I have used clear, explanatory commit messages.

The following are not always needed (cross them out if they are not):

  • I have added automated tests to cover my changes.
    - [ ] I have attached screenshots to demo visual changes.
    - [ ] I have opened a PR to update the guides.
    - [ ] I have updated the README to account for my changes.

Release notes

New Taxon and Taxonomy validations

New validations were added to the Taxon and Taxonomy models but are behind the new temporary extra_taxon_validations and extra_taxonomy_validations preferences.

  • Taxonomy, must have a unique name;
  • Taxon, must have a unique name under the same parent Taxon (or at root level); and
  • Taxon, a validation was added to ensure Taxonomies can only have 1 root,

Due to the upgrading process, you have the opportunity to introduce these preferences and validations at a later time after upgrading to 3.4.

Before using the new preferences via uncommenting them in new_solidus_defaults or loading 3.4 defaults, ensure you don't have any now invalid Taxons or Taxonomies on production.

You can do this by:

  1. Pulling the production Taxons and Taxonomies to a development or staging environment;
  2. Setting the preferences to true;
  3. Running Spree::Taxon.all.select(&:valid?) and Spree::Taxonomy.all.select(&:valid?); and
  4. If you don't have empty arrays, you will need to manually fix these records on production (warning you may have products attached to these Taxons/Taxonomies).

Notes:

  • If you had any invalid records, take caution in the case that you have any code that creates Taxons or Taxonomies.
  • This may break some of your tests if you had incorrectly built Taxons, see Taxon factory changes or easy ways to resolve this.

Improved Taxon testing factory

  • Now, if you pass just a parent Taxon, the Taxonomy will be inferred from the parent;
  • As before, if you pass no taxonomy, a Taxonomy and its root Taxon will be made;
  • Now, the parent will be inferred from the given taxonomy or default created Taxonomy - this means, the created Taxon will always be nested (If you want a root Taxon, create a Taxonomy and get its root);

These changes help to create valid Taxons and makes setup easier.

@RyanofWoods RyanofWoods requested a review from a team as a code owner January 12, 2023 12:07
@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Jan 12, 2023
@RyanofWoods RyanofWoods force-pushed the ryanofwoods/improve-taxon-validations branch from b739ef5 to 913a336 Compare January 12, 2023 12:07
@RyanofWoods
Copy link
Contributor Author

It was difficult to understand the use of the spec that was deleted in Remove unuseful Taxon spec. Discussion about this spec would be appreciated 🙏

@RyanofWoods RyanofWoods force-pushed the ryanofwoods/improve-taxon-validations branch from 913a336 to 665546d Compare January 12, 2023 12:11
@RyanofWoods RyanofWoods force-pushed the ryanofwoods/improve-taxon-validations branch from 665546d to f4c685e Compare January 12, 2023 13:14
core/app/models/spree/taxon.rb Outdated Show resolved Hide resolved
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.

Love it! I've been hit by this issue in the past, thanks for taking care of this. I left some comments but overall this is a great change I look forward to merge.

core/app/models/spree/taxon.rb Outdated Show resolved Hide resolved
core/app/models/spree/taxon.rb Outdated Show resolved Hide resolved
@RyanofWoods RyanofWoods force-pushed the ryanofwoods/improve-taxon-validations branch from f4c685e to 681c790 Compare January 13, 2023 07:44
@waiting-for-dev waiting-for-dev added the type:enhancement Proposed or newly added feature label Jan 16, 2023
@RyanofWoods RyanofWoods force-pushed the ryanofwoods/improve-taxon-validations branch from 681c790 to 3379110 Compare January 16, 2023 12:44
@kennyadsl
Copy link
Member

@RyanofWoods would love to move this forward. Can you please rebase?

@RyanofWoods RyanofWoods force-pushed the ryanofwoods/improve-taxon-validations branch from 3379110 to 7887cbf Compare February 15, 2023 13:02
@RyanofWoods
Copy link
Contributor Author

I rebased and added one new commit @kennyadsl. It fixes a mistake I made in the Taxon factory. On approval, I can squash it into the Improve Taxon factory commit.

@RyanofWoods RyanofWoods force-pushed the ryanofwoods/improve-taxon-validations branch from 7887cbf to a696f43 Compare February 16, 2023 12:52
@RyanofWoods RyanofWoods requested review from kennyadsl and waiting-for-dev and removed request for kennyadsl and waiting-for-dev February 17, 2023 12:53
When creating a Taxon under a Taxonomy normally via admin it will
make set the taxonomy correctly and the parent to a given specific
parent or the the taxonomy root by default.

This makes the factory behave in a similar way.

If you want a root Taxon, you should create a Taxonomy and grab the
root, as a root is automatically created upon creating a Taxonomy.

Note, some reason icons were not being correctly set by the factory for
nested Taxons so it had to be changed to do after creation.
Spree::Taxonomy rightfully assumes there's only one root [1]. Before,
it was it easy to create another "root" taxon, which would not appear
in the admin and/or the Taxonomy would return the wrong root. This is
because `parent_id: nil` is the defining factor of a root.

As the validation is checking uniqueness on taxonomy_id, a spec was
added to ensure a Taxonomy can still have multiple Taxons (just not
multiple ones with a parent_id of nil).

[1] https://github.com/solidusio/solidus/blob/2b79f72aa53f5caa850c587888fff46c1c91f7b7/core/app/models/spree/taxonomy.rb#L10
This spec didn't really test the regression issue well:
1. This spec should call first_or_create! instead to trigger errors as
   well as test attributes;
2. The Taxon would be incorrectly created anyway as no taxonomy_id or
   parent_id is given; and
3. Rails should be responsible for testing this method.

Implementation of spec:
- spree/spree@425cea4

Original issue:
- spree/spree#2703
In the next commit, a validation will be added to ensure their names
are unique if they belong to the same parent. This will indirectly
cause Taxonomies roots to fail if the Taxonomy name already exists.
This will mean the Taxonomy will be created but not the root. To
prevent creation of unuseful Taxonomy, a validation was added to
prevent this at a higher level.
It doesn't make sense to Taxons with the same name under the same
parent.

A spec was also added to ensure Taxons with the same name can exist if
they have different parents.
@RyanofWoods RyanofWoods force-pushed the ryanofwoods/improve-taxon-validations branch from a696f43 to 6df4c2b Compare February 17, 2023 13:07
rainerdema added a commit to solidusio/solidus_starter_frontend that referenced this pull request Mar 21, 2023
The specs on `solidus_starter_frontend` were failing due
to recent improvements with taxon/taxonomy factories made
in the solidus repository, specifically related to PR solidusio/solidus#4851.
This commit updates the specs to be compatible with the
latest changes and ensures that the test suite passes
successfully.
rainerdema added a commit to solidusio/solidus_starter_frontend that referenced this pull request Mar 21, 2023
The specs on `solidus_starter_frontend` were failing due
to recent improvements with taxon/taxonomy factories made
in the solidus repository, specifically related to:
solidusio/solidus#4851
This commit updates the specs to be compatible with the
latest changes and ensures that the test suite passes
successfully.
rainerdema added a commit to solidusio/solidus_starter_frontend that referenced this pull request Mar 22, 2023
The specs on `solidus_starter_frontend` were failing due
to recent improvements with taxon/taxonomy factories made
in the solidus repository, specifically related to:
solidusio/solidus#4851
This commit updates the specs to be compatible with the
latest changes and ensures that the test suite passes
successfully.
kennyadsl added a commit to nebulab/solidus that referenced this pull request Mar 28, 2023
kennyadsl added a commit to nebulab/solidus that referenced this pull request Mar 28, 2023
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 12, 2023
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 12, 2023
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 14, 2023
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 14, 2023
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 18, 2023
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 18, 2023
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 18, 2023
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 18, 2023
kennyadsl added a commit to solidusio/solidus_frontend that referenced this pull request Apr 21, 2023
In Solidus core we improved how taxon factories are built
and we need to reflect that change in the frontend specs
as well.

Ref solidusio/solidus#4851
kennyadsl added a commit to solidusio-contrib/solidus_importer that referenced this pull request May 2, 2023
1. There's no more a frontend
2. Something changed in how images are saved and now they require access
   to the database, we can't use build_stubbed anymore in variant specs
3. some taxon/taxonomy validations was failing due to solidusio/solidus#4851
elia pushed a commit to solidusio-contrib/solidus_importer that referenced this pull request Aug 2, 2023
1. There's no more a frontend
2. Something changed in how images are saved and now they require access
   to the database, we can't use build_stubbed anymore in variant specs
3. some taxon/taxonomy validations was failing due to solidusio/solidus#4851
elia pushed a commit to solidusio-contrib/solidus_importer that referenced this pull request Aug 2, 2023
1. There's no more a frontend
2. Something changed in how images are saved and now they require access
   to the database, we can't use build_stubbed anymore in variant specs
3. some taxon/taxonomy validations was failing due to solidusio/solidus#4851
waiting-for-dev added a commit to solidusio/solidus_graphql_api that referenced this pull request Aug 10, 2023
See solidusio/solidus#4851

We also create an unrelated taxon to make the test more reliable
waiting-for-dev added a commit to solidusio/solidus_graphql_api that referenced this pull request Aug 10, 2023
See solidusio/solidus#4851

We also create an unrelated taxon to make the test more reliable

[skip ci]
waiting-for-dev added a commit to solidusio/solidus_graphql_api that referenced this pull request Aug 11, 2023
See solidusio/solidus#4851

We also create an unrelated taxon in the integration test to make it
more reliable

[skip ci]
waiting-for-dev added a commit to solidusio/solidus_graphql_api that referenced this pull request Aug 11, 2023
See solidusio/solidus#4851

We also create an unrelated taxon in the integration test to make it
more reliable

[skip ci]
waiting-for-dev added a commit to solidusio/solidus_graphql_api that referenced this pull request Aug 14, 2023
See solidusio/solidus#4851

We also create an unrelated taxon in the integration test to make it
more reliable

[skip ci]
waiting-for-dev added a commit to solidusio/solidus_graphql_api that referenced this pull request Aug 14, 2023
See solidusio/solidus#4851

We also create an unrelated taxon in the integration test to make it
more reliable

[skip ci]
waiting-for-dev added a commit to solidusio/solidus_graphql_api that referenced this pull request Aug 15, 2023
See solidusio/solidus#4851

We also create an unrelated taxon in the integration test to make it
more reliable

[skip ci]
waiting-for-dev added a commit to solidusio/solidus_graphql_api that referenced this pull request Aug 15, 2023
See solidusio/solidus#4851

We also create an unrelated taxon in the integration test to make it
more reliable

[skip ci]
ts-ua added a commit to ts-ua/solidus-start-app that referenced this pull request Feb 23, 2024
The specs on `solidus_starter_frontend` were failing due
to recent improvements with taxon/taxonomy factories made
in the solidus repository, specifically related to:
solidusio/solidus#4851
This commit updates the specs to be compatible with the
latest changes and ensures that the test suite passes
successfully.
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 type:enhancement Proposed or newly added feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problematic Taxons can be created
5 participants