lib/locomotive/wagon/server/dynamic_assets.rb in locomotivecms_wagon-1.1.0 vs lib/locomotive/wagon/server/dynamic_assets.rb in locomotivecms_wagon-1.2.0

- old
+ new

@@ -1,25 +1,32 @@ module Locomotive::Wagon class Server class DynamicAssets < Middleware - def call(env) - self.set_accessors(env) + attr_reader :app, :sprockets, :regexp - path = env['PATH_INFO'] + def initialize(app, root) + super(app) - if path =~ /^\/(stylesheets|javascripts)\// + @regexp = /^\/(javascripts|stylesheets)\/(.*)$/ - mime_type = MIME::Types.type_for(path).first.try(:to_s) || 'text/plain' - asset = self.mounting_point.theme_assets.detect do |_asset| - _asset.path == path - end + # make sure Compass is correctly configured + Locomotive::Mounter::Extensions::Compass.configure(root) - if asset - [200, { 'Content-Type' => mime_type }, [asset.content!]] - else - [404, { 'Content-Type' => mime_type }, ['Asset not found']] + @sprockets = Sprockets::Environment.new + @sprockets.append_path File.join(root, 'public/stylesheets') + @sprockets.append_path File.join(root, 'public/javascripts') + end + + def call(env) + if env['PATH_INFO'] =~ self.regexp + env['PATH_INFO'] = $2 + + begin + self.sprockets.call(env) + rescue Exception => e + raise Locomotive::Wagon::DefaultException.new "Unable to serve a dynamic asset. Please check the logs.", e end else app.call(env) end end \ No newline at end of file