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

Cache page_title result to allow for #25

Merged
merged 1 commit into from
Nov 9, 2015
Merged
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
8 changes: 5 additions & 3 deletions lib/rails_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ def page_class
end

def page_title(options={})
default_page_title = "#{page_controller_class.capitalize} #{page_action_class.capitalize}"
i18n_options = { default: default_page_title }.merge!(options)
I18n.t("#{page_controller_class}.#{page_action_class}.title", i18n_options)
@page_title ||= begin
default_page_title = "#{page_controller_class.capitalize} #{page_action_class.capitalize}"
i18n_options = { default: default_page_title }.merge!(options)
I18n.t("#{page_controller_class}.#{page_action_class}.title", i18n_options)
end
end

def javascript_initialization(options = {})
Expand Down
5 changes: 5 additions & 0 deletions test/rails_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
it "uses :default provided by gem user" do
view.page_title(default: 'my custom default').must_equal 'my custom default'
end

it "calling multiple times reuses first result (template renders before layout)" do
view.page_title(default: 'my custom default').must_equal 'my custom default'
view.page_title.must_equal 'my custom default'
end
end

describe 'when translation is available' do
Expand Down