From a09f0fbf04ca852496627ca4082380e7ac3565c9 Mon Sep 17 00:00:00 2001 From: David Knorr Date: Fri, 15 May 2009 11:19:55 +0200 Subject: [PATCH] Added ActionView::Helpers::TitleHelper module containing a title method for generating the tag: title('Welcome') # => Welcome title(:site => 'Site') # => <title>Site - Welcome More configuration options are available. Check out the documentation. --- actionpack/lib/action_view/helpers.rb | 1 + actionpack/lib/action_view/helpers/title_helper.rb | 46 ++++++++++++++++++++ actionpack/test/template/title_helper_test.rb | 37 ++++++++++++++++ 3 files changed, 84 insertions(+), 0 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/title_helper.rb create mode 100644 actionpack/test/template/title_helper_test.rb diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index 693ab7c..ba29175 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -20,6 +20,7 @@ module ActionView #:nodoc: autoload :ScriptaculousHelper, 'action_view/helpers/scriptaculous_helper' autoload :TagHelper, 'action_view/helpers/tag_helper' autoload :TextHelper, 'action_view/helpers/text_helper' + autoload :TitleHelper, 'action_view/helpers/title_helper' autoload :TranslationHelper, 'action_view/helpers/translation_helper' autoload :UrlHelper, 'action_view/helpers/url_helper' diff --git a/actionpack/lib/action_view/helpers/title_helper.rb b/actionpack/lib/action_view/helpers/title_helper.rb new file mode 100644 index 0000000..40a4a13 --- /dev/null +++ b/actionpack/lib/action_view/helpers/title_helper.rb @@ -0,0 +1,46 @@ +module ActionView + module Helpers #:nodoc: + # Provides a method to generate the HTML tag. + module TitleHelper + + # Called with no options and one argument, title will simply save and + # return whatever you pass to it. Called with one or more options, title + # will return a <title> tag containing the previously saved value, a + # separator, and a site name specified in the <tt>site</tt> option: + # + # title('Welcome') + # # => Welcome + # + # title(:site => 'Site') + # # => <title>Site - Welcome + # + # Use the separator option to specify a custom separator: + # + # title('Welcome') + # title(:site => 'Site', :separator => ' / ') + # # => Site / Welcome + # + # Use the reverse option to reverse the order of saved value and + # site name: + # + # title('Welcome') + # title(:site => 'Site', :reverse => true) + # # => Welcome - Site + def title(*args) + options = args.extract_options! + options.reverse_merge!(:separator => ' - ') + title = args.first + if title + @_title = title + else + "" + + "#{h(options[:reverse] ? @_title : options[:site])}" + + "#{options[:separator] if @_title && options[:site]}" + + "#{h(options[:reverse] ? options[:site] : @_title)}" + + "" + end + end + end + end +end + diff --git a/actionpack/test/template/title_helper_test.rb b/actionpack/test/template/title_helper_test.rb new file mode 100644 index 0000000..5b51cd0 --- /dev/null +++ b/actionpack/test/template/title_helper_test.rb @@ -0,0 +1,37 @@ +require 'abstract_unit' + +class TitleHelperTest < ActionView::TestCase + tests ActionView::Helpers::TitleHelper + + def test_title_with_one_argument + assert_equal 'Welcome', title('Welcome') + end + + def test_title_with_unset_title + assert_equal 'Site', title(:site => 'Site') + end + + def test_title_with_title_set + title('Welcome') + assert_equal 'Site - Welcome', title(:site => 'Site') + end + + def title_reverse_option + title('Welcome') + options = {:site => 'Site', :reverse => true} + assert_equal 'Welcome - Site', title(options) + end + + def test_title_separator_option + title('Welcome') + options = {:site => 'Site', :separator => ' / '} + assert_equal 'Site / Welcome', title(options) + end + + def test_title_reverse_and_separator_options + title('Welcome') + options = {:site => 'Site', :reverse => true, :separator => ' | '} + assert_equal 'Welcome | Site', title(options) + end +end + -- 1.5.6.3