public
Last active

Provides helpers for managing page title.

  • Download Gist
title_helper.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#
# = Rails Title Helper
#
# Provides helpers for managing page title.
#
# Category:: Rails
# Package:: Helpers
# Author:: Simone Carletti <weppos@weppos.net>
# 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
title_helper_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.