Module: Facades::Helpers::Layout

Included in:
Facades::Helpers
Defined in:
lib/facades/helpers/layout.rb

Overview

Convenience helpers generally used in layout files

Instance Method Summary collapse

Instance Method Details

#browser_nameObject

Returns a short-hand string identifying the current browser



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/facades/helpers/layout.rb', line 10

def browser_name
  uastr = request.user_agent.to_s
  return "webkit"  if uastr =~ /(webkit)[ \/]([\w.]+)/i
  return "opera"   if uastr =~ /(opera)(?:.*version)?[ \/]([\w.]+)/i
  if matches = uastr.match(/(msie) ([\w.]+)/i)
    version = (matches[2]||0).to_i
    return "ie#{version}"
  end
  return "mozilla" if uastr =~ /(mozilla)(?:.*? rv:([\w.]+))?/i    
  "unknown"
end

#google_analytics(site_id, &block) ⇒ String

Create a script tag for activating google analytics

Parameters:

  • site_id (String)

    The site ID provided by google analytics

Returns:

  • (String)

    script tag



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/facades/helpers/layout.rb', line 38

def google_analytics(site_id, &block)
  return "" if defined?(Rails) && Rails.env != "production"
  (:script) do          
    %Q{
      var _gaq=[['_setAccount','#{site_id}'],['_trackPageview']];
      (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
      g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
      s.parentNode.insertBefore(g,s)}(document,'script'));
      #{(block_given? ? capture(&block) : "")}
  	}
	end.html_safe
end

#meta_tag(name, content) ⇒ String

Allows easy assigning of meta tags from templates

Parameters:

  • name (Symbol)

    Name of the meta tag (ie: keywords / description)

  • content (String)

    The content to be used in the meta tag

Returns:

  • (String)

    A html meta tag with the name and content attributes set



29
30
31
# File 'lib/facades/helpers/layout.rb', line 29

def meta_tag(name, content)
  tag(:meta, :name => name, :content => content)
end

#page_id(content = nil) ⇒ String

Creates a page id to be used for identifying a page for CSS/design purposes. If a variable is defined, it will be used. If not, one will be generated from the current controller/action.

Examples:

Create a page_id and use it in the body tag

<%= page_id('home') %>

<body id="<%= page_id %>"> #=> <body id="home">

Defaulting page id to controller/action

# IndexController#home
<body id="<%= page_id %>"> #=> <body id="index_home">

Parameters:

  • content (String) (defaults to: nil)

    The string to be used as the page id.

Returns:

  • (String)

    The assigned page id



68
69
70
71
# File 'lib/facades/helpers/layout.rb', line 68

def page_id(content = nil)
  return (@view_flow.get(:page_id).present? ? @view_flow.get(:page_id) : default_page_title_for_view) unless content
  provide(:page_id, content) if content
end

#page_title(content = nil) ⇒ String

Convenience method to create a page title for the <title></title> tag.

Examples:

Set the page title from a view template

<%= page_title('This is my page title') %>

# In the layout:
<title><%= page_title %></title> #=> <title>This is my page title</title>

Parameters:

  • content (String) (defaults to: nil)

    The text for the page title

Returns:

  • (String)

    The provided content



85
86
87
88
89
# File 'lib/facades/helpers/layout.rb', line 85

def page_title(content = nil)
  provide(:page_title, content) and return if content
  return @view_flow.get(:page_title) unless content
  ""
end

#robot_meta_tagString

Configures a “robots” meta tag based on the rails environment. In environments other than ‘production’ it sets the value to “noindex, nofollow” for each page that uses the layout in which it is called. This helps prevent spiders from crawling / indexing content when used on staging sites.

Returns:

  • (String)

    A html meta tag for “robots” with the appropriate content attribute set



99
100
101
# File 'lib/facades/helpers/layout.rb', line 99

def robot_meta_tag
  tag(:meta, :name => 'robots', :content => (Rails.env.eql?('production') ? 'index, follow' : 'noindex, nofollow'))
end