1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

jQuery is the new default

This commit is contained in:
Xavier Noria 2011-03-24 23:43:37 +01:00
parent 805126b20a
commit 096fa1b60f
7 changed files with 104 additions and 94 deletions

View file

@ -94,84 +94,98 @@ module ActionView
end
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route
# Returns an HTML script tag for each of the +sources+ provided. You
# can pass in the filename (.js extension is optional) of JavaScript files
# that exist in your <tt>public/javascripts</tt> directory for inclusion into the
# current page or you can pass the full path relative to your document
# root. To include the Prototype and Scriptaculous JavaScript libraries in
# your application, pass <tt>:defaults</tt> as the source. When using
# <tt>:defaults</tt>, if an <tt>application.js</tt> file exists in
# <tt>public/javascripts</tt> it will be included as well. You can modify the
# HTML attributes of the script tag by passing a hash as the last argument.
# Returns an HTML script tag for each of the +sources+ provided.
#
# Sources may be paths to JavaScript files. Relative paths are assumed to be relative
# to <tt>public/javascripts</tt>, full paths are assumed to be relative to the document
# root. Relative paths are idiomatic, use absolute paths only when needed.
#
# When passing paths, the ".js" extension is optional.
#
# To include the default JavaScript expansion pass <tt>:defaults</tt> as source.
# By default, <tt>:defaults</tt> loads jQuery. If the application was generated
# with "-j prototype" the libraries Prototype and Scriptaculous are loaded instead.
# In any case, the defaults can be overridden in <tt>config/application.rb</tt>:
#
# config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js)
#
# When using <tt>:defaults</tt>, if an <tt>application.js</tt> file exists in
# <tt>public/javascripts</tt> it will be included as well at the end.
#
# You can modify the HTML attributes of the script tag by passing a hash as the
# last argument.
#
# ==== Examples
# javascript_include_tag "xmlhr" # =>
# <script type="text/javascript" src="/javascripts/xmlhr.js?1284139606"></script>
# javascript_include_tag "xmlhr"
# # => <script type="text/javascript" src="/javascripts/xmlhr.js?1284139606"></script>
#
# javascript_include_tag "xmlhr.js" # =>
# <script type="text/javascript" src="/javascripts/xmlhr.js?1284139606"></script>
# javascript_include_tag "xmlhr.js"
# # => <script type="text/javascript" src="/javascripts/xmlhr.js?1284139606"></script>
#
# javascript_include_tag "common.javascript", "/elsewhere/cools" # =>
# <script type="text/javascript" src="/javascripts/common.javascript?1284139606"></script>
# <script type="text/javascript" src="/elsewhere/cools.js?1423139606"></script>
# javascript_include_tag "common.javascript", "/elsewhere/cools"
# # => <script type="text/javascript" src="/javascripts/common.javascript?1284139606"></script>
# # <script type="text/javascript" src="/elsewhere/cools.js?1423139606"></script>
#
# javascript_include_tag "http://www.railsapplication.com/xmlhr" # =>
# <script type="text/javascript" src="http://www.railsapplication.com/xmlhr.js?1284139606"></script>
# javascript_include_tag "http://www.railsapplication.com/xmlhr"
# # => <script type="text/javascript" src="http://www.railsapplication.com/xmlhr.js?1284139606"></script>
#
# javascript_include_tag "http://www.railsapplication.com/xmlhr.js" # =>
# <script type="text/javascript" src="http://www.railsapplication.com/xmlhr.js?1284139606"></script>
# javascript_include_tag "http://www.railsapplication.com/xmlhr.js"
# # => <script type="text/javascript" src="http://www.railsapplication.com/xmlhr.js?1284139606"></script>
#
# javascript_include_tag :defaults # =>
# <script type="text/javascript" src="/javascripts/prototype.js?1284139606"></script>
# <script type="text/javascript" src="/javascripts/effects.js?1284139606"></script>
# ...
# <script type="text/javascript" src="/javascripts/application.js?1284139606"></script>
# javascript_include_tag :defaults
# # => <script type="text/javascript" src="/javascripts/jquery.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/rails.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/application.js?1284139606"></script>
#
# * = The application.js file is only referenced if it exists
#
# You can also include all javascripts in the +javascripts+ directory using <tt>:all</tt> as the source:
# You can also include all JavaScripts in the +javascripts+ directory using <tt>:all</tt> as the source:
#
# javascript_include_tag :all # =>
# <script type="text/javascript" src="/javascripts/prototype.js?1284139606"></script>
# <script type="text/javascript" src="/javascripts/effects.js?1284139606"></script>
# ...
# <script type="text/javascript" src="/javascripts/application.js?1284139606"></script>
# <script type="text/javascript" src="/javascripts/shop.js?1284139606"></script>
# <script type="text/javascript" src="/javascripts/checkout.js?1284139606"></script>
# javascript_include_tag :all
# # => <script type="text/javascript" src="/javascripts/jquery.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/rails.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/application.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/shop.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/checkout.js?1284139606"></script>
#
# Note that the default javascript files will be included first. So Prototype and Scriptaculous are available to
# all subsequently included files.
# Note that your defaults of choice will be included first, so they will be available to all subsequently
# included files.
#
# If you want Rails to search in all the subdirectories under javascripts, you should explicitly set <tt>:recursive</tt>:
# If you want Rails to search in all the subdirectories under <tt>public/javascripts</tt>, you should
# explicitly set <tt>:recursive</tt>:
#
# javascript_include_tag :all, :recursive => true
#
# == Caching multiple javascripts into one
# == Caching multiple JavaScripts into one
#
# You can also cache multiple javascripts into one file, which requires less HTTP connections to download and can better be
# compressed by gzip (leading to faster transfers). Caching will only happen if config.perform_caching
# is set to <tt>true</tt> (which is the case by default for the Rails production environment, but not for the development
# environment).
# You can also cache multiple JavaScripts into one file, which requires less HTTP connections to download
# and can better be compressed by gzip (leading to faster transfers). Caching will only happen if
# <tt>config.perform_caching</tt> is set to true (which is the case by default for the Rails
# production environment, but not for the development environment).
#
# ==== Examples
# javascript_include_tag :all, :cache => true # when config.perform_caching is false =>
# <script type="text/javascript" src="/javascripts/prototype.js?1284139606"></script>
# <script type="text/javascript" src="/javascripts/effects.js?1284139606"></script>
# ...
# <script type="text/javascript" src="/javascripts/application.js?1284139606"></script>
# <script type="text/javascript" src="/javascripts/shop.js?1284139606"></script>
# <script type="text/javascript" src="/javascripts/checkout.js?1284139606"></script>
#
# javascript_include_tag :all, :cache => true # when config.perform_caching is true =>
# <script type="text/javascript" src="/javascripts/all.js?1344139789"></script>
# # assuming config.perform_caching is false
# javascript_include_tag :all, :cache => true
# # => <script type="text/javascript" src="/javascripts/jquery.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/rails.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/application.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/shop.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/checkout.js?1284139606"></script>
#
# javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when config.perform_caching is false =>
# <script type="text/javascript" src="/javascripts/prototype.js?1284139606"></script>
# <script type="text/javascript" src="/javascripts/cart.js?1289139157"></script>
# <script type="text/javascript" src="/javascripts/checkout.js?1299139816"></script>
# # assuming config.perform_caching is true
# javascript_include_tag :all, :cache => true
# # => <script type="text/javascript" src="/javascripts/all.js?1344139789"></script>
#
# javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when config.perform_caching is true =>
# <script type="text/javascript" src="/javascripts/shop.js?1299139816"></script>
# # assuming config.perform_caching is false
# javascript_include_tag "jquery", "cart", "checkout", :cache => "shop"
# # => <script type="text/javascript" src="/javascripts/jquery.js?1284139606"></script>
# # <script type="text/javascript" src="/javascripts/cart.js?1289139157"></script>
# # <script type="text/javascript" src="/javascripts/checkout.js?1299139816"></script>
#
# # assuming config.perform_caching is true
# javascript_include_tag "jquery", "cart", "checkout", :cache => "shop"
# # => <script type="text/javascript" src="/javascripts/shop.js?1299139816"></script>
#
# The <tt>:recursive</tt> option is also available for caching:
#
@ -184,9 +198,7 @@ module ActionView
@javascript_include.include_tag(*sources)
end
end
end
end
end
end

View file

@ -6,7 +6,7 @@ module ActionView
class Railtie < Rails::Railtie
config.action_view = ActiveSupport::OrderedOptions.new
config.action_view.stylesheet_expansions = {}
config.action_view.javascript_expansions = { :defaults => ['prototype', 'effects', 'dragdrop', 'controls', 'rails'] }
config.action_view.javascript_expansions = { :defaults => %w(jquery rails) }
initializer "action_view.cache_asset_ids" do |app|
unless app.config.cache_classes

View file

@ -707,18 +707,28 @@ To include +http://example.com/main.js+:
<%= javascript_include_tag "http://example.com/main.js" %>
</erb>
The +defaults+ option loads the Prototype and Scriptaculous libraries:
The +:defaults+ option loads jQuery by default:
<erb>
<%= javascript_include_tag :defaults %>
</erb>
The +all+ option loads every JavaScript file in +public/javascripts+, starting with the Prototype and Scriptaculous libraries:
If the application was generated with "-j prototype" <tt>:defaults</tt> loads Prototype and Scriptaculous. And you can in any case override the expansion in <tt>config/application.rb</tt>:
<ruby>
config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js)
</ruby>
When using <tt>:defaults</tt>, if an <tt>application.js</tt> file exists in <tt>public/javascripts</tt> it will be included as well at then end.
The +:all+ option loads every JavaScript file in +public/javascripts+:
<erb>
<%= javascript_include_tag :all %>
</erb>
Note that your defaults of choice will be included first, so they will be available to all subsequently included files.
You can supply the +:recursive+ option to load files in subfolders of +public/javascripts+ as well:
<erb>

View file

@ -10,7 +10,7 @@ module Rails
module Generators
class AppBase < Base
DATABASES = %w( mysql oracle postgresql sqlite3 frontbase ibm_db )
JAVASCRIPTS = %w( prototype jquery )
JAVASCRIPTS = %w( jquery prototype )
attr_accessor :rails_template
add_shebang_option!
@ -36,11 +36,11 @@ module Rails
class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3",
:desc => "Preconfigure for selected database (options: #{DATABASES.join('/')})"
class_option :javascript, :type => :string, :aliases => "-j", :default => "prototype",
:desc => "Preconfigure for selected javascript library (options: #{JAVASCRIPTS.join('/')})"
class_option :javascript, :type => :string, :aliases => "-j", :default => "jquery",
:desc => "Preconfigure for selected JavaScript library (options: #{JAVASCRIPTS.join('/')})"
class_option :skip_javascript, :type => :boolean, :aliases => "-J", :default => false,
:desc => "Skip javascript files"
:desc => "Skip JavaScript files"
class_option :dev, :type => :boolean, :default => false,
:desc => "Setup the #{name} with Gemfile pointing to your Rails checkout"

View file

@ -42,10 +42,10 @@ module <%= app_const_base %>
# JavaScript files you want as :defaults (application.js is always included).
<% if options[:skip_javascript] -%>
config.action_view.javascript_expansions[:defaults] = %w()
<% elsif options[:javascript] == 'jquery' -%>
config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
<% elsif options[:javascript] == 'prototype' -%>
config.action_view.javascript_expansions[:defaults] = %w(prototype effects dragdrop controls rails)
<% else -%>
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
# config.action_view.javascript_expansions[:defaults] = %w(prototype effects dragdrop controls rails)
<% end -%>
<% if options[:skip_test_unit] -%>

View file

@ -146,15 +146,12 @@ class AppGeneratorTest < Rails::Generators::TestCase
assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
end
def test_prototype_and_test_unit_are_added_by_default
def test_jquery_and_test_unit_are_added_by_default
run_generator
assert_file "config/application.rb", /#\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(jquery rails\)/
assert_file "config/application.rb", /#\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(prototype effects dragdrop controls rails\)/
assert_file "public/javascripts/application.js"
assert_file "public/javascripts/prototype.js"
assert_file "public/javascripts/jquery.js"
assert_file "public/javascripts/rails.js"
assert_file "public/javascripts/controls.js"
assert_file "public/javascripts/dragdrop.js"
assert_file "public/javascripts/effects.js"
assert_file "test"
end
@ -162,24 +159,24 @@ class AppGeneratorTest < Rails::Generators::TestCase
run_generator [destination_root, "--skip-javascript"]
assert_file "config/application.rb", /^\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(\)/
assert_file "public/javascripts/application.js"
assert_no_file "public/javascripts/prototype.js"
assert_no_file "public/javascripts/jquery.js"
assert_no_file "public/javascripts/rails.js"
end
def test_config_prototype_javascript_library
run_generator [destination_root, "-j", "prototype"]
assert_file "config/application.rb", /#\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(jquery rails\)/
assert_file "config/application.rb", /^\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(prototype effects dragdrop controls rails\)/
assert_file "public/javascripts/application.js"
assert_file "public/javascripts/prototype.js"
assert_file "public/javascripts/controls.js"
assert_file "public/javascripts/dragdrop.js"
assert_file "public/javascripts/effects.js"
assert_file "public/javascripts/dragdrop.js"
assert_file "public/javascripts/controls.js"
assert_file "public/javascripts/rails.js", /prototype/
end
def test_config_jquery_javascript_library
run_generator [destination_root, "-j", "jquery"]
assert_file "config/application.rb", /^\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(jquery rails\)/
assert_file "config/application.rb", /#\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(prototype effects dragdrop controls rails\)/
assert_file "public/javascripts/application.js"
assert_file "public/javascripts/jquery.js"
assert_file "public/javascripts/rails.js", /jQuery/

View file

@ -95,41 +95,32 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
def test_skipping_javascripts_without_mountable_option
run_generator
assert_no_file "public/javascripts/prototype.js"
assert_no_file "public/javascripts/jquery.js"
assert_no_file "public/javascripts/rails.js"
assert_no_file "public/javascripts/controls.js"
assert_no_file "public/javascripts/dragdrop.js"
assert_no_file "public/javascripts/dragdrop.js"
assert_no_file "public/javascripts/application.js"
end
def test_javascripts_generation
run_generator [destination_root, "--mountable"]
assert_file "public/javascripts/jquery.js"
assert_file "public/javascripts/rails.js"
assert_file "public/javascripts/prototype.js"
assert_file "public/javascripts/controls.js"
assert_file "public/javascripts/dragdrop.js"
assert_file "public/javascripts/dragdrop.js"
assert_file "public/javascripts/application.js"
end
def test_skip_javascripts
run_generator [destination_root, "--skip-javascript", "--mountable"]
assert_no_file "public/javascripts/prototype.js"
assert_no_file "public/javascripts/jquery.js"
assert_no_file "public/javascripts/rails.js"
assert_no_file "public/javascripts/controls.js"
assert_no_file "public/javascripts/dragdrop.js"
assert_no_file "public/javascripts/dragdrop.js"
end
def test_ensure_that_javascript_option_is_passed_to_app_generator
run_generator [destination_root, "--javascript", "jquery"]
assert_file "test/dummy/public/javascripts/jquery.js"
run_generator [destination_root, "--javascript", "prototype"]
assert_file "test/dummy/public/javascripts/prototype.js"
end
def test_ensure_that_skip_javascript_option_is_passed_to_app_generator
run_generator [destination_root, "--skip_javascript"]
assert_no_file "test/dummy/public/javascripts/prototype.js"
assert_no_file "test/dummy/public/javascripts/jquery.js"
end
def test_template_from_dir_pwd