Skip to content

Instantly share code, notes, and snippets.

@chrisjpowers
Created June 10, 2010 22:07
Show Gist options
  • Save chrisjpowers/433713 to your computer and use it in GitHub Desktop.
Save chrisjpowers/433713 to your computer and use it in GitHub Desktop.
module AutoAssetsHelper
# Outputs script and link tags for js/css based on
# the current action, controller and layout. This
# should be preferred over adding assets within a
# view file for specific views/layouts/controllers.
#
# Example: If UsersController#dashboard is requested
# and it uses the 'front' layout, these files will be
# included if they exist:
#
# javascripts/users.js
# javascripts/users/dashboard.js
# javascripts/layouts/front.js
# stylesheets/users.js
# stylesheets/users/dashboard.js
# stylesheets/layouts/front.js
#
def auto_assets
out = []
c = params[:controller]
a = params[:action]
l = response.layout
[l, c, "#{c}/#{a}"].each do |name|
if File.exist?("public/stylesheets/#{name}.css")
out << stylesheet_link_tag(name)
end
if File.exist?("public/javascripts/#{name}.js")
out << javascript_include_tag(name)
end
end
out.join("\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment