Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit b3021e61c276bb6a884e96db40047578e3845a4f @kenn committed Apr 21, 2012
Showing with 172 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +3 −0 Gemfile
  3. +22 −0 LICENSE
  4. +68 −0 README.md
  5. +2 −0 Rakefile
  6. +19 −0 jquery-rails-cdn.gemspec
  7. +34 −0 lib/jquery-rails-cdn.rb
  8. +7 −0 lib/jquery-rails-cdn/version.rb
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,3 @@
+source 'https://rubygems.org'
+
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Kenn Ejima
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
+# jquery-rails-cdn
+
+Add CDN support to [jquery-rails](https://github.com/rails/jquery-rails).
+
+Serving jQuery from a publicly available [CDN](http://en.wikipedia.org/wiki/Content_Delivery_Network) has clear benefits:
+
+* **Speed**: Users will be able to download jQuery from the closest physical location.
+* **Caching**: CDN is used so widely that potentially your users may not need to download jQuery at all.
+* **Parallelism**: Browsers have a limitation on how many connections can be made to a single host. Using CDN for jQuery offloads a big one.
+
+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:
+
+* Updating your js code won't evict the entire cache in browsers - your code changes more often than jQuery upgrades, right?
+* `rake assets:precompile` takes less time and less peak memory usage.
+
+This gem adds the following features:
+
+* Supports multiple CDN. (Google, Microsoft and jquery.com)
+* jQuery version is automatically detected via jquery-rails.
+* Automatically fallback to jquery-rails' bundled jquery when:
+ * You're on a development environment, so that you can work offline.
+ * The CDN is down or unavailable.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+```ruby
+gem 'jquery-rails-cdn'
+```
+
+## Usage
+
+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.
+
+If you're using asset pipeline with Rails 3.1+, first remove '//= require jquery' from `application.js`.
+
+Then in layout:
+
+```ruby
+= jquery_include_tag :google
+= javascript_include_tag 'application'
+```
+
+Note that valid CDN symbols are `:google`, `:google_ssl`, `:microsoft` and `:jquery`.
+
+It will generate the following on production:
+
+```html
+<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
+<script type="text/javascript">
+//<![CDATA[
+window.jQuery || document.write(unescape('%3Cscript src="/assets/jquery.js?body=1" type="text/javascript">%3C/script>'))
+//]]>
+</script>
+```
+
+on development:
+
+```html
+<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
+```
+
+If you want to check the production URL, you can pass `:force => true` as an option.
+
+```ruby
+jquery_include_tag :google, :force => true
+```
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/jquery-rails-cdn/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Kenn Ejima"]
+ gem.email = ["kenn.ejima@gmail.com"]
+ gem.description = %q{Add CDN support to jquery-rails}
+ gem.summary = %q{Add CDN support to jquery-rails}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "jquery-rails-cdn"
+ gem.require_paths = ["lib"]
+ gem.version = Jquery::Rails::Cdn::VERSION
+
+ gem.add_runtime_dependency "jquery-rails"
+end
@@ -0,0 +1,34 @@
+require 'jquery-rails'
+require 'jquery-rails-cdn/version'
+
+module Jquery::Rails::Cdn
+ module ActionViewExtensions
+ OFFLINE = (Rails.env.development? or Rails.env.test?)
+ URL = {
+ :google => "http://ajax.googleapis.com/ajax/libs/jquery/#{Jquery::Rails::JQUERY_VERSION}/jquery.min.js",
+ :google_ssl => "https://ajax.googleapis.com/ajax/libs/jquery/#{Jquery::Rails::JQUERY_VERSION}/jquery.min.js",
+ :microsoft => "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-#{Jquery::Rails::JQUERY_VERSION}.min.js",
+ :jquery => "http://code.jquery.com/jquery-#{Jquery::Rails::JQUERY_VERSION}.min.js"
+ }
+
+ def jquery_url(name, options = {})
+ URL[name]
+ end
+
+ def jquery_include_tag(name, options = {})
+ return javascript_include_tag(:jquery) if OFFLINE and !options[:force]
+
+ [ javascript_include_tag(jquery_url(name, options)),
+ javascript_tag("window.jQuery || document.write(unescape('#{javascript_include_tag(:jquery).gsub('<','%3C')}'))")
+ ].join("\n").html_safe
+ end
+ end
+
+ class Railtie < Rails::Railtie
+ initializer 'jquery_rails_cdn.action_view' do |app|
+ ActiveSupport.on_load(:action_view) do
+ include Jquery::Rails::Cdn::ActionViewExtensions
+ end
+ end
+ end
+end
@@ -0,0 +1,7 @@
+module Jquery
+ module Rails
+ module Cdn
+ VERSION = "0.0.1"
+ end
+ end
+end

0 comments on commit b3021e6

Please sign in to comment.