diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index a728fde..50e570a 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -310,6 +310,26 @@ module ActionView
#
# javascript_include_tag :all, :recursive => true
#
+ # == Duplicate sources
+ #
+ # By default, any duplicate source (after expansion) is removed, so you only get one request per resource.
+ # But if you want you can override this behavior by specifying :unique => false.
+ #
+ # javascript_include_tag :all, :unique => false
+ #
+ # ==== Examples
+ # # :robber expands to 'bank' and 'robber', :banker expands to 'bank' and 'banker'
+ # javascript_include_tag :robber, :banker # =>
+ #
+ #
+ #
+ #
+ # javascript_include_tag :robber, :banker, :unique => false # =>
+ #
+ #
+ #
+ #
+ #
# == 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
@@ -345,6 +365,8 @@ module ActionView
concat = options.delete("concat")
cache = concat || options.delete("cache")
recursive = options.delete("recursive")
+ unique = options.delete("unique")
+ unique = true if unique.nil?
if concat || (config.perform_caching && cache)
joined_javascript_name = (cache == true ? "all" : cache) + ".js"
@@ -355,7 +377,7 @@ module ActionView
end
javascript_src_tag(joined_javascript_name, options)
else
- sources = expand_javascript_sources(sources, recursive)
+ sources = expand_javascript_sources(sources, recursive, unique)
ensure_javascript_sources!(sources) if cache
sources.collect { |source| javascript_src_tag(source, options) }.join("\n").html_safe
end
@@ -831,7 +853,7 @@ module ActionView
expand_stylesheet_sources(*args).collect { |source| compute_public_path(source, 'stylesheets', 'css', false) }
end
- def expand_javascript_sources(sources, recursive = false)
+ def expand_javascript_sources(sources, recursive = false, unique = true)
if sources.include?(:all)
all_javascript_files = (collect_asset_files(config.javascripts_dir, ('**' if recursive), '*.js') - ['application']) << 'application'
((determine_source(:defaults, @@javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq
@@ -839,6 +861,7 @@ module ActionView
expanded_sources = sources.collect do |source|
determine_source(source, @@javascript_expansions)
end.flatten
+ expanded_sources.uniq! if unique
expanded_sources << "application" if sources.include?(:defaults) && File.exist?(File.join(config.javascripts_dir, "application.js"))
expanded_sources
end
diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb
index 3abcdfb..194450d 100644
--- a/actionpack/test/template/asset_tag_helper_test.rb
+++ b/actionpack/test/template/asset_tag_helper_test.rb
@@ -265,7 +265,19 @@ class AssetTagHelperTest < ActionView::TestCase
def test_custom_javascript_expansions_and_defaults_puts_application_js_at_the_end
ENV["RAILS_ASSET_ID"] = ""
ActionView::Helpers::AssetTagHelper::register_javascript_expansion :robbery => ["bank", "robber"]
- assert_dom_equal %(\n\n\n\n\n\n\n\n\n), javascript_include_tag('controls',:defaults, :robbery, 'effects')
+ assert_dom_equal %(\n\n\n\n\n\n\n\n\n), javascript_include_tag('controls',:defaults, :robbery, 'effects', :unique => false)
+ end
+
+ def test_duplicate_sources_should_only_be_included_once
+ ENV["RAILS_ASSET_ID"] = ""
+ ActionView::Helpers::AssetTagHelper::register_javascript_expansion :robbery => ["bank", "robber"],:banker => ["bank", "office"]
+ assert_dom_equal %(\n\n), javascript_include_tag(:robbery, :banker)
+ end
+
+ def test_duplicate_sources_should_be_all_included_when_required_is_false
+ ENV["RAILS_ASSET_ID"] = ""
+ ActionView::Helpers::AssetTagHelper::register_javascript_expansion :robbery => ["bank", "robber"],:banker => ["bank", "office"]
+ assert_dom_equal %(\n\n\n), javascript_include_tag(:robbery, :banker, :unique => false)
end
def test_custom_javascript_expansions_with_undefined_symbol