Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created September 21, 2013 15:25
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 apeiros/6651575 to your computer and use it in GitHub Desktop.
Save apeiros/6651575 to your computer and use it in GitHub Desktop.
A rails LayoutHelper
module LayoutHelper
ActionMapping = {
'create' => 'new',
'update' => 'edit',
}
private
def rendered_action
@_rendered_action ||= begin
action_name = controller.action_name
ActionMapping.fetch(action_name, action_name)
end
end
def page_body_id
"page_#{controller.controller_path}/#{rendered_action}".tr('/','_')
end
def include_template_stylesheet
path = "views/#{controller.controller_path}/#{rendered_action}"
glob_pattern = "app/assets/stylesheets/#{path}.{css,sass,erb,css.sass}"
if Dir.glob(glob_pattern).empty?
"<!-- No view specific stylesheet (#{glob_pattern}) -->".html_safe
else
stylesheet_link_tag path
end
end
def include_template_javascript
path = "views/#{controller.controller_path}/#{rendered_action}"
glob_pattern = "app/assets/javascripts/#{path}.{js,erb}"
if Dir.glob(glob_pattern).empty?
"<!-- No view specific javascript (#{glob_pattern}) -->".html_safe
else
javascript_include_tag path
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment