Skip to content

Commit

Permalink
Add logic to ApplicationHelper#title
Browse files Browse the repository at this point in the history
  • Loading branch information
atd committed Feb 26, 2009
1 parent 06268b3 commit 3697325
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
37 changes: 30 additions & 7 deletions app/helpers/application_helper.rb
Expand Up @@ -9,13 +9,36 @@ module ApplicationHelper

# Get title in this order:
# 1. string argument
# 2. class variable <tt>@title</tt>, typically assigned in the Controller
# 3. <tt>controller.controller_name</tt> - <tt>site.name</tt>
def title(new_title = "" )
title = new_title.present? ?
new_title :
@title ||
"#{ controller.controller_name.titleize } - #{ site.name }"
# 2. @title instance variable
# 3. Title based on variables set by the Controller
# 4. <tt>controller.controller_name</tt> - <tt>site.name</tt>
#
# Options:
# <tt>append_site_name</tt>:: Append the Site name to the title, ie, "Title - Example Site". Defaults to <tt>false</tt>
#
def title(new_title = "", options = {})
title = if new_title.present?
new_title
elsif @title
@title
elsif @contents
container ?
t(:other_in_container, :scope => controller.controller_name.singularize) :
t(:other, :scope => controller.controller_name.singularize)
elsif @resources
t(:other, :scope => controller.controller_name.singularize)
elsif @resource
if @resource.new_record?
t(:new, :scope => @resource.class.to_s.underscore)
elsif controller.action_name == 'edit' || @resource.errors.any?
t(:editing, :scope => @resource.class.to_s.underscore)
else
@resource.title
end
else
controller.controller_name.titleize
end
title << " - #{ site.name }" if options[:append_site_name]

sanitize(title)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Expand Up @@ -2,7 +2,7 @@
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><%= title %></title>
<title><%= title :append_site_name => true %></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="<%= @description %>" />
<meta name="keywords" content="red,social" />
Expand Down

0 comments on commit 3697325

Please sign in to comment.