Module: Motr::Helpers::LayoutHelpers
- Defined in:
- lib/motr/helpers/layout_helpers.rb
Overview
# Convenience helpers generally used in layout files
Instance Method Summary collapse
-
#include_javascripts(*args) ⇒ String
Include javascript libraries from local and external resources.
-
#meta_tag(name, content = nil) ⇒ String
Allows easy assigning of meta tags from templates.
-
#page_id(content = nil) ⇒ String
Creates a page id to be used for identifying a page for CSS/design purposes.
-
#page_title(content = nil) ⇒ String
Convenienve method to create a page title for the <title></title> tag.
-
#robot_meta_tag ⇒ String
Configures a “robots” meta tag based on the rails environment.
-
#set_var(var_name, content) ⇒ String
Convenience method for content_for allowing for single-line variable creation.
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.
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) = args. source = .delete(:source) cache = .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? && !.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? .stringify_keys!.each do |lib| response << content_tag(:script, "", :src => js_load_path(*lib), :type => 'text/javascript') end end args.map(&:to_s).each{ |lib| response << content_tag(: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
54 55 56 57 58 59 |
# File 'lib/motr/helpers/layout_helpers.rb', line 54 def (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.
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.
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_tag ⇒ String
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.
130 131 132 |
# File 'lib/motr/helpers/layout_helpers.rb', line 130 def 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.
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 |