Skip to content
This repository
Browse code

Add :concat option to asset tag helpers to force concatenation.

This is useful for working around IE's stylesheet limit.

  stylesheet_link_tag :all, :concat => true
  • Loading branch information...
commit a491b19502781266b05918cf99b6ba67898e3be9 1 parent 447d60e
Joshua Peek authored June 15, 2009
24  actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -272,14 +272,17 @@ def javascript_path(source)
272 272
       #   javascript_include_tag :all, :cache => true, :recursive => true
273 273
       def javascript_include_tag(*sources)
274 274
         options = sources.extract_options!.stringify_keys
275  
-        cache   = options.delete("cache")
  275
+        concat  = options.delete("concat")
  276
+        cache   = concat || options.delete("cache")
276 277
         recursive = options.delete("recursive")
277 278
 
278  
-        if ActionController::Base.perform_caching && cache
  279
+        if concat || (ActionController::Base.perform_caching && cache)
279 280
           joined_javascript_name = (cache == true ? "all" : cache) + ".js"
280 281
           joined_javascript_path = File.join(joined_javascript_name[/^#{File::SEPARATOR}/] ? ASSETS_DIR : JAVASCRIPTS_DIR, joined_javascript_name)
281 282
 
282  
-          write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive)) unless File.exists?(joined_javascript_path)
  283
+          unless ActionController::Base.perform_caching && File.exists?(joined_javascript_path)
  284
+            write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive))
  285
+          end
283 286
           javascript_src_tag(joined_javascript_name, options)
284 287
         else
285 288
           expand_javascript_sources(sources, recursive).collect { |source| javascript_src_tag(source, options) }.join("\n")
@@ -410,16 +413,25 @@ def stylesheet_path(source)
410 413
       # The <tt>:recursive</tt> option is also available for caching:
411 414
       #
412 415
       #   stylesheet_link_tag :all, :cache => true, :recursive => true
  416
+      #
  417
+      # To force concatenation (even in development mode) set <tt>:concat</tt> to true. This is useful if
  418
+      # you have too many stylesheets for IE to load.
  419
+      #
  420
+      #   stylesheet_link_tag :all, :concat => true
  421
+      #
413 422
       def stylesheet_link_tag(*sources)
414 423
         options = sources.extract_options!.stringify_keys
415  
-        cache   = options.delete("cache")
  424
+        concat  = options.delete("concat")
  425
+        cache   = concat || options.delete("cache")
416 426
         recursive = options.delete("recursive")
417 427
 
418  
-        if ActionController::Base.perform_caching && cache
  428
+        if concat || (ActionController::Base.perform_caching && cache)
419 429
           joined_stylesheet_name = (cache == true ? "all" : cache) + ".css"
420 430
           joined_stylesheet_path = File.join(joined_stylesheet_name[/^#{File::SEPARATOR}/] ? ASSETS_DIR : STYLESHEETS_DIR, joined_stylesheet_name)
421 431
 
422  
-          write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources, recursive)) unless File.exists?(joined_stylesheet_path)
  432
+          unless ActionController::Base.perform_caching && File.exists?(joined_stylesheet_path)
  433
+            write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources, recursive))
  434
+          end
423 435
           stylesheet_tag(joined_stylesheet_name, options)
424 436
         else
425 437
           expand_stylesheet_sources(sources, recursive).collect { |source| stylesheet_tag(source, options) }.join("\n")
30  actionpack/test/template/asset_tag_helper_test.rb
@@ -567,6 +567,36 @@ def test_caching_stylesheet_link_tag_when_caching_on
567 567
     FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::ASSETS_DIR, 'absolute'))
568 568
   end
569 569
 
  570
+  def test_concat_stylesheet_link_tag_when_caching_off
  571
+    ENV["RAILS_ASSET_ID"] = ""
  572
+
  573
+    assert_dom_equal(
  574
+      %(<link href="/stylesheets/all.css" media="screen" rel="stylesheet" type="text/css" />),
  575
+      stylesheet_link_tag(:all, :concat => true)
  576
+    )
  577
+
  578
+    expected = Dir["#{ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR}/*.css"].map { |p| File.mtime(p) }.max
  579
+    assert_equal expected, File.mtime(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
  580
+
  581
+    assert_dom_equal(
  582
+      %(<link href="/stylesheets/money.css" media="screen" rel="stylesheet" type="text/css" />),
  583
+      stylesheet_link_tag(:all, :concat => "money")
  584
+    )
  585
+
  586
+    assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
  587
+
  588
+    assert_dom_equal(
  589
+      %(<link href="/absolute/test.css" media="screen" rel="stylesheet" type="text/css" />),
  590
+      stylesheet_link_tag(:all, :concat => "/absolute/test")
  591
+    )
  592
+
  593
+    assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::ASSETS_DIR, 'absolute', 'test.css'))
  594
+  ensure
  595
+    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
  596
+    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
  597
+    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::ASSETS_DIR, 'absolute'))
  598
+  end
  599
+
570 600
   def test_caching_stylesheet_link_tag_when_caching_on_with_proc_asset_host
571 601
     ENV["RAILS_ASSET_ID"] = ""
572 602
     ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" }

0 notes on commit a491b19

Please sign in to comment.
Something went wrong with that request. Please try again.