diff --git a/README.md b/README.md index 7fafa8b..07ed4a0 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: '%{user_name} - AppName' +``` + ## Acknowledgement Though the idea of translating titles was arrived at seperately, [Brandon 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