Enumerate deferred scripts with a sprockets helper

This commit is contained in:
M Smart 2014-07-22 19:46:43 -04:00
parent aad5d439d0
commit ec280913b2
2 changed files with 21 additions and 5 deletions

View File

@ -1,11 +1,7 @@
<script>
window.assetPath = (function(){
// TODO: automate this to grab from the manifest, Rails voodoo should be able to get it
var map = {
'defer/html-sanitizer-bundle': <%= asset_path('defer/html-sanitizer-bundle.js').inspect.html_safe %>,
'defer/google_diff_match_patch': <%= asset_path('defer/google_diff_match_patch.js').inspect.html_safe %>
};
var map = <%= deferred_javascripts %>;
return function(asset){ return map[asset]; };
})();

View File

@ -0,0 +1,20 @@
module DeferredJavascriptsHelper
# Provides a javascript map of the files in the 'defer' directory
def deferred_javascripts
files = {}
Dir.glob("#{Rails.root}/app/assets/javascripts/defer/*.js").each do |file|
module_name = "defer/#{File.basename(file, '.js')}"
file_name = asset_path("defer/#{File.basename(file)}")
files[module_name] = file_name
end
return files.to_json.html_safe
end
end
module Sprockets::Rails::Helper
include DeferredJavascriptsHelper
end