jquery-rails-cdn 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +68 -0
- data/Rakefile +2 -0
- data/jquery-rails-cdn.gemspec +19 -0
- data/lib/jquery-rails-cdn.rb +34 -0
- data/lib/jquery-rails-cdn/version.rb +7 -0
- metadata +70 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kenn Ejima
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# jquery-rails-cdn
|
2
|
+
|
3
|
+
Add CDN support to [jquery-rails](https://github.com/rails/jquery-rails).
|
4
|
+
|
5
|
+
Serving jQuery from a publicly available [CDN](http://en.wikipedia.org/wiki/Content_Delivery_Network) has clear benefits:
|
6
|
+
|
7
|
+
* **Speed**: Users will be able to download jQuery from the closest physical location.
|
8
|
+
* **Caching**: CDN is used so widely that potentially your users may not need to download jQuery at all.
|
9
|
+
* **Parallelism**: Browsers have a limitation on how many connections can be made to a single host. Using CDN for jQuery offloads a big one.
|
10
|
+
|
11
|
+
On top of that, if you're using asset pipeline, you may have noticed that the major chunks of the code in `application.js` is jQuery. Implications of externalizing jQuery from `application.js` are:
|
12
|
+
|
13
|
+
* Updating your js code won't evict the entire cache in browsers - your code changes more often than jQuery upgrades, right?
|
14
|
+
* `rake assets:precompile` takes less time and less peak memory usage.
|
15
|
+
|
16
|
+
This gem adds the following features:
|
17
|
+
|
18
|
+
* Supports multiple CDN. (Google, Microsoft and jquery.com)
|
19
|
+
* jQuery version is automatically detected via jquery-rails.
|
20
|
+
* Automatically fallback to jquery-rails' bundled jquery when:
|
21
|
+
* You're on a development environment, so that you can work offline.
|
22
|
+
* The CDN is down or unavailable.
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add this line to your application's Gemfile:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem 'jquery-rails-cdn'
|
30
|
+
```
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
This gem adds two methods `jquery_include_tag` and `jquery_url` to generate a script tag to the jQuery on a CDN of your preference.
|
35
|
+
|
36
|
+
If you're using asset pipeline with Rails 3.1+, first remove '//= require jquery' from `application.js`.
|
37
|
+
|
38
|
+
Then in layout:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
= jquery_include_tag :google
|
42
|
+
= javascript_include_tag 'application'
|
43
|
+
```
|
44
|
+
|
45
|
+
Note that valid CDN symbols are `:google`, `:google_ssl`, `:microsoft` and `:jquery`.
|
46
|
+
|
47
|
+
It will generate the following on production:
|
48
|
+
|
49
|
+
```html
|
50
|
+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
|
51
|
+
<script type="text/javascript">
|
52
|
+
//<![CDATA[
|
53
|
+
window.jQuery || document.write(unescape('%3Cscript src="/assets/jquery.js?body=1" type="text/javascript">%3C/script>'))
|
54
|
+
//]]>
|
55
|
+
</script>
|
56
|
+
```
|
57
|
+
|
58
|
+
on development:
|
59
|
+
|
60
|
+
```html
|
61
|
+
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
|
62
|
+
```
|
63
|
+
|
64
|
+
If you want to check the production URL, you can pass `:force => true` as an option.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
jquery_include_tag :google, :force => true
|
68
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/jquery-rails-cdn/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Kenn Ejima"]
|
6
|
+
gem.email = ["kenn.ejima@gmail.com"]
|
7
|
+
gem.description = %q{Add CDN support to jquery-rails}
|
8
|
+
gem.summary = %q{Add CDN support to jquery-rails}
|
9
|
+
gem.homepage = "https://github.com/kenn/jquery-rails-cdn"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "jquery-rails-cdn"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Jquery::Rails::Cdn::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "jquery-rails"
|
19
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'jquery-rails'
|
2
|
+
require 'jquery-rails-cdn/version'
|
3
|
+
|
4
|
+
module Jquery::Rails::Cdn
|
5
|
+
module ActionViewExtensions
|
6
|
+
OFFLINE = (Rails.env.development? or Rails.env.test?)
|
7
|
+
URL = {
|
8
|
+
:google => "http://ajax.googleapis.com/ajax/libs/jquery/#{Jquery::Rails::JQUERY_VERSION}/jquery.min.js",
|
9
|
+
:google_ssl => "https://ajax.googleapis.com/ajax/libs/jquery/#{Jquery::Rails::JQUERY_VERSION}/jquery.min.js",
|
10
|
+
:microsoft => "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-#{Jquery::Rails::JQUERY_VERSION}.min.js",
|
11
|
+
:jquery => "http://code.jquery.com/jquery-#{Jquery::Rails::JQUERY_VERSION}.min.js"
|
12
|
+
}
|
13
|
+
|
14
|
+
def jquery_url(name, options = {})
|
15
|
+
URL[name]
|
16
|
+
end
|
17
|
+
|
18
|
+
def jquery_include_tag(name, options = {})
|
19
|
+
return javascript_include_tag(:jquery) if OFFLINE and !options[:force]
|
20
|
+
|
21
|
+
[ javascript_include_tag(jquery_url(name, options)),
|
22
|
+
javascript_tag("window.jQuery || document.write(unescape('#{javascript_include_tag(:jquery).gsub('<','%3C')}'))")
|
23
|
+
].join("\n").html_safe
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Railtie < Rails::Railtie
|
28
|
+
initializer 'jquery_rails_cdn.action_view' do |app|
|
29
|
+
ActiveSupport.on_load(:action_view) do
|
30
|
+
include Jquery::Rails::Cdn::ActionViewExtensions
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jquery-rails-cdn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kenn Ejima
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: jquery-rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Add CDN support to jquery-rails
|
31
|
+
email:
|
32
|
+
- kenn.ejima@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- jquery-rails-cdn.gemspec
|
43
|
+
- lib/jquery-rails-cdn.rb
|
44
|
+
- lib/jquery-rails-cdn/version.rb
|
45
|
+
homepage: https://github.com/kenn/jquery-rails-cdn
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.19
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Add CDN support to jquery-rails
|
69
|
+
test_files: []
|
70
|
+
has_rdoc:
|