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

Dynamically render ReportsController translations #2751

Merged

Conversation

stewart
Copy link
Contributor

@stewart stewart commented May 29, 2018

This PR corrects an issue that occurs when attempting to extend Spree::Admin::ReportsController with additional reports. The ReportsController.add_available_report! class method currently translates its arguments with I18n.t when called, rather than when the /admin/reports page is actually rendered.

If additional reports are being decorated onto this controller via class_eval or prepended modules, there is a possibility that the i18n library has not yet been initialized – this results in static "translation missing" errors in the admin. In particular, this is the case with config.to_prepare, which is commonly used to load these kinds of Solidus modifications.

To correct this issue, we can switch to dynamically rendering the translations in the view, rather than generating them statically in the controller at startup.

This commit modifies how translations for report names/descriptions are
rendered by the `Spree::Admin::ReportsController` class.

The previous implementation generates translations once, at startup,
rendering the translated results statically onto the index page.

This results in confusing behaviour when attempting to add additional
reports to this class via prepended modules or `class_eval`, as the
`i18n` subsystem is not initialized until late in the Rails boot
process, potentially after these modifications have been loaded.

This results in strange and confusing behaviour where the page insists
translations are missing, despite being present.

To remove this source of confusion, we can store the name/description
translation keys in our `@@available_reports` class variable, and
generate the translations from them when the index page is rendered.
@stewart stewart force-pushed the fix/reports-static-translations branch from 35ae3bb to a737dd3 Compare May 29, 2018 22:40

it 'should have the proper sales total report description' do
expect(Spree::Admin::ReportsController.available_reports[:sales_total][:description]).to eql('Sales Total For All Orders')
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It may be worth replacing this test with a view spec, but I'm uncertain how much value testing translations provides in this context.

Copy link
Member

@gmacdougall gmacdougall left a comment

Choose a reason for hiding this comment

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

👍

This is a very weird and difficult to get around issue caused by some over-eager translating.

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.

Makes sense, thanks!

@gmacdougall gmacdougall merged commit d24e3d5 into solidusio:master Jun 29, 2018
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.

4 participants