|
| 1 | +require 'rails' |
| 2 | + |
| 3 | +module Jquery |
| 4 | + module Generators |
| 5 | + class InstallGenerator < ::Rails::Generators::Base |
| 6 | + desc "This generator downloads and installs jQuery, jQuery-ujs HEAD, and (optionally) the newest jQuery UI" |
| 7 | + class_option :ui, :type => :boolean, :default => false, :desc => "Include jQueryUI" |
| 8 | + class_option :version, :type => :string, :default => "1.6", :desc => "Which version of jQuery to fetch" |
| 9 | + @@default_version = "1.6" |
| 10 | + |
| 11 | + def remove_prototype |
| 12 | + %w(controls.js dragdrop.js effects.js prototype.js).each do |js| |
| 13 | + remove_file "public/javascripts/#{js}" |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + def download_jquery |
| 18 | + say_status("fetching", "jQuery (#{options.version})", :green) |
| 19 | + get_jquery(options.version) |
| 20 | + rescue OpenURI::HTTPError |
| 21 | + say_status("warning", "could not find jQuery (#{options.version})", :yellow) |
| 22 | + say_status("fetching", "jQuery (#{@@default_version})", :green) |
| 23 | + get_jquery(@@default_version) |
| 24 | + end |
| 25 | + |
| 26 | + def download_jquery_ui |
| 27 | + if options.ui? |
| 28 | + say_status("fetching", "jQuery UI (latest 1.x release)", :green) |
| 29 | + get "http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.js", "public/javascripts/jquery-ui.js" |
| 30 | + get "http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js", "public/javascripts/jquery-ui.min.js" |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + def download_ujs_driver |
| 35 | + say_status("fetching", "jQuery UJS adapter (github HEAD)", :green) |
| 36 | + get "https://github.com/rails/jquery-ujs/raw/master/src/rails.js", "public/javascripts/rails.js" |
| 37 | + end |
| 38 | + |
| 39 | + private |
| 40 | + |
| 41 | + def get_jquery(version) |
| 42 | + get "http://ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.js", "public/javascripts/jquery.js" |
| 43 | + get "http://ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js", "public/javascripts/jquery.min.js" |
| 44 | + end |
| 45 | + |
| 46 | + end |
| 47 | + end |
| 48 | +end if ::Rails.version < "3.1" |
0 commit comments