Skip to content

Instantly share code, notes, and snippets.

@spllr
Created October 15, 2012 10:14
Show Gist options
  • Save spllr/3891814 to your computer and use it in GitHub Desktop.
Save spllr/3891814 to your computer and use it in GitHub Desktop.
Padrino Sprockets Plugin
module SprocketsInitializer
def self.registered(app)
require "sprockets"
app.controllers :assets do
helpers do
def sprockets_env
return @sprockets_environment if @sprockets_environment
@sprockets_environment = Sprockets::Environment.new(Padrino.root)
@sprockets_environment.append_path 'app/assets/javascripts'
@sprockets_environment.append_path 'app/assets/stylesheets'
@sprockets_environment.append_path 'app/assets/images'
# @sprockets_environment.engines[".coffee"].default_bare=true
@sprockets_environment.js_compressor = Uglifier.new(mangle: true)
@sprockets_environment
end
def asset_glob(type)
dir = case type
when :js then "javascripts"
when :css then "stylesheets"
else
"images"
end
Padrino.root("app/assets/#{dir}/**/*")
end
def assets_digest
Digest::MD5.hexdigest (env["PATH_INFO"] + Dir[asset_glob(content_type)].map { |f| File.mtime(f).to_s }.join)
end
end
get :path, :map => "/assets/*splat", :provides => [:js, :css, :png, :jpg, :tiff] do
etag assets_digest
env['PATH_INFO'].gsub!(%r{^/?assets}, '')
sprockets_env.call(env)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment