Created
July 28, 2010 11:42
-
-
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
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
# In config/environments/development.rb | |
config.middleware.use 'DevelopmentJavascript' |
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
# 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