From a737dd34364c9f9ca3be7a7b3acca41dac5c14ce Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Tue, 29 May 2018 13:40:24 -0700 Subject: [PATCH] Dynamically render ReportsController translations 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. --- .../controllers/spree/admin/reports_controller.rb | 6 +++++- backend/app/views/spree/admin/reports/index.html.erb | 4 ++-- .../spree/admin/reports_controller_spec.rb | 12 ++---------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/backend/app/controllers/spree/admin/reports_controller.rb b/backend/app/controllers/spree/admin/reports_controller.rb index 6862dd70ec1..d1c4a1c9acb 100644 --- a/backend/app/controllers/spree/admin/reports_controller.rb +++ b/backend/app/controllers/spree/admin/reports_controller.rb @@ -14,7 +14,11 @@ def add_available_report!(report_key, report_description_key = nil) if report_description_key.nil? report_description_key = "#{report_key}_description" end - @@available_reports[report_key] = { name: I18n.t(report_key, scope: 'spree'), description: I18n.t(report_description_key, scope: 'spree') } + + @@available_reports[report_key] = { + name: report_key, + description: report_description_key, + } end end diff --git a/backend/app/views/spree/admin/reports/index.html.erb b/backend/app/views/spree/admin/reports/index.html.erb index 6142c9c5428..ef41d51955e 100644 --- a/backend/app/views/spree/admin/reports/index.html.erb +++ b/backend/app/views/spree/admin/reports/index.html.erb @@ -11,8 +11,8 @@ <% @reports.each do |key, value| %> - <%= link_to value[:name], send("#{key}_admin_reports_url".to_sym) %> - <%= value[:description] %> + <%= link_to t(value[:name], scope: 'spree'), send("#{key}_admin_reports_url".to_sym) %> + <%= t(value[:description], scope: 'spree') %> <% end %> diff --git a/backend/spec/controllers/spree/admin/reports_controller_spec.rb b/backend/spec/controllers/spree/admin/reports_controller_spec.rb index 9095f552d81..7046178f643 100644 --- a/backend/spec/controllers/spree/admin/reports_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/reports_controller_spec.rb @@ -9,24 +9,16 @@ it 'should contain sales_total' do expect(Spree::Admin::ReportsController.available_reports.keys.include?(:sales_total)).to be true end - - 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 end describe 'ReportsController.add_available_report!' do context 'when adding the report name' do it 'should contain the report' do - I18n.backend.store_translations(:en, spree: { - some_report: 'Awesome Report', - some_report_description: 'This report is great!' - }) Spree::Admin::ReportsController.add_available_report!(:some_report) expect(Spree::Admin::ReportsController.available_reports.keys.include?(:some_report)).to be true expect(Spree::Admin::ReportsController.available_reports[:some_report]).to eq( - name: 'Awesome Report', - description: 'This report is great!' + name: :some_report, + description: 'some_report_description' ) end end