Skip to content

Instantly share code, notes, and snippets.

@daz4126
Last active October 4, 2015 03:27
Show Gist options
  • Save daz4126/2569507 to your computer and use it in GitHub Desktop.
Save daz4126/2569507 to your computer and use it in GitHub Desktop.
require 'sinatra/base'
module Sinatra
module JavaScripts
def js *scripts
@js ||= []
@js = scripts
end
def javascripts(*args)
js = []
js << settings.javascripts if settings.respond_to?('javascripts')
js << args
js << @js if @js
js.flatten.uniq.map do |script|
"<script src=\"#{path_to script}\"></script>"
end.join
end
def path_to script
case script
when :jquery then 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
when :rightjs then 'http://cdn.rightjs.org/right-2.3.0.js'
when :backbone then 'http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.0/backbone-min.js'
when :underscore then 'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.1/underscore-min.js'
else script.to_s + '.js'
end
end
end
helpers JavaScripts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment