Module: Motr::Helpers::LayoutHelpers

Defined in:
lib/motr/helpers/layout_helpers.rb

Overview

# Convenience helpers generally used in layout files

Instance Method Summary collapse

Instance Method Details

#include_javascripts(*args) ⇒ String

Include javascript libraries from local and external resources. This method extends javascript_include_tag to support loading libraries from sources such as Google’s CDN, or javascripts packaged by motr

When loading libraries from Google CDN, uncompressed versions will be used in any environment besides production. In production environments, minified versions will be used.

Examples:

include_javascripts(:jquery => '5.0', :source => :google) #=> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"...

Parameters:

  • *args (Array)

    An argument list containing a the libraries to load, and any load options

Returns:

  • (String)

    A javscript “script” tag



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/motr/helpers/layout_helpers.rb', line 22

def include_javascripts(*args)
  
  options = args.extract_options!
  source  = options.delete(:source)
  cache   = options.delete(:cache) || false

  if !source.nil? && source.to_s.match(/google/i) && !args.empty?
    raise Motr::Errors::InvalidOptions.new("When using Google as a :source, all arguments must be in the hash format { :library => :version }.") 
  elsif source.nil? && args.empty? && !options.empty?
    raise Motr::Errors::InvalidOptions.new("Missing :source attribute for `include_javascripts`. When passing a library/version hash as an argument, a source (cdn) is required.")
  end
  
  response = ""

  unless source.nil?
    options.stringify_keys!.each do |lib| 
      response << (:script, "", :src => js_load_path(*lib), :type => 'text/javascript')
    end          
  end
  
  args.map(&:to_s).each{ |lib| response << (:script, "", :src => "/javascripts/#{lib}.js", :type => 'text/javascript') }
  response.html_safe
  
end

#meta_tag(name, content = nil) ⇒ String

Allows easy assigning of meta tags from templates

Parameters:

  • name (Symbol)

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

  • content (String) (defaults to: nil)

    The content to be used in the meta tag

Returns:

  • (String)

    A html meta tag with the name and content attributes set



54
55
56
57
58
59
# File 'lib/motr/helpers/layout_helpers.rb', line 54

def meta_tag(name, content = nil)
  return _retrieve_variable(:"__#{name}") if content.nil?
  content = tag(:meta, :name => name, :content => content)
  _create_variable(:"__#{name}", content, false)
  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



78
79
80
81
82
83
84
# File 'lib/motr/helpers/layout_helpers.rb', line 78

def page_id(content = nil)
  _create_variable(:__page_id, content, false) and return unless content.nil?
  return _retrieve_variable(:__page_id) if content_for?(:__page_id)
  cname = controller.class.to_s.gsub(/controller$/i,'').underscore.split("/").join('_')
  aname = controller.action_name
  "#{cname}_#{aname}"
end

#page_title(content = nil) ⇒ String

Convenienve 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



98
99
100
101
102
# File 'lib/motr/helpers/layout_helpers.rb', line 98

def page_title(content = nil)
  _create_variable(:__page_title, content, false) and return unless content.nil?
  return _retrieve_variable(:__page_title) if content_for?(:__page_title)
  return ""
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



130
131
132
# File 'lib/motr/helpers/layout_helpers.rb', line 130

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

#set_var(var_name, content) ⇒ String

Convenience method for content_for allowing for single-line variable creation. Also defines a method that can be re-used within a template.

Examples:

Setting a “page_class” variable

<% set_var(:page_class, 'cool') %>
<%= page_class %> #=> 'cool'

Parameters:

  • var_name (Symbol)

    The name of the variable to create

  • content (String)

    The content that variable defines

Returns:

  • (String)

    An empty string. Use the newly created method from var_name to return the value



118
119
120
# File 'lib/motr/helpers/layout_helpers.rb', line 118

def set_var(var_name, content)
  _create_variable("#{var_name}", content) and return ''
end