Module: JqueryCdn

Defined in:
lib/jquery-cdn.rb,
lib/jquery-cdn/helpers.rb,
lib/jquery-cdn/version.rb,
lib/jquery-cdn/railties.rb

Defined Under Namespace

Modules: Helpers, RailsHelpers Classes: Engine, Railtie

Constant Summary collapse

URL =
{
  google:     "//ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js",
  microsoft:  "//ajax.aspnetcdn.com/ajax/jQuery/jquery-#{version}.min.js",
  jquery:     "http://code.jquery.com/jquery-#{version}.min.js",
  yandex:     "//yandex.st/jquery/#{version}/jquery.min.js",
  cloudflare: "//cdnjs.cloudflare.com/ajax/libs/jquery/#{version}/jquery.min.js"
}
VERSION =
"2.2.4"

Class Method Summary collapse

Class Method Details

.include_jquery(options = { }) ⇒ Object

Return <script> tags with jQuery.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object

Add assets paths to standalone Sprockets environment.



20
21
22
23
# 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) ⇒ Object

Set proc to generate locale jQuery URL



36
37
38
# File 'lib/jquery-cdn.rb', line 36

def self.local_url=(proc)
  @local_url = proc
end

.script_tag(attrs, body = '') ⇒ Object

Return <script> tag



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object

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



26
27
28
29
30
31
32
33
# 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