Skip to content

Use human_attribute_name for promo calculator labels #6195

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mamhoff
Copy link
Contributor

@mamhoff mamhoff commented Mar 27, 2025

Summary

Promotion calculators have preferences, and these need to be translated with the correct scope.

When you specify a preference for a calculator, a virtual attribute is generated. For example:

class MyCalculator < Spree::Calculator
  preference :percent, :integer, default: 10
end

In this scenario, we get a virtual attribute preferred_percent on the MyCalculator class. The expectation is that then one can translated that attribute by calling MyCalculator.human_attribute_name(:preferred_percent), and - crucially - that this attribute is used in form labels.

While all other calculator forms use this (through form.label :preferred_percent) or similar, the default form did not, and instead used I18n.t(:percent, scope: :spree), which is not specific to the calculator, and might lead to mistranslations.

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

@mamhoff mamhoff requested a review from a team as a code owner March 27, 2025 10:17
@github-actions github-actions bot added the changelog:solidus_promotions Changes to the solidus_promotions gem label Mar 27, 2025
@mamhoff mamhoff force-pushed the calculator-label-i18n branch from fc55634 to 8ab1144 Compare March 27, 2025 11:28
Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Thanks. Makes sense. Linter fail is due to a Rubocop update and not related.

Promotion calculators have preferences, and these need to be translated
with the correct scope.

When you specify a preference for a calculator, a virtual attribute is
generated. For example:

```
class MyCalculator < Spree::Calculator
  preference :percent, :integer, default: 10
end
```

In this scenario, we get a virtual attribute `preferred_percent` on the
`MyCalculator` class. The expectation is that then one can translated
that attribute by calling:
`MyCalculator.human_attribute_name(:preferred_percent)`, and - crucially
- that this attribute is used in form labels.

While all other calculator forms use this (through `form.label
:preferred_percent`) or similar, the default form did not, and instead
used `I18n.t(:percent, scope: :spree)`, which is not specific to the
calculator, and might lead to mistranslations.
@tvdeyen tvdeyen force-pushed the calculator-label-i18n branch from 8ab1144 to e259238 Compare March 27, 2025 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_promotions Changes to the solidus_promotions gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants