From 8e2a05b33da1cd27d18a82168b49ba642122fbdb Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 6 Aug 2012 16:22:23 -0400 Subject: Do not include application.js if it doesn't exists Rails were including 'application.js' to the pack when using `javascript_include_tag :all` even there's no application.js in the public directory. --- actionpack/CHANGELOG.md | 2 ++ .../helpers/asset_tag_helpers/javascript_tag_helpers.rb | 11 ++++++----- actionpack/test/template/asset_tag_helper_test.rb | 9 +++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index c43ec62555..3ec41a1060 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 3.2.8 ## +* `javascript_include_tag :all` will now not include `application.js` if the file does not exists. *Prem Sichanugrist* + * Reverted the deprecation of `:confirm`. *Rafael Mendonça França* * Reverted the deprecation of `:disable_with`. *Rafael Mendonça França* diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb index d9f1f88ade..7449f93671 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb @@ -27,7 +27,8 @@ module ActionView def expand_sources(sources, recursive = false) if sources.include?(:all) - all_asset_files = (collect_asset_files(custom_dir, ('**' if recursive), "*.#{extension}") - ['application']) << 'application' + all_asset_files = (collect_asset_files(custom_dir, ('**' if recursive), "*.#{extension}") - ['application']) + add_application_js(all_asset_files, sources) ((determine_source(:defaults, expansions).dup & all_asset_files) + all_asset_files).uniq else expanded_sources = sources.inject([]) do |list, source| @@ -40,7 +41,7 @@ module ActionView end def add_application_js(expanded_sources, sources) - if sources.include?(:defaults) && File.exist?(File.join(custom_dir, "application.#{extension}")) + if (sources.include?(:defaults) || sources.include?(:all)) && File.exist?(File.join(custom_dir, "application.#{extension}")) expanded_sources.delete('application') expanded_sources << "application" end @@ -101,8 +102,8 @@ module ActionView # # config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js) # - # When using :defaults, if an application.js file exists in - # public/javascripts it will be included as well at the end. + # When using :defaults or :all, if an application.js file exists + # in public/javascripts 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. @@ -129,7 +130,7 @@ module ActionView # # # # # - # * = The application.js file is only referenced if it exists + # Note: The application.js file is only referenced if it exists # # You can also include all JavaScripts in the +javascripts+ directory using :all as the source: # diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 6a0fdf0590..b1a01b53b1 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -338,6 +338,15 @@ class AssetTagHelperTest < ActionView::TestCase assert_raise(ArgumentError) { javascript_include_tag(:defaults) } end + def test_all_javascript_expansion_not_include_application_js_if_not_exists + FileUtils.mv(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'application.js'), + File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'application.bak')) + assert_no_match(/application\.js/, javascript_include_tag(:all)) + ensure + FileUtils.mv(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'application.bak'), + File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'application.js')) + end + def test_stylesheet_path ENV["RAILS_ASSET_ID"] = "" StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } -- cgit v1.2.3