lib/rack/jquery_ui.rb in rack-jquery_ui-1.1.0 vs lib/rack/jquery_ui.rb in rack-jquery_ui-2.0.0

- old
+ new

@@ -9,62 +9,92 @@ include JQuery::Helpers # The usual file name. JQUERY_UI_FILE_NAME = "jquery-ui.min.js" - # Script tags for the Media Temple CDN - MEDIA_TEMPLE = "<script src='http://code.jquery.com/ui/#{JQUERY_UI_VERSION}/jquery-ui.js'></script>" - # Script tags for the Google CDN - GOOGLE = "<script src='//ajax.googleapis.com/ajax/libs/jqueryui/#{JQUERY_UI_VERSION}/jquery-ui.min.js'></script>" + # Namespaced for convenience, to help with checking + # which CDN supports what. + module CDNs - # Script tags for the Microsoft CDN - MICROSOFT = "<script src='//ajax.aspnetcdn.com/ajax/jquery.ui/#{JQUERY_UI_VERSION}/jquery-ui.min.js'></script>" + # Script tags for the Media Temple CDN + MEDIA_TEMPLE = "http://code.jquery.com/ui/#{JQUERY_UI_VERSION}/jquery-ui.js" + + # Script tags for the Google CDN + GOOGLE = "//ajax.googleapis.com/ajax/libs/jqueryui/#{JQUERY_UI_VERSION}/jquery-ui.min.js" + + # Script tags for the Microsoft CDN + MICROSOFT = "//ajax.aspnetcdn.com/ajax/jquery.ui/#{JQUERY_UI_VERSION}/jquery-ui.min.js" + + + # Script tags for the Cloudflare CDN + CLOUDFLARE = "//cdnjs.cloudflare.com/ajax/libs/jqueryui/#{JQUERY_UI_VERSION}/jquery-ui.min.js" + end - - # Script tags for the Cloudflare CDN - CLOUDFLARE = "<script src='//cdnjs.cloudflare.com/ajax/libs/jqueryui/#{JQUERY_UI_VERSION}/jquery-ui.min.js'></script>" - - # This javascript checks if the jQuery-UI object has loaded. If not, that most likely means the CDN is unreachable, so it uses the local minified jQuery. FALLBACK = <<STR <script type="text/javascript"> !window.jQuery.ui && document.write(unescape("%3Cscript src='/js/jquery-ui/#{JQUERY_UI_VERSION}/#{JQUERY_UI_FILE_NAME}' type='text/javascript'%3E%3C/script%3E")) </script> STR # @param [Symbol] organisation Choose which CDN to use, either :google, :microsoft or :media_temple # @return [String] The HTML script tags to get the CDN. - def self.cdn( organisation=:google ) - script = case organisation - when :media_temple then MEDIA_TEMPLE - when :microsoft then MICROSOFT - when :cloudflare then CLOUDFLARE - else GOOGLE + def self.cdn( organisation=:google, options={} ) + raise = (opt = options[:raise]).nil? ? + raise? : + opt + script = if organisation === :media_temple + CDNs::MEDIA_TEMPLE + elsif organisation === :microsoft + CDNs::MICROSOFT + elsif (organisation === :cloudflare) && raise + fail "The Cloudflare CDN does not support version jQuery UI version #{JQUERY_UI_VERSION}." + else + CDNs::GOOGLE end - "#{script}\n#{FALLBACK}" + "<script src='#{script}'></script>\n#{FALLBACK}" end # Default options hash for the middleware. DEFAULT_OPTIONS = { - :http_path => "/js/jquery-ui/#{JQUERY_UI_VERSION}" + :http_path => "/js/jquery-ui/#{JQUERY_UI_VERSION}", + :raise => false } + # Whether to raise an exception if the chosen CDN + # does not support the jQuery UI version this library is using + # @param [TrueClass] bool + def self.raise=( bool ) + @raise = bool + end + + + # @see raise= + # @return [TrueClass] + def self.raise? + @raise = false if @raise.nil? + @raise + end + + # @param [#call] app # @param [Hash] options # @option options [String] :http_path If you wish the jQuery fallback route to be "/js/jquery-ui/1.10.1/jquery-ui.min.js" (or whichever version this is at) then do nothing, that's the default. If you want the path to be "/assets/javascripts/jquery-ui/1.10.1/jquery-ui.min.js" then pass in `:http_path => "/assets/javascripts/#{Rack::JQueryUI::JQUERY_UI_VERSION}". # @note ***Don't leave out the version number!***. The scripts provided by jQuery don't contain the version in the filename like the jQuery scripts do, which means that organising them and sending the right headers back is bound to go wrong unless you put the version number somewhere in the route. You have been warned! + # @option options [TrueClass] :raise If one of the CDNs does not support then raise an error if it is chosen. Defaults to false. # @example # # The default: # use Rack::JQueryUI # # With a different route to the fallback: # use Rack::JQueryUI, :http_path => "/assets/js/#{Rack::JQueryUI::JQUERY_UI_VERSION}" def initialize( app, options={} ) @app, @options = app, DEFAULT_OPTIONS.merge(options) @http_path_to_jquery = ::File.join @options[:http_path], JQUERY_UI_FILE_NAME + self.class.raise = @options[:raise] end # @param [Hash] env Rack request environment hash. def call( env )