Skip to content

Instantly share code, notes, and snippets.

@brentkirby
Last active April 26, 2018 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentkirby/28b8a7c408de048e9befb4c2ef52989f to your computer and use it in GitHub Desktop.
Save brentkirby/28b8a7c408de048e9befb4c2ef52989f to your computer and use it in GitHub Desktop.
Various helpers for populating layout data.
module LayoutHelper
##
# Render flash messages
#
# @param [String] attrs Attributes to add to the surrounding <div>
#
def flash_messages(attrs = {})
return if flash.nil? or flash.empty?
closer = "<span class='close'>&times;</span>"
klasses = (attrs.delete(:class) || "").split(" ")
klasses << "flash-message"
content = ""
flash.each do |key, value|
next if value === true or value.to_s == 'true' # fix awkward devise message
klasses << "alert"
name = key.to_s.underscore
name = 'danger' if ['alert', 'error'].include?(key.to_s)
name = 'info' if ['notice'].include?(key.to_s)
klasses << "alert-#{name}"
msg_attrs = attrs.merge(class: [key.to_s, klasses].flatten.join(' '))
content.concat content_tag(:div, "<div class='container text-center'>#{value} #{closer}</div>".html_safe, msg_attrs).html_safe
end
content.html_safe
end
##
#
# 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.
#
# @param [String] content The string to be used as the page id.
# @return [String] The assigned page id
# @example Create a page_id and use it in the body tag
# <%= page_id('home') %>
#
# <body id="<%= page_id %>"> #=> <body id="home">
#
# @example Defaulting page id to controller/action
# # IndexController#home
# <body id="<%= page_id %>"> #=> <body id="index_home">
#
#
def page_id(content = nil)
return (@view_flow.get(:page_id).present? ? @view_flow.get(:page_id) : default_page_id_for_view) unless content
provide(:page_id, content) if content
end
##
#
# Convenience method to store and override metadata for a page
#
# @param [Symbol, String] The meta element to target
# @param [String] The content
# @return [String] The provided content
# @example Set the page title from a view template
# <%= page_description('A description for the page.') %>
#
# # In the layout:
# <meta name="keywords" content="<%= page_keywords %>" /> #=> <meta name="keywords" content="one, two, three" />
#
def page_meta(type, content = nil)
provide(:page_description, content) and return if content
return @view_flow.get(:"page_#{type.to_s}") unless content
""
end
##
#
# Convenience method to create a page title for the <title></title> tag.
#
# @param [String] content The text for the page title
# @return [String] The provided content
# @example 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>
#
def page_title(content = nil)
provide(:page_title, content) and return if content
return @view_flow.get(:page_title) unless content
""
end
##
#
# 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.
#
# @return [String] A html meta tag for "robots" with the appropriate content attribute set
#
def robot_meta_tag
is_prod = Rails.env.eql?('production')
tag(:meta, :name => 'robots', :content => (is_prod ? 'index, follow' : 'noindex, nofollow'))
end
##
# Capture a script block to render
# within the layout.
#
def scripts(&block)
if block_given?
@_script_data ||= []
@_script_data.push(capture(&block))
else
(@_script_data ||= []).join("\n").html_safe
end
end
private
##
# Constructs a default page id from the current
# controller and action
#
def default_page_id_for_view
if defined?(controller)
cname = controller.class.to_s.gsub(/controller$/i,'').underscore.split("/").join('_')
aname = controller.action_name
return "#{cname}_#{aname}"
elsif defined?(params) && (params || {})[:controller].present?
return [params[:controller].split("/").join("_"), params[:action]].compact.join("_")
elsif defined?(request) && request.respond_to?(:path_info)
::File.basename(request.path_info).to_s
end
""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment