Created
July 15, 2010 14:56
-
-
Save paul/477054 to your computer and use it in GitHub Desktop.
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
require 'find' | |
module BundleHelper | |
# Use this to indicate the local name of a js lib, and a publicly hosted one. The local (unminimized) will be included in development mode, and the public one used for production | |
# | |
# Examples: | |
# | |
# != javascript_dev ['jquery-1.4.1', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js'] | |
# != javascript_dev ['jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js'] | |
# | |
def javascript_dev(*sources) | |
output = "" | |
sources = sources.to_a | |
sources.each do |pair| | |
output << javascript_src_tag(Rails.env == 'development' ? "dev/#{pair[0]}" : pair[1], {}) | |
end | |
output | |
end | |
# This will include every file under 'public/javascript/{source}/*'. In development mode, it will have one <script> tag per file. In production, it will have one script tag for 'public/javascript/{source}.js' | |
# | |
# Example: | |
# | |
# != javascript_bundle 'common' | |
# | |
def javascript_bundle(*sources) | |
sources = sources.to_a | |
bundle_files? ? javascript_include_bundles(sources) : javascript_include_files(sources) | |
end | |
# This method assumes you have manually bundled js using a rake command | |
# or similar. So there better be bundle_* files. | |
def javascript_include_bundles(bundles) | |
output = "" | |
bundles.each do |bundle| | |
output << javascript_src_tag("bundle_#{bundle}", {}) + "\n" | |
end | |
output | |
end | |
def javascript_include_files(bundles) | |
output = "" | |
bundles.each do |bundle| | |
files = recursive_file_list("public/javascripts/#{bundle}", ".js") | |
files.each do |file| | |
file = file.gsub('public/javascripts/', '') | |
output << javascript_src_tag(file, {}) + "\n" | |
end | |
end | |
output | |
end | |
def bundle_files? | |
Rails.env == 'production' || Rails.env == 'staging' || params[:bundle] || cookies[:bundle] == "yes" | |
end | |
def recursive_file_list(basedir, extname) | |
Dir["#{basedir}/**/*#{extname}"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment