diff --git a/lib/rails_utils.rb b/lib/rails_utils.rb index 97dbeb0..6f271ed 100644 --- a/lib/rails_utils.rb +++ b/lib/rails_utils.rb @@ -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 = {}) diff --git a/test/rails_utils_test.rb b/test/rails_utils_test.rb index 43fb2a5..3b2b527 100644 --- a/test/rails_utils_test.rb +++ b/test/rails_utils_test.rb @@ -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