Class: Rack::JQueryUI

Inherits:
Object
  • Object
show all
Includes:
JQuery::Helpers
Defined in:
lib/rack/jquery_ui.rb,
lib/rack/jquery_ui/version.rb

Overview

jQuery CDN script tags and fallback in one neat package.

Defined Under Namespace

Modules: CDNs

Constant Summary collapse

JQUERY_UI_FILE_NAME =

The usual file name.

"jquery-ui.min.js"
FALLBACK_PATH =

path to the fallback script

"/js/jquery-ui/#{JQUERY_UI_VERSION}/#{JQUERY_UI_FILE_NAME}"
FALLBACK =

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.

<<STR
<script type="text/javascript">
  !window.jQuery.ui && document.write(unescape("%3Cscript src='#{FALLBACK_PATH}' type='text/javascript'%3E%3C/script%3E"))
</script>
STR
DEFAULT_OPTIONS =

Default options hash for the middleware.

{
  :http_path  => "/js/jquery-ui/#{JQUERY_UI_VERSION}",
  :raise      =>  false
}
VERSION =

library version

"3.0.2"
JQUERY_UI_VERSION =

version of jQuery-UI script.

jquery_ui_version
JQUERY_UI_VERSION_DATE =

This is the release date of the jQuery file, it makes an easy “Last-Modified” date for setting the headers around caching. TODO remember to change Last-Modified with each release!

"Fri, 03 May 2013 00:00:00 GMT"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ JQueryUI

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!

Returns a new instance of JQueryUI.

Examples:

# The default:
use Rack::JQueryUI
# With a different route to the fallback:
use Rack::JQueryUI, :http_path => "/assets/js/#{Rack::JQueryUI::JQUERY_UI_VERSION}"

Parameters:

  • app (#call)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :http_path (String)

    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/#JQUERY_UI_VERSION”.

  • :raise (TrueClass)

    If one of the CDNs does not support then raise an error if it is chosen. Defaults to false.



119
120
121
122
123
124
# File 'lib/rack/jquery_ui.rb', line 119

def initialize( app, options={} )
  @app, @options  = app, DEFAULT_OPTIONS.merge(options)
  @http_path_to_jquery = ::File.join @options[:http_path], JQUERY_UI_FILE_NAME
  @raise = @options.fetch :raise, false
  @organisation = options.fetch :organisation, :media_temple
end

Class Method Details

.cdn(env, options = {}) ⇒ String

Returns The HTML script tags to get the CDN.

Examples:

# in a Haml file… (but could be any type of template)
# For the default
Rack::JQueryUI.cdn env

# Choose the organisation
Rack::JQueryUI.cdn env, :organisation => :cloudflare

# Raise an error if the organisation doesn't
# support this version of jQuery UI
Rack::JQueryUI.cdn env, :raise => true

# Use the unminified version from the CDN
Rack::JQueryUI.cdn env, :debug => true

Parameters:

  • env (Hash)

    The rack env hash.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :organisation (Symbol, false, nil)

    Choose which CDN to use, either :google, :microsoft or :media_temple, or :cloudflare. Pass in ‘false` to force use of the local jQuery script. `nil` will force choosing the default CDN.

  • :debug (true, false)

    If you need the unminified version from the CDN for debugging then pass this in. The unminified scripts aren’t included via the fallback to keep the library light, so this won’t change anything if fallback is used.

Returns:

  • (String)

    The HTML script tags to get the CDN.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rack/jquery_ui.rb', line 65

def self.cdn( env, options={} )
  if env.nil? || env.has_key?(:organisation)
    fail ArgumentError, "The Rack::JQueryUI.cdn method needs the Rack environment passed to it, or at the very least, an empty hash."
  end
  raise = (opt = options[:raise]).nil? ?
            opt :
            (env["rack.jquery_ui.raise"] || false)

  organisation =  options[:organisation]
  if organisation.nil? # because false is valid
    organisation = env["rack.jquery_ui.organisation"] ||
                    :media_temple
  end

  unless organisation == false
    script_src = if organisation === :media_temple         
        CDNs::MEDIA_TEMPLE
      elsif organisation === :microsoft
        CDNs::MICROSOFT
      elsif organisation === :cloudflare
        CDNs::CLOUDFLARE
      elsif organisation === :google
        CDNs::GOOGLE
      else
        CDNs::MEDIA_TEMPLE
    end
      
    debug = options.fetch :debug, false
  
    script_src = "#{script_src[0..-7]}js" if debug
    "<script src='#{script_src}'></script>\n#{FALLBACK}"
  else
    "<script src='#{FALLBACK_PATH}'></script>"
  end
end

Instance Method Details

#_call(env) ⇒ Object

For thread safety

Parameters:

  • env (Hash)

    Rack request environment hash.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rack/jquery_ui.rb', line 135

def _call( env )
  request = Rack::Request.new(env.dup)
  env.merge! "rack.jquery_ui.organisation" => @organisation
  env.merge! "rack.jquery_ui.raise" => @raise
  if request.path_info == @http_path_to_jquery
    response = Rack::Response.new
    # for caching
    response.headers.merge! caching_headers( "#{JQUERY_UI_FILE_NAME}-#{JQUERY_UI_VERSION}", JQUERY_UI_VERSION_DATE)

    # There's no need to test if the IF_MODIFIED_SINCE against the release date because the header will only be passed if the file was previously accessed by the requester, and the file is never updated. If it is updated then it is accessed by a different path.
    if request.env['HTTP_IF_MODIFIED_SINCE']
      response.status = 304
    else
      response.status = 200
      response.write ::File.read( ::File.expand_path "../../../vendor/assets/javascripts/jquery-ui/#{JQUERY_UI_VERSION}/#{JQUERY_UI_FILE_NAME}", __FILE__)
    end
    response.finish
  else
    @app.call(env)
  end
end

#call(env) ⇒ Object

Parameters:

  • env (Hash)

    Rack request environment hash.



128
129
130
# File 'lib/rack/jquery_ui.rb', line 128

def call( env )
  dup._call env
end