Created
September 21, 2013 15:25
-
-
Save apeiros/6651575 to your computer and use it in GitHub Desktop.
A rails LayoutHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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