require 'jquery-rails' require 'jquery-rails-cdn/version' module Jquery::Rails::Cdn module ActionViewExtensions JQUERY_VERSION = Jquery::Rails::JQUERY_VERSION JQUERY_UI_VERSION = Jquery::Rails::JQUERY_UI_VERSION OFFLINE = (Rails.env.development? or Rails.env.test?) CDNS = { :jquery => { :google => "//ajax.googleapis.com/ajax/libs/jquery/#{JQUERY_VERSION}/jquery.min.js", :microsoft => "//ajax.aspnetcdn.com/ajax/jQuery/jquery-#{JQUERY_VERSION}.min.js", :jquery => "//code.jquery.com/jquery-#{JQUERY_VERSION}.min.js", :yandex => "//yandex.st/jquery/#{JQUERY_VERSION}/jquery.min.js" }, :jquery_ui => { :google => "//ajax.googleapis.com/ajax/libs/jqueryui/#{JQUERY_UI_VERSION}/jquery-ui.min.js", :microsoft => "//ajax.aspnetcdn.com/ajax/jquery.ui/#{JQUERY_UI_VERSION}/jquery-ui.min.js", :jquery => "//code.jquery.com/ui/#{JQUERY_UI_VERSION}/jquery-ui.min.js", :yandex => "//yandex.st/jquery-ui/#{JQUERY_UI_VERSION}/jquery-ui.min.js" }, } def jquery_url(name) return CDNS[:jquery][name] end def jquery_ui_url(name) return CDNS[:jquery_ui][name] end def jquery_include_tag(name, options = {}) options.reverse_merge! :local_copy => false jquery = 'jquery' jquery = jquery + '.min' if options.delete(:compressed) if OFFLINE and !options.delete(:force) options.delete(:local_copy) # not used in OFFLINE mode return javascript_include_tag(jquery, options) else j = [ javascript_include_tag(jquery_url(name), options) ] if options.delete(:local_copy) j << javascript_tag("window.jQuery || document.write(unescape('#{javascript_include_tag(jquery, options).gsub('<','%3C')}'))") end j.join("\n").html_safe end end def jquery_ui_include_tag(name, options = {}) options.reverse_merge! :local_copy => false jqueryui = 'jquery-ui' jqueryui = jqueryui + '.min' if options.delete(:compressed) if OFFLINE and !options.delete(:force) options.delete(:local_copy) # not used in OFFLINE mode return javascript_include_tag(jqueryui, options) else j = [ javascript_include_tag(jquery_ui_url(name), options) ] if options.delete(:local_copy) j << javascript_tag("window.jQuery.ui || document.write(unescape('#{javascript_include_tag(jqueryui, options).gsub('<','%3C')}'))") end j.join("\n").html_safe end 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