├── .gitignore ├── Gemfile ├── MIT-LICENSE.txt ├── README.md ├── Rakefile ├── jquery-cdn.gemspec ├── lib ├── jquery-cdn.rb └── jquery-cdn │ ├── helpers.rb │ ├── railties.rb │ └── version.rb └── vendor └── assets └── javascripts └── jquery.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *~ 3 | 4 | pkg/ 5 | build/ 6 | 7 | .bundle 8 | Gemfile.lock 9 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | gem 'rake' 6 | gem 'httpclient' 7 | -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2013 jQuery Foundation and other contributors 2 | http://jquery.com/ 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining 5 | a copy of this software and associated documentation files (the 6 | "Software"), to deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, merge, publish, 8 | distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to 10 | the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED: Best way to load latest jQuery in Ruby 2 | 3 | **This project is depreacted. Use Node.js build tools instead of Sprockets.** 4 | 5 | Load jQuery from CDN in production and use local copy in development. 6 | jQuery-CDN supports Ruby on Rails, Sinatra and other non-Rails environments 7 | with Sprockets. 8 | 9 | Another gem [jquery-rails](https://github.com/rails/jquery-rails) contains also 10 | UJS adapter for jQuery. So it need to test any jQuery updates and will release 11 | new jQuery version after few month (for example, there is still no jQuery 2 12 | in `jquery-ujs`). If you don’t need UJS, this gem will be better for you. 13 | 14 | jQuery-CDN now has 2 branches: with jQuery 2.x and 1.x. 15 | 16 | ## Features 17 | 18 | ### Load from CDN 19 | 20 | Public CDN is a best way to serve jQuery: 21 | 22 | * **Speed**: users will be able to download jQuery from the closest physical 23 | location. 24 | * **Caching**: CDN is used so widely that potentially your users may not need 25 | to download jQuery at all. 26 | * **Parallelism**: browsers have a limitation on how many connections can 27 | be made to a single host. Using CDN for jQuery offloads a big one. 28 | 29 | In development gem will use local copy of jQuery, so you can develop app 30 | in airplane without Internet. In production gem will use CDN, 31 | but if it will down, gem will automatically fallback to bundled jQuery. 32 | 33 | ### Latest version of jQuery 34 | 35 | Instead of `jquery-rails` this gem always contain latest version of jQuery, 36 | because it doesn’t need to test compatibility with UJS adapter. 37 | 38 | For example, `jquery-rails` 39 | [doesn’t support](https://github.com/rails/jquery-rails/issues/124) 40 | jQuery 2 even after 4 months. 41 | 42 | ### Gem version is same that jQuery 43 | 44 | Instead of `jquery-rails`, this gem versions tell exactly what jQuery is inside. 45 | `Gemfile` maintaining will be much easy: 46 | 47 | ```ruby 48 | gem 'jquery-cdn', '1.10.2' # Use jQuery 1.10.2 49 | ``` 50 | 51 | ### Sinatra and plain Ruby support 52 | 53 | You can use jQuery-CDN with Ruby on Rails, Sinatra or any other Ruby environment 54 | with Sprockets. 55 | 56 | ## How To 57 | 58 | ### Ruby on Rails 59 | 60 | Add `jquery-cdn` gem to your `Gemfile`: 61 | 62 | ```ruby 63 | gem 'jquery-cdn' 64 | ``` 65 | 66 | If you support IE 6, 7 or 8, lock jQuery in 1.x versions: 67 | 68 | ```ruby 69 | gem 'jquery-cdn', '~> 1.0' 70 | ``` 71 | 72 | Call `include_jquery` helper in layout: 73 | 74 | ```haml 75 | !!! 5 76 | %html 77 | %head 78 | %title My site 79 | = include_jquery 80 | = javascript_include_tag('application') 81 | ``` 82 | 83 | ### Ruby 84 | 85 | If you use Sinatra or other non-Rails frameworks with Sprockets, 86 | just connect your Sprockets environment to jQuery-CDN: 87 | 88 | ```ruby 89 | require 'jquery-cdn' 90 | 91 | assets = Sprockets::Environment.new do |env| 92 | # Your assets settings 93 | end 94 | 95 | JqueryCdn.install(assets) 96 | ``` 97 | 98 | Set local jQuery URL (by default, `/assets/jquery.js`): 99 | 100 | ```ruby 101 | JqueryCdn.local_url = proc { '/jquery.js' } 102 | ``` 103 | 104 | Include `JqueryCdn::Helpers` module to your app: 105 | 106 | ```ruby 107 | class YourApp < Sinatra::Base 108 | helpers { include JqueryCdn::Helpers } 109 | end 110 | ``` 111 | 112 | And use `include_jquery` helper with `env` option: 113 | 114 | ```haml 115 | !!! 5 116 | %html 117 | %head 118 | = include_jquery(env: app.environment) 119 | ``` 120 | 121 | ## Options 122 | 123 | Helper `include_jquery` has 2 options: 124 | 125 | * `env`: CDN will be used only in `:production` environment. Rails helper can 126 | detect it automatically. By default, `:production`. 127 | * `cdn`: CDN to use. By default, `:google`. 128 | 129 | Other options will be used as ` 172 | 173 | ``` 174 | 175 | This HTML checks, is jQuery normally loaded from Google. On any problems it will 176 | load local copy of jQuery. 177 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | 3 | require 'bundler/gem_tasks' 4 | 5 | desc 'Update assets from jQuery repo' 6 | task :update do 7 | require 'json' 8 | require 'httpclient' 9 | 10 | def asset(file) 11 | Pathname(__FILE__).dirname.join('vendor/assets/javascripts').join(file) 12 | end 13 | 14 | def github_tags(repo) 15 | http = HTTPClient.new 16 | body = http.get("https://api.github.com/repos/#{repo}/tags").body 17 | response = JSON.parse(body) 18 | response.reject { |i| i['name'] =~ /rc|beta|alpha/ }. 19 | map { |i| Gem::Version.new(i['name']) }. 20 | sort 21 | end 22 | 23 | def fetch(tag) 24 | url = "http://ajax.googleapis.com/ajax/libs/jquery/#{tag}/jquery.js" 25 | path = asset("jquery.js") 26 | 27 | path.dirname.rmtree if path.dirname.exist? 28 | path.dirname.mkpath 29 | 30 | path.open('w') do |io| 31 | http = HTTPClient.new 32 | http.transparent_gzip_decompression = true 33 | io << http.get(url).body 34 | end 35 | end 36 | 37 | def update_version(tag) 38 | version_file = Pathname(__FILE__).dirname.join('lib/jquery-cdn/version.rb') 39 | version_file.open('w') do |io| 40 | io << "module JqueryCdn\n VERSION = \"#{tag}\"\nend\n" 41 | end 42 | end 43 | 44 | puts "Fetching tags" 45 | tag = github_tags('jquery/jquery').last 46 | 47 | require './lib/jquery-cdn/version' 48 | if tag.to_s == JqueryCdn::VERSION 49 | puts "No releases, since #{ JqueryCdn::VERSION }" 50 | else 51 | puts "Load jQuery #{tag}" 52 | fetch(tag) 53 | 54 | puts "Update gem version" 55 | update_version(tag) 56 | 57 | puts "Done" 58 | end 59 | end 60 | 61 | desc 'Delete all generated files' 62 | task :clobber do 63 | rm_r 'pkg' rescue nil 64 | end 65 | -------------------------------------------------------------------------------- /jquery-cdn.gemspec: -------------------------------------------------------------------------------- 1 | require File.expand_path('../lib/jquery-cdn/version', __FILE__) 2 | 3 | Gem::Specification.new do |s| 4 | s.platform = Gem::Platform::RUBY 5 | s.name = 'jquery-cdn' 6 | s.version = JqueryCdn::VERSION 7 | s.summary = 'Best way to use latest jQuery in Ruby app' 8 | 9 | s.files = `git ls-files`.split("\n") 10 | s.require_path = 'lib' 11 | 12 | s.author = 'Andrey Sitnik' 13 | s.email = 'andrey@sitnik.ru' 14 | s.homepage = 'https://github.com/ai/jquery-cdn' 15 | s.license = 'MIT' 16 | 17 | s.add_dependency 'sprockets', '>= 2' 18 | end 19 | -------------------------------------------------------------------------------- /lib/jquery-cdn.rb: -------------------------------------------------------------------------------- 1 | require 'pathname' 2 | require 'cgi' 3 | 4 | lib = Pathname(__FILE__).dirname.join('jquery-cdn') 5 | require lib.join('version').to_s 6 | require lib.join('helpers').to_s 7 | 8 | module JqueryCdn 9 | 10 | version = JqueryCdn::VERSION.split('.')[0..2].join('.') 11 | URL = { 12 | google: "//ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js", 13 | microsoft: "//ajax.aspnetcdn.com/ajax/jQuery/jquery-#{version}.min.js", 14 | jquery: "http://code.jquery.com/jquery-#{version}.min.js", 15 | yandex: "//yandex.st/jquery/#{version}/jquery.min.js", 16 | cloudflare: "//cdnjs.cloudflare.com/ajax/libs/jquery/#{version}/jquery.min.js" 17 | } 18 | 19 | # Add assets paths to standalone Sprockets environment. 20 | def self.install(sprockets) 21 | root = Pathname(__FILE__).dirname.join('..').expand_path 22 | sprockets.append_path(root.join('vendor/assets/javascripts')) 23 | end 24 | 25 | # Return URL to local or CDN jQuery, depend on `env`. 26 | def self.url(env, cdn) 27 | if env == :production 28 | raise ArgumentError, "Unknown CDN #{cdn}" unless URL.has_key? cdn 29 | URL[cdn] 30 | else 31 | @local_url.call 32 | end 33 | end 34 | 35 | # Set proc to generate locale jQuery URL 36 | def self.local_url=(proc) 37 | @local_url = proc 38 | end 39 | 40 | # Return " 55 | end 56 | 57 | # Return