Created
August 3, 2008 20:46
-
-
Save weppos/3840 to your computer and use it in GitHub Desktop.
Provides helpers for managing page title.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# = Rails Title Helper | |
# | |
# Provides helpers for managing page title. | |
# | |
# Category:: Rails | |
# Package:: Helpers | |
# Author:: Simone Carletti <[email protected]> | |
# Copyright:: 2007-2008 The Authors | |
# License:: MIT License | |
# Link:: http://www.simonecarletti.com/ | |
# Source:: http://gist.github.com/2769 | |
# | |
module TitleHelper | |
# | |
# = Title Helper | |
# | |
# This helper provides both input and output capabilities. | |
# When called with +args+, it stores all args for later retrieval. | |
# Additionally, it always return a formatted title | |
# thus is can be easily called without arguments | |
# to display the full page title. | |
# | |
# You can pass some additional options to customize the behavior | |
# of the output string. | |
# The following options are supported: | |
# | |
# [<tt>:separator</tt>] | |
# the separator used to separe all elements of the title, | |
# by default a dash. | |
# | |
# [<tt>:site</tt>] | |
# the name of the site to be appended to the page title. | |
# | |
# [<tt>:headline</tt>] | |
# website headline to be appended to the page title, | |
# after any element. | |
# | |
# === Examples | |
# | |
# # in a template set the title of the page | |
# <h1><%= title "Latest news" %></h1> | |
# # => <h1>Latest news</h1> | |
# | |
# # in the layout print the title of the page | |
# # with the name of the site | |
# <title><%= title :site => 'My Site' %></title> | |
# # => <title>Latest news | My Site</title> | |
# | |
# | |
def title(*args) | |
@title_helper_title ||= [] | |
@title_helper_title_options ||= { | |
:separator => ' - ', | |
:headline => nil, | |
:site => nil, | |
} | |
options = args.extract_options! | |
@title_helper_title += args | |
@title_helper_title_options.merge!(options) | |
t = @title_helper_title.clone | |
t << @title_helper_title_options[:site] | |
t << @title_helper_title_options[:headline] | |
t.compact.join(@title_helper_title_options[:separator]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
require 'rubygems' | |
require 'active_support' | |
require 'title_helper' | |
class TitleHelperTest < Test::Unit::TestCase | |
include TitleHelper | |
def setup | |
reset! | |
end | |
def test_title_should_accepts_zero_or_more_params | |
assert_nothing_raised { title } | |
assert_nothing_raised { title('One') } | |
assert_nothing_raised { title('One', 'Two', 'Three') } | |
end | |
def test_title_should_return_content | |
assert_equal('', title) | |
assert_equal('One', title('One')) | |
end | |
def test_title_should_return_empty_string_if_empty | |
assert_equal('', title) | |
end | |
def test_title_should_store_content | |
assert_equal('', title) | |
assert_equal('One', title('One')) | |
assert_equal('One', title) | |
end | |
def test_title_should_join_content | |
assert_equal('', title) | |
assert_equal('One', title('One')) | |
assert_equal('One - Two', title('Two')) | |
assert_equal('One - Two - Three - Four', title('Three', 'Four')) | |
end | |
def test_title_should_join_content_with_separator | |
assert_equal('One - Two', title('One', 'Two')) | |
assert_equal('One | Two', title(:separator => ' | ')) | |
assert_equal('One x Two x Three', title('Three', :separator => ' x ')) | |
end | |
def test_title_should_append_headline_to_content | |
assert_equal('One - Two', title('One', 'Two')) | |
assert_equal('One - Two - Cool!', title(:headline => 'Cool!')) | |
assert_equal('One - Two - Three - Yeah!', title('Three', :headline => 'Yeah!')) | |
end | |
def test_title_should_append_site_to_content | |
assert_equal('One - Two', title('One', 'Two')) | |
assert_equal('One - Two - Cool!', title(:site => 'Cool!')) | |
assert_equal('One - Two - Three - Yeah!', title('Three', :site => 'Yeah!')) | |
end | |
def test_title_should_append_site_then_headline | |
assert_equal('One - Two', title('One', 'Two')) | |
assert_equal('One - Two - Cool!', title(:site => 'Cool!')) | |
assert_equal('One - Two - Cool! - Yeah!', title(:headline => 'Yeah!')) | |
end | |
protected | |
def reset! | |
title(nil) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment