module JqueryCdn

Constants

URL
VERSION

Public Class Methods

include_jquery(options = { }) click to toggle source

Return <script> tags with jQuery.

# File lib/jquery-cdn.rb, line 58
def self.include_jquery(options = { })
  attrs = options.dup
  env   = attrs.delete(:env) || :production
  cdn   = attrs.delete(:cdn) || :google

  attrs[:src] = url(env, cdn)

  script_tag(attrs) + if not options[:defer] and env == :production
    fallback = include_jquery(options.merge(env: :development))
    escaped  = "unescape('#{ fallback.gsub('<', '%3C') }')"
    script_tag("window.jQuery || document.write(#{ escaped })")
  else
    ''
  end
end
install(sprockets) click to toggle source

Add assets paths to standalone Sprockets environment.

# File lib/jquery-cdn.rb, line 20
def self.install(sprockets)
  root = Pathname(__FILE__).dirname.join('..').expand_path
  sprockets.append_path(root.join('vendor/assets/javascripts'))
end
local_url=(proc) click to toggle source

Set proc to generate locale jQuery URL

# File lib/jquery-cdn.rb, line 36
def self.local_url=(proc)
  @local_url = proc
end
script_tag(attrs, body = '') click to toggle source

Return <script> tag

# File lib/jquery-cdn.rb, line 41
def self.script_tag(attrs, body = '')
  if attrs.is_a? String
    body  = attrs
    attrs = { }
  end

  attrs = attrs.map { |key, value|
    if value == true
      " #{key}"
    else
      " #{key}=\"#{value}\""
    end
  }.join
  "<script#{ attrs }>#{ body }</script>"
end
url(env, cdn) click to toggle source

Return URL to local or CDN jQuery, depend on `env`.

# File lib/jquery-cdn.rb, line 26
def self.url(env, cdn)
  if env == :production
    raise ArgumentError, "Unknown CDN #{cdn}" unless URL.has_key? cdn
    URL[cdn]
  else
    @local_url.call
  end
end