From 5b1f9f901d1494fda876df9345ce11fb9ebe0d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Mugnolo?= Date: Sun, 14 Jan 2018 11:57:57 -0300 Subject: [PATCH] Make context safe for interpolation --- app/helpers/title/title_helper.rb | 10 +++++++++- spec/helpers/title_helper_spec.rb | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/helpers/title/title_helper.rb b/app/helpers/title/title_helper.rb index ce1a2ce..acb51e0 100644 --- a/app/helpers/title/title_helper.rb +++ b/app/helpers/title/title_helper.rb @@ -15,7 +15,7 @@ def initialize(controller_path, action_name, context) def to_s I18n.t( [:titles, controller_name, action_name].join('.'), - context.merge(default: defaults) + safe_context.merge(default: defaults) ) end @@ -59,6 +59,14 @@ def adjusted_action_name(action_name) action_name end end + + def reserved_keys + I18n::RESERVED_KEYS + end + + def safe_context + context.except(*reserved_keys) + end end end end diff --git a/spec/helpers/title_helper_spec.rb b/spec/helpers/title_helper_spec.rb index 82e11cb..05c9993 100644 --- a/spec/helpers/title_helper_spec.rb +++ b/spec/helpers/title_helper_spec.rb @@ -51,6 +51,15 @@ expect(helper.title(greeting: 'Hello')).to eq('Hello Caleb') end + it 'makes context safe to be passed as interpolation options' do + stub_rails + stub_controller_and_action(:users, :show) + load_translations(users: { show: 'User' }) + allow(helper).to receive_message_chain(:controller, :view_assigns).and_return('scope' => 'Foo') + + expect(helper.title).to eq('User') + end + def stub_rails allow(helper).to receive(:controller_path).and_return('dashboards') allow(helper).to receive(:action_name)