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

Pass additional context to TitleHelper#title #3

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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><%= title(user_name: current_user.name) %></title>
```

```yaml
en:
titles:
application: '%{user_name} - AppName'
```

## Acknowledgement

Though the idea of translating titles was arrived at seperately, [Brandon
Expand Down
5 changes: 3 additions & 2 deletions app/helpers/title/title_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 11 additions & 2 deletions spec/helpers/title_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down