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

Fix reimbursement_category_name ending up in a translation missing error #3507

Conversation

elia
Copy link
Member

@elia elia commented Feb 5, 2020

Description

While debugging the reason for which after the upgrade to Solidus 2.9 an application was still marking store credit with the "Gift Card" store credit category, I found that Spree::StoreCreditCategory.reimbursement_category_name contained the string "translation missing: en.spree.store_credit_category.default", although the I18n key was already set to "Reimbursement".

The problem is that the I18n lookup is done before the Rails app is able to load the application translations.

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 (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@elia elia marked this pull request as ready for review February 5, 2020 15:41
@spaghetticode
Copy link
Member

After some investigation with @elia we discovered the problem is generated when there's a decorator for the class Spree::StoreCreditCategory defined in the store code.

Basically the issue happens because decorator files are loaded before I18n translations are completely initialized.

@kennyadsl
Copy link
Member

@spaghetticode not sure if you are saying that this not needed anymore and it's an issue of how you are decorating files in your application. If it's needed we can take a look at the default option of class_attribute but I think it does the exact same thing and won't solve the issue.

@spaghetticode
Copy link
Member

@kennyadsl I think the fix is still needed, it happens to every store that uses a _decorator.rb file to decorate Spree::StoreCreditCategory.
I agree that using the default option will not solve the issue, though I did not try it... just guessing.

Elia's solution works great but is specific for this situation; I'd prefer a more generic solution that would work for all instances where this problem could happen, both present and future.

One solution that comes to my mind would be to load decorators after loading I18n translations... but not sure if this is feasible, so what we have now may be a good compromise... @elia any thoughts on this?

@elia
Copy link
Member Author

elia commented Feb 19, 2020

@kennyadsl @spaghetticode I would avoid setting a value (unless static) until after the initialization, can be this way or via an actual initializer. So the options I see are:

  • Lazily assign as it's done in this PR
  • Move the default value to an initializer
  • Use some sort of code instead of a display value like name to fetch the record
  • Turn the configuration value into a proc that can be easily loaded and fully customizable without resorting to decorators, that BTW can be used to configure not just the name but the whole fetching strategy.

Since I also object to falling back to .first if no category is found I would say that the last option is superior because it can easily default to the current behavior and scale up to very thick code for fetching the reimbursement category.

@kennyadsl
Copy link
Member

@elia maybe we can just redefine the getter? This way the preference would work with the default value no matter where you call it?

@elia elia force-pushed the elia/fix-reimbursement-category-name-i18n-lookup branch from dd359a0 to fb587a6 Compare March 13, 2020 15:16
@elia elia force-pushed the elia/fix-reimbursement-category-name-i18n-lookup branch from fb587a6 to d3068e0 Compare March 13, 2020 15:50
@elia
Copy link
Member Author

elia commented Mar 13, 2020

@kennyadsl I changed route altogether and moved the responsibility of fetching the store credit category for a reimbursement to the reimbursement itself via Spree::Reimbursement#store_credit_category that in turn relies on Spree::Reimbursement.store_credit_category_proc.

Hope this makes more sense and looks cleaner (especially once we'll throw away the deprecated stuff).

@elia elia force-pushed the elia/fix-reimbursement-category-name-i18n-lookup branch from d3068e0 to eeff1f0 Compare March 23, 2020 17:58
@aldesantis aldesantis added this to the 2.11 milestone May 22, 2020
core/app/models/spree/reimbursement.rb Outdated Show resolved Hide resolved
core/app/models/spree/reimbursement.rb Outdated Show resolved Hide resolved
core/app/models/spree/store_credit_category.rb Outdated Show resolved Hide resolved
@kennyadsl
Copy link
Member

@elia did you have some time to take a look at the comments @aldesantis left? 🙏

@filippoliverani
Copy link
Contributor

filippoliverani commented Sep 4, 2020

@elia did you have some time to take a look at the comments @aldesantis left? 🙏

I've agreed with @elia to take on the PR, I'm pushing some changes to address @aldesantis' comments 👍

@filippoliverani filippoliverani force-pushed the elia/fix-reimbursement-category-name-i18n-lookup branch from eeff1f0 to 2aa7b81 Compare September 4, 2020 15:15
@aldesantis
Copy link
Member

@elia @filippoliverani this looks good overall. Could you guys just squash the commits? 🙏

@elia elia force-pushed the elia/fix-reimbursement-category-name-i18n-lookup branch from 2aa7b81 to a5db6ba Compare September 22, 2020 08:01
@elia
Copy link
Member Author

elia commented Sep 22, 2020

@elia @filippoliverani this looks good overall. Could you guys just squash the commits? 🙏

@aldesantis done ✅

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 generally dislike silencing deprecations outside of the specs. What if the method we are silencing calls another deprecated method?

Since it turned out that silence is not yet thread-safe, I like it even less.

I'd prefer to explore an alternative.

core/app/models/spree/reimbursement.rb Outdated Show resolved Hide resolved
@elia elia marked this pull request as draft September 23, 2020 21:37
@DanielePalombo
Copy link
Contributor

This is the changelog provided from @elia

Fixed how the default reimbursement store-credit category is fetched
Before this change the store-credit category for reimbursement was fetched by name using a missing translation (i.e. en.spree.store_credit_category.default) that resulted in the name "Default". If no category was found the code fell back on the first category from the database, which wasn't guaranteed to be the right one. Trying to update the translation to the desired category name was also useless due to how code was loaded.
Now it's possible to disable this legacy behavior and switch to a simpler one, in which the code will look for a CreditCategory named "Reimbursement". Here's a list of checks and fixes you can perform to ensure you can enable the correct implementation:

  • If you don't use reimbursements you're good to go, skip below to Disabling the legacy behavior
  • Ensure you din't decorate or patched any of the involved code, especially:
    • Spree::StoreCreditCategory.reimbursement_category
    • Spree::StoreCreditCategory.reimbursement_category_name
  • Ensure your "production" environment is already returning the correct category, you can assess that by running this in your "production" console: Spree::StoreCreditCategory.reimbursement_category(nil).name == "Reimbursement"
    Disabling the legacy behavior
    If everything is sound, or you are ok with a different category name for newly created reimbursement store credits you can switch to the new behavior by configuring this Solidus preference in your spree.rb initializer:
Spree.config do |config|
  config.use_legacy_store_credit_reimbursement_category_name = false
end

If you had modifications in your codebase consider disabling the legacy behavior and porting them to a simple overwrite of Spree::Reimbursement#store_credit_category.
The legacy behavior will be removed in the next major version of Solidus.

@kennyadsl kennyadsl requested a review from a team October 9, 2020 15:43
@kennyadsl
Copy link
Member

@aldesantis @spaghetticode can you please review again when you have time?

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.

Code looks good to me, I left a note for a possible improvement, but I'm blocking because the last commit needs to be squashed.


before do
create(:store_credit_category, name: 'foo')
create(:store_credit_category, name: Spree::StoreCreditCategory::REIMBURSEMENT)
Copy link
Member

Choose a reason for hiding this comment

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

I think you can use the new reimbursement trait here

elia and others added 2 commits October 23, 2020 10:27
Replace a non-breaking-space with a regular one.
Add Spree::Reimbursement.store_credit_category_proc as a simple way
to redefine how the reimbursement category should be fetched.

Add Spree::Reimbursement#store_credit_category as the most natural
way to fetch a given category for a reimbursement.

This fixes reimbursement_category_name ending up in a translation
missing error. We need to lazily load the translation because, when
the file is loaded, Rails will still have to load translations from
the main application.

Co-Authored-By: Filippo Liverani <dev@mailvore.com>
@elia elia force-pushed the elia/fix-reimbursement-category-name-i18n-lookup branch from 9814a75 to 7ade8ca Compare October 23, 2020 08:27
@spaghetticode
Copy link
Member

@elia no need to squash the second commit, looks legit 👍
Thank you!

@spaghetticode spaghetticode merged commit efb3819 into solidusio:master Oct 23, 2020
@spaghetticode spaghetticode deleted the elia/fix-reimbursement-category-name-i18n-lookup branch October 23, 2020 09:35
kennyadsl added a commit to nebulab/solidus that referenced this pull request Nov 17, 2020
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Nov 19, 2020
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Nov 27, 2020
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Nov 27, 2020
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Dec 9, 2020
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Dec 10, 2020
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Dec 15, 2020
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
DanielePalombo pushed a commit to nebulab/solidus that referenced this pull request Dec 18, 2020
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Dec 21, 2020
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Jan 19, 2021
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Jan 28, 2021
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
kennyadsl added a commit to nebulab/solidus that referenced this pull request Jan 28, 2021
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
rmparr pushed a commit to rmparr/solidus that referenced this pull request Jun 1, 2022
It removes:

- .reimbursement_category_name
- .reimbursement_category_name=
- .reimbursement_category
- #reimbursement_category_name

ref: solidusio#3507
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.

None yet

9 participants