jquery_include_tag helper

Implement the jquery_include_tag helper, which is used in several
views but was not implemented in the code. This helper attempts to
load jQuery from the Google CDN (matching the version of the vendored
jquery.js in jquery-rails), falling back to the vendored copy if the
google CDN fails. The helper also loads jquery_ujs.js from jquery-rails.

A jquery_cdn setting is added to application.yml to enable or disable
serving jQuery from the Google CDN (for those podmins who would rather
not use Google's services).

Update app/views/application.html.haml to use the jquery_include_tag
helper since it was the only view that was including the vendored
copy of jquery directly with jquery_include_tag.
This commit is contained in:
Steven Hancock 2012-03-22 23:38:09 -07:00 committed by Maxwell Salzberg
parent 1aa0b15c8c
commit e1607baff4
4 changed files with 50 additions and 1 deletions

View file

@ -35,4 +35,19 @@ module ApplicationHelper
def diaspora_id_host
User.diaspora_id_host
end
# Require jQuery from CDN if possible, falling back to vendored copy, and require
# vendored jquery_ujs
def jquery_include_tag
buf = []
if AppConfig[:jquery_cdn]
version = Jquery::Rails::JQUERY_VERSION
buf << [ javascript_include_tag("//ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js") ]
buf << [ javascript_tag("!window.jQuery && document.write(unescape('#{j javascript_include_tag("jquery")}'));") ]
else
buf << [ javascript_include_tag('jquery') ]
end
buf << [ javascript_include_tag('jquery_ujs') ]
buf.join("\n").html_safe
end
end

View file

@ -29,7 +29,7 @@
= javascript_include_tag :ie
<![endif]-->
= javascript_include_tag 'jquery', 'jquery_ujs'
= jquery_include_tag
- unless @landing_page
= javascript_include_tag :main, :templates

View file

@ -39,6 +39,9 @@ defaults: &defaults
# let your reverse proxy/webserver do it.
serve_static_assets: false
# Serve jQuery from Google's CDN
jquery_cdn: true
#
# Settings
#

View file

@ -48,4 +48,35 @@ describe ApplicationHelper do
all_services_connected?.should be_false
end
end
describe "#jquery_include_tag" do
describe "with google cdn" do
before do
AppConfig[:jquery_cdn] = true
end
it 'inclues jquery.js from google cdn' do
jquery_include_tag.should match(/googleapis\.com/)
end
it 'falls back to asset pipeline on cdn failure' do
jquery_include_tag.should match(/document\.write/)
end
end
describe "without google cdn" do
before do
AppConfig[:jquery_cdn] = false
end
it 'includes jquery.js from asset pipeline' do
jquery_include_tag.should match(/jquery\.js/)
jquery_include_tag.should_not match(/googleapis\.com/)
end
end
it 'inclues jquery_ujs.js' do
jquery_include_tag.should match(/jquery_ujs\.js/)
end
end
end