From ce1610be93403b9e731abe8a5de99b3eb98bc983 Mon Sep 17 00:00:00 2001 From: George Brocklehurst Date: Fri, 4 Jul 2014 10:17:35 +0200 Subject: [PATCH 1/2] Add additional_context param to TitleHelper#title The helper previously passed the view assigns to the PageTitle to be used in internationalised strings. This commit allows the caller to pass a Hash which will be merged into the view assigns, making it possible to use additional values in page titles. --- app/helpers/title/title_helper.rb | 5 +++-- spec/helpers/title_helper_spec.rb | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/helpers/title/title_helper.rb b/app/helpers/title/title_helper.rb index 3f5c319..6e0581e 100644 --- a/app/helpers/title/title_helper.rb +++ b/app/helpers/title/title_helper.rb @@ -1,7 +1,8 @@ module Title module TitleHelper - def title - PageTitle.new(controller_path, action_name, controller.view_assigns.symbolize_keys).to_s + def title(additional_context = {}) + context = controller.view_assigns.merge(additional_context).symbolize_keys + PageTitle.new(controller_path, action_name, context).to_s end class PageTitle diff --git a/spec/helpers/title_helper_spec.rb b/spec/helpers/title_helper_spec.rb index d2dbcc9..23920c5 100644 --- a/spec/helpers/title_helper_spec.rb +++ b/spec/helpers/title_helper_spec.rb @@ -37,15 +37,24 @@ stub_rails stub_controller_and_action(:users, :show) load_translations(users: { show: '%{name}' }) - helper.stub_chain(:controller, :view_assigns, :symbolize_keys).and_return({ name: 'Caleb' }) + helper.stub_chain(:controller, :view_assigns).and_return('name' => 'Caleb') expect(helper.title).to eq('Caleb') end + it 'can accept a hash of extra context in addition to the view assigns' do + stub_rails + stub_controller_and_action(:users, :show) + load_translations(users: { show: '%{greeting} %{name}' }) + helper.stub_chain(:controller, :view_assigns).and_return('name' => 'Caleb') + + expect(helper.title(greeting: 'Hello')).to eq('Hello Caleb') + end + def stub_rails helper.stub(:controller_path).and_return('dashboards') helper.stub(:action_name) - helper.stub_chain(:controller, :view_assigns, :symbolize_keys).and_return({}) + helper.stub_chain(:controller, :view_assigns).and_return({}) Rails.stub_chain(:application, :class).and_return('Dummy::Application') end From b34c9833741431d88a1b99367734bc842516d9bf Mon Sep 17 00:00:00 2001 From: George Brocklehurst Date: Fri, 4 Jul 2014 10:24:01 +0200 Subject: [PATCH 2/2] Document additional context parameter in README. --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 7fafa8b..5d50508 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,19 @@ def to_s end ``` +You can pass additional values to the `#title` helper, which can be referenced +in your translations: + +```erb +<%= title(user_name: current_user.name) %> +``` + +```yaml +en: + titles: + application: 'AppName - %{user_name}' +``` + ## Acknowledgement Though the idea of translating titles was arrived at seperately, [Brandon