Skip to content

Instantly share code, notes, and snippets.

@jcf
Created July 28, 2010 11:42
Show Gist options
  • Save jcf/494167 to your computer and use it in GitHub Desktop.
Save jcf/494167 to your computer and use it in GitHub Desktop.
Keep your javascripts in app/javascripts. Read and serve in development and package before deployment
# In config/environments/development.rb
config.middleware.use 'DevelopmentJavascript'
# lib/development_javascript.rb
class DevelopmentJavascript
def initialize(app)
@app = app
end
def call(env)
path = Rack::Utils.unescape(env['REQUEST_PATH'])
return @app.call(env) unless path.match(%r{/app/javascripts/(.*\.js)})
javascript_file = ::File.expand_path("../../app/javascripts/#{$1}", __FILE__)
if ::File.exist?(javascript_file)
size = ::File.size(javascript_file).to_s
content = ::File.new(javascript_file, 'r').read
headers = {'Content-Type' => 'text/javascript', 'Content-Length' => size}
[200, headers, [content]]
else
@app.call(env)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment