├── README.md └── jquery.rb /README.md: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | Rails template to setup a new Rails 3 app to use the new jQuery drivers instead of the default prototype ones. 4 | 5 | How to Use - New Project 6 | ======================== 7 | `rails new -m ` 8 | 9 | So, for example, if you make download this template in `~/Sites`:
10 | `rails new -m ~/Sites/jquery.rb` 11 | 12 | Otherwise, you can run it directly from github like so:
13 | `rails new -m https://github.com/lleger/Rails-3-jQuery/raw/master/jquery.rb` 14 | 15 | This template also makes a new javascript expansion, :cdn, for use in the javascript_include_tag that pulls jquery.min.js from Google's cache. For example:
16 | `<% javascript_include_tag :cdn %>` 17 | 18 | How to Use - Existing Project 19 | ============================= 20 | If you're converting a Rails 3 app to jQuery, you can use 21 | `rake rails:template` to execute the template inside your existing app. 22 | 23 | `rake rails:template LOCATION=https://github.com/lleger/Rails-3-jQuery/raw/master/jquery.rb` 24 | 25 | How to Update 26 | ============= 27 | The template creates a new rakefile called `jquery.rb` in `lib/tasks` that includes a rake tast `jquery:update` to update the jQuery js and Rails drivers js files. To use, simply run the following command: 28 | 29 | `rake jquery:update` 30 | 31 | The rake task will automatically download both js files and overwrite those in `public/javascripts`. 32 | 33 | Process 34 | ======= 35 | This template works via this process: 36 | 37 | 1. Remove old prototype js 38 | 2. Download latest jQuery min and place into javascripts folder 39 | 3. Download latest jQuery drivers and place into javascripts folder 40 | 4. Create requested javascript expansions 41 | 42 | License 43 | ======= 44 | You are free to make any and all modifications to the template and use it in whatever appropriate context you desire, as long as you keep the attributions in place. 45 | 46 | Pull Requests 47 | ============= 48 | I'm sure there are a lot of ways this can be done better, so if you make changes please send me a pull request. 49 | -------------------------------------------------------------------------------- /jquery.rb: -------------------------------------------------------------------------------- 1 | # This template installs the new jQuery drivers, removes 2 | # the old prototype drivers, and adds a line to the config 3 | # which overrides the :defaults expansion and 4 | # provides a :cdn expansion 5 | # Written by: Logan Leger, logan@loganleger.com 6 | # http://github.com/lleger/Rails-3-jQuery 7 | 8 | # Delete old prototype drivers 9 | # Do this first so that you don't delete the new jQuery rails one below 10 | inside('public/javascripts') do 11 | FileUtils.rm_rf %w(controls.js dragdrop.js effects.js prototype.js rails.js) 12 | end 13 | 14 | # Download latest jQuery.min 15 | get "http://code.jquery.com/jquery-latest.min.js", "public/javascripts/jquery.js" 16 | 17 | # Download latest jQuery drivers 18 | get "https://github.com/rails/jquery-ujs/raw/master/src/rails.js", "public/javascripts/rails.js" 19 | 20 | # Remove jQuery Comments in application.rb 21 | gsub_file 'config/application.rb', /#\s*(JavaScript files you want as :defaults (application.js is always included).)/, '\1' 22 | gsub_file 'config/application.rb', /#\s*(config.action_view.javascript_expansions[:defaults] = %w(jquery rails))/, '\1' 23 | 24 | # Add expansions to application.rb 25 | application do 26 | " # Added by the Rails 3 jQuery Template 27 | # http://github.com/lleger/Rails-3-jQuery, written by Logan Leger 28 | config.action_view.javascript_expansions[:defaults] = %w(jquery rails) 29 | config.action_view.javascript_expansions[:cdn] = %w(https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js rails)\n" 30 | end 31 | 32 | # Add a jquery.rake file to lib/tasks 33 | # and a jquery:update task to update 34 | # jquery and rails drivers 35 | rakefile("jquery.rake") do 36 | <<-TASK 37 | # Created by the Rails 3 jQuery Template 38 | # http://github.com/lleger/Rails-3-jQuery, written by Logan Leger 39 | require 'net/https' 40 | require 'uri' 41 | 42 | namespace :jquery do 43 | desc "Update jQuery and Rails jQuery drivers" 44 | task :update do 45 | http = Net::HTTP.new("ajax.googleapis.com",443) 46 | http.use_ssl = true 47 | http.start do |http| 48 | http.use_ssl = true 49 | resp = http.get("/ajax/libs/jquery/1/jquery.min.js") 50 | open("public/javascripts/jquery.js", "wb") do |file| 51 | file.write(resp.body) 52 | end 53 | end 54 | 55 | http = Net::HTTP.new("github.com", 443) 56 | http.use_ssl = true 57 | http.start do |http| 58 | http.use_ssl = true 59 | resp = http.get("/rails/jquery-ujs/raw/master/src/rails.js") 60 | open("public/javascripts/rails.js", "wb") do |file| 61 | file.write(resp.body) 62 | end 63 | end 64 | 65 | puts "jQuery and Rails jQuery drivers were updated!" 66 | end 67 | end 68 | TASK 69 | end 70 | 71 | --------------------------------------------------------------------------------