From e1607baff4ac71316da651bc7fa743e1ac6d0d5d Mon Sep 17 00:00:00 2001 From: Steven Hancock Date: Thu, 22 Mar 2012 23:38:09 -0700 Subject: [PATCH] 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. --- app/helpers/application_helper.rb | 15 ++++++++++++ app/views/layouts/application.html.haml | 2 +- config/application.yml.example | 3 +++ spec/helpers/application_helper_spec.rb | 31 +++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c61fafea9..e611a2cbd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index a3a0c4c7a..01fa8f767 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -29,7 +29,7 @@ = javascript_include_tag :ie - = javascript_include_tag 'jquery', 'jquery_ujs' + = jquery_include_tag - unless @landing_page = javascript_include_tag :main, :templates diff --git a/config/application.yml.example b/config/application.yml.example index 1de63b89a..164768668 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -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 # diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 271fd662b..7a34f9b89 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -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