Skip to content

Commit

Permalink
Merge pull request #25 from choonkeat/feature/cache_page_title
Browse files Browse the repository at this point in the history
Cache `page_title` result to allow for
  • Loading branch information
JuanitoFatas committed Nov 9, 2015
2 parents 0ce8772 + dadee5d commit 4d90e0e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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

0 comments on commit 4d90e0e

Please sign in to comment.