Module: TitleHelper

Defined in:
app/helpers/title_helper.rb

Overview

Each displayed html page has got a header title tag, i.e. <html><head><title>…</title></head>…</html>. For a typical page, the title should be something like this: “User ‘doe’ - YourAppName”, where the first part depends on the currently shown page, the second part just contains your app’s name. If no first part exists, just display the app’s name.

The first part of the title, i.e. the part specific to the currently shown page, is set in the html view of the currently shown page, since it is only required for html responses.

<!-- In the view, e.g. app/views/users/show.html.erb: -->
<% set_title("User '#{@user.alias}'") %>

In the layout, the full title can be refered to as ‘website_title_with_app_name`.

<!-- In the layout, e.g. app/views/layouts/application.html.erb: -->
<head><title><%= website_title_with_app_name %></title>...</head>

This behaviour is mostly inspired by Micheal Hartl’s rails tutorial:

https://github.com/mhartl/sample_app/tree/master/app/views
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Instance Method Summary collapse

Instance Method Details

#set_title(title) ⇒ Object



25
26
27
# File 'app/helpers/title_helper.rb', line 25

def set_title( title )
  set_website_title(title)
end

#set_website_title(title) ⇒ Object



28
29
30
# File 'app/helpers/title_helper.rb', line 28

def set_website_title( title )
  provide(:title, title)
end

#website_title_set_by_controllerObject



32
33
34
35
36
37
38
# File 'app/helpers/title_helper.rb', line 32

def website_title_set_by_controller
  # yield(:title) uses the new mechanism.
  # @title uses the old mechanism.
  # TODO: Remove @title when it is not used in the controllers anymore.
  #
  @title || content_for(:title)
end

#website_title_with_app_nameObject



40
41
42
43
44
45
46
47
48
49
50
# File 'app/helpers/title_helper.rb', line 40

def website_title_with_app_name
  if website_title_set_by_controller.present? 
    if website_title_set_by_controller.include?(application_name)
      return website_title_set_by_controller
    else
      return "#{website_title_set_by_controller} - #{application_name}"
    end
  else
    return application_name
  end
end