public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/josh/rails.git
Add :recursive option to javascript_include_tag and stylesheet_link_tag to 
be used along with :all. [#480 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
djanowski (author)
Fri Jul 04 13:12:30 -0700 2008
lifo (committer)
Tue Jul 08 05:50:59 -0700 2008
commit  91320f2a809d3a59efabd9f839fe9b879f4a9b29
tree    38c4c015ad32b5773edf2fd28437eb622a9a75e8
parent  2f4aaed7b3feb3be787a316fab3144c06bb21a27
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *Edge*
0
 
0
+* Add :recursive option to javascript_include_tag and stylesheet_link_tag to be used along with :all. #480 [Damian Janowski]
0
+
0
 * Disable the Accept header by default [Michael Koziarski]
0
 
0
     The accept header is poorly implemented by browsers and causes strange
...
209
210
211
 
 
 
 
212
213
214
...
235
236
237
 
 
 
 
238
239
240
 
241
242
243
244
245
246
 
247
248
249
 
250
251
252
...
332
333
334
335
 
336
337
338
339
340
341
 
 
 
 
342
343
344
...
362
363
364
 
 
 
 
365
366
367
 
368
369
370
371
372
373
 
374
375
376
 
377
378
379
...
556
557
558
559
560
 
 
561
562
563
564
 
 
565
566
567
 
568
569
570
 
 
 
571
572
573
...
577
578
579
580
 
581
582
 
 
583
584
585
...
604
605
606
 
 
 
 
 
 
 
 
607
608
609
...
209
210
211
212
213
214
215
216
217
218
...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
 
255
256
257
 
258
259
260
261
...
341
342
343
 
344
345
346
347
348
349
350
351
352
353
354
355
356
357
...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
 
391
392
393
 
394
395
396
397
...
574
575
576
 
 
577
578
579
580
 
 
581
582
583
584
 
585
586
 
 
587
588
589
590
591
592
...
596
597
598
 
599
600
 
601
602
603
604
605
...
624
625
626
627
628
629
630
631
632
633
634
635
636
637
0
@@ -209,6 +209,10 @@ module ActionView
0
       # Note that the default javascript files will be included first. So Prototype and Scriptaculous are available to
0
       # all subsequently included files.
0
       #
0
+ # If you want Rails to search in all the subdirectories under javascripts, you should explicitly set <tt>:recursive</tt>:
0
+ #
0
+ # javascript_include_tag :all, :recursive => true
0
+ #
0
       # == Caching multiple javascripts into one
0
       #
0
       # You can also cache multiple javascripts into one file, which requires less HTTP connections to download and can better be
0
@@ -235,18 +239,23 @@ module ActionView
0
       #
0
       # javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is true =>
0
       # <script type="text/javascript" src="/javascripts/shop.js"></script>
0
+ #
0
+ # The <tt>:recursive</tt> option is also available for caching:
0
+ #
0
+ # javascript_include_tag :all, :cache => true, :recursive => true
0
       def javascript_include_tag(*sources)
0
         options = sources.extract_options!.stringify_keys
0
         cache = options.delete("cache")
0
+ recursive = options.delete("recursive")
0
 
0
         if ActionController::Base.perform_caching && cache
0
           joined_javascript_name = (cache == true ? "all" : cache) + ".js"
0
           joined_javascript_path = File.join(JAVASCRIPTS_DIR, joined_javascript_name)
0
 
0
- write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources)) unless File.exists?(joined_javascript_path)
0
+ write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive)) unless File.exists?(joined_javascript_path)
0
           javascript_src_tag(joined_javascript_name, options)
0
         else
0
- expand_javascript_sources(sources).collect { |source| javascript_src_tag(source, options) }.join("\n")
0
+ expand_javascript_sources(sources, recursive).collect { |source| javascript_src_tag(source, options) }.join("\n")
0
         end
0
       end
0
 
0
@@ -332,13 +341,17 @@ module ActionView
0
       # <link href="/stylesheets/random.styles" media="screen" rel="stylesheet" type="text/css" />
0
       # <link href="/css/stylish.css" media="screen" rel="stylesheet" type="text/css" />
0
       #
0
- # You can also include all styles in the stylesheet directory using <tt>:all</tt> as the source:
0
+ # You can also include all styles in the stylesheets directory using <tt>:all</tt> as the source:
0
       #
0
       # stylesheet_link_tag :all # =>
0
       # <link href="/stylesheets/style1.css" media="screen" rel="stylesheet" type="text/css" />
0
       # <link href="/stylesheets/styleB.css" media="screen" rel="stylesheet" type="text/css" />
0
       # <link href="/stylesheets/styleX2.css" media="screen" rel="stylesheet" type="text/css" />
0
       #
0
+ # If you want Rails to search in all the subdirectories under stylesheets, you should explicitly set <tt>:recursive</tt>:
0
+ #
0
+ # stylesheet_link_tag :all, :recursive => true
0
+ #
0
       # == Caching multiple stylesheets into one
0
       #
0
       # You can also cache multiple stylesheets into one file, which requires less HTTP connections and can better be
0
@@ -362,18 +375,23 @@ module ActionView
0
       #
0
       # stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when ActionController::Base.perform_caching is true =>
0
       # <link href="/stylesheets/payment.css" media="screen" rel="stylesheet" type="text/css" />
0
+ #
0
+ # The <tt>:recursive</tt> option is also available for caching:
0
+ #
0
+ # stylesheet_link_tag :all, :cache => true, :recursive => true
0
       def stylesheet_link_tag(*sources)
0
         options = sources.extract_options!.stringify_keys
0
         cache = options.delete("cache")
0
+ recursive = options.delete("recursive")
0
 
0
         if ActionController::Base.perform_caching && cache
0
           joined_stylesheet_name = (cache == true ? "all" : cache) + ".css"
0
           joined_stylesheet_path = File.join(STYLESHEETS_DIR, joined_stylesheet_name)
0
 
0
- write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources)) unless File.exists?(joined_stylesheet_path)
0
+ write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources, recursive)) unless File.exists?(joined_stylesheet_path)
0
           stylesheet_tag(joined_stylesheet_name, options)
0
         else
0
- expand_stylesheet_sources(sources).collect { |source| stylesheet_tag(source, options) }.join("\n")
0
+ expand_stylesheet_sources(sources, recursive).collect { |source| stylesheet_tag(source, options) }.join("\n")
0
         end
0
       end
0
 
0
@@ -556,18 +574,19 @@ module ActionView
0
           tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => html_escape(path_to_stylesheet(source)) }.merge(options), false, false)
0
         end
0
 
0
- def compute_javascript_paths(sources)
0
- expand_javascript_sources(sources).collect { |source| compute_public_path(source, 'javascripts', 'js', false) }
0
+ def compute_javascript_paths(*args)
0
+ expand_javascript_sources(*args).collect { |source| compute_public_path(source, 'javascripts', 'js', false) }
0
         end
0
 
0
- def compute_stylesheet_paths(sources)
0
- expand_stylesheet_sources(sources).collect { |source| compute_public_path(source, 'stylesheets', 'css', false) }
0
+ def compute_stylesheet_paths(*args)
0
+ expand_stylesheet_sources(*args).collect { |source| compute_public_path(source, 'stylesheets', 'css', false) }
0
         end
0
 
0
- def expand_javascript_sources(sources)
0
+ def expand_javascript_sources(sources, recursive = false)
0
           if sources.include?(:all)
0
- all_javascript_files = Dir[File.join(JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).gsub(/\.\w+$/, '') }.sort
0
- @@all_javascript_sources ||= ((determine_source(:defaults, @@javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq
0
+ all_javascript_files = collect_asset_files(JAVASCRIPTS_DIR, ('**' if recursive), '*.js')
0
+ @@all_javascript_sources ||= {}
0
+ @@all_javascript_sources[recursive] ||= ((determine_source(:defaults, @@javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq
0
           else
0
             expanded_sources = sources.collect do |source|
0
               determine_source(source, @@javascript_expansions)
0
@@ -577,9 +596,10 @@ module ActionView
0
           end
0
         end
0
 
0
- def expand_stylesheet_sources(sources)
0
+ def expand_stylesheet_sources(sources, recursive)
0
           if sources.first == :all
0
- @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).gsub(/\.\w+$/, '') }.sort
0
+ @@all_stylesheet_sources ||= {}
0
+ @@all_stylesheet_sources[recursive] ||= collect_asset_files(STYLESHEETS_DIR, ('**' if recursive), '*.css')
0
           else
0
             sources.collect do |source|
0
               determine_source(source, @@stylesheet_expansions)
0
@@ -604,6 +624,14 @@ module ActionView
0
           FileUtils.mkdir_p(File.dirname(joined_asset_path))
0
           File.open(joined_asset_path, "w+") { |cache| cache.write(join_asset_file_contents(asset_paths)) }
0
         end
0
+
0
+ def collect_asset_files(*path)
0
+ dir = path.first
0
+
0
+ Dir[File.join(*path.compact)].collect do |file|
0
+ file[-(file.size - dir.size - 1)..-1].sub(/\.\w+$/, '')
0
+ end.sort
0
+ end
0
     end
0
   end
0
 end
...
83
84
85
 
86
87
88
...
108
109
110
 
111
112
113
...
343
344
345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
347
348
...
373
374
375
 
 
 
 
 
376
377
378
...
380
381
382
 
 
 
 
 
383
384
385
...
432
433
434
 
 
 
 
 
435
436
437
...
439
440
441
 
 
 
 
 
442
443
444
...
83
84
85
86
87
88
89
...
109
110
111
112
113
114
115
...
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
...
396
397
398
399
400
401
402
403
404
405
406
...
408
409
410
411
412
413
414
415
416
417
418
...
465
466
467
468
469
470
471
472
473
474
475
...
477
478
479
480
481
482
483
484
485
486
487
0
@@ -83,6 +83,7 @@ class AssetTagHelperTest < ActionView::TestCase
0
     %(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(<script src="/javascripts/common.javascript" type="text/javascript"></script>\n<script src="/elsewhere/cools.js" type="text/javascript"></script>),
0
     %(javascript_include_tag(:defaults)) => %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>),
0
     %(javascript_include_tag(:all)) => %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>\n<script src="/javascripts/bank.js" type="text/javascript"></script>\n<script src="/javascripts/robber.js" type="text/javascript"></script>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
0
+ %(javascript_include_tag(:all, :recursive => true)) => %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>\n<script src="/javascripts/bank.js" type="text/javascript"></script>\n<script src="/javascripts/robber.js" type="text/javascript"></script>\n<script src="/javascripts/subdir/subdir.js" type="text/javascript"></script>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
0
     %(javascript_include_tag(:defaults, "test")) => %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/test.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>),
0
     %(javascript_include_tag("test", :defaults)) => %(<script src="/javascripts/test.js" type="text/javascript"></script>\n<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>)
0
   }
0
@@ -108,6 +109,7 @@ class AssetTagHelperTest < ActionView::TestCase
0
     %(stylesheet_link_tag("dir/file")) => %(<link href="/stylesheets/dir/file.css" media="screen" rel="stylesheet" type="text/css" />),
0
     %(stylesheet_link_tag("style", :media => "all")) => %(<link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" />),
0
     %(stylesheet_link_tag(:all)) => %(<link href="/stylesheets/bank.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/robber.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
0
+ %(stylesheet_link_tag(:all, :recursive => true)) => %(<link href="/stylesheets/bank.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/robber.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/subdir/subdir.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
0
     %(stylesheet_link_tag(:all, :media => "all")) => %(<link href="/stylesheets/bank.css" media="all" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/robber.css" media="all" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/version.1.0.css" media="all" rel="stylesheet" type="text/css" />),
0
     %(stylesheet_link_tag("random.styles", "/css/stylish")) => %(<link href="/stylesheets/random.styles" media="screen" rel="stylesheet" type="text/css" />\n<link href="/css/stylish.css" media="screen" rel="stylesheet" type="text/css" />),
0
     %(stylesheet_link_tag("http://www.example.com/styles/style")) => %(<link href="http://www.example.com/styles/style.css" media="screen" rel="stylesheet" type="text/css" />)
0
@@ -343,6 +345,27 @@ class AssetTagHelperTest < ActionView::TestCase
0
     FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'cache', 'money.js'))
0
   end
0
   
0
+ def test_caching_javascript_include_tag_with_all_and_recursive_puts_defaults_at_the_start_of_the_file
0
+ ENV["RAILS_ASSET_ID"] = ""
0
+ ActionController::Base.asset_host = 'http://a0.example.com'
0
+ ActionController::Base.perform_caching = true
0
+
0
+ assert_dom_equal(
0
+ %(<script src="http://a0.example.com/javascripts/combined.js" type="text/javascript"></script>),
0
+ javascript_include_tag(:all, :cache => "combined", :recursive => true)
0
+ )
0
+
0
+ assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js'))
0
+
0
+ assert_equal(
0
+ %(// prototype js\n\n// effects js\n\n// dragdrop js\n\n// controls js\n\n// application js\n\n// bank js\n\n// robber js\n\n// subdir js\n\n\n// version.1.0 js),
0
+ IO.read(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js'))
0
+ )
0
+
0
+ ensure
0
+ FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js'))
0
+ end
0
+
0
   def test_caching_javascript_include_tag_with_all_puts_defaults_at_the_start_of_the_file
0
     ENV["RAILS_ASSET_ID"] = ""
0
     ActionController::Base.asset_host = 'http://a0.example.com'
0
@@ -373,6 +396,11 @@ class AssetTagHelperTest < ActionView::TestCase
0
       javascript_include_tag(:all, :cache => true)
0
     )
0
 
0
+ assert_dom_equal(
0
+ %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>\n<script src="/javascripts/bank.js" type="text/javascript"></script>\n<script src="/javascripts/robber.js" type="text/javascript"></script>\n<script src="/javascripts/subdir/subdir.js" type="text/javascript"></script>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
0
+ javascript_include_tag(:all, :cache => true, :recursive => true)
0
+ )
0
+
0
     assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
0
     
0
     assert_dom_equal(
0
@@ -380,6 +408,11 @@ class AssetTagHelperTest < ActionView::TestCase
0
       javascript_include_tag(:all, :cache => "money")
0
     )
0
 
0
+ assert_dom_equal(
0
+ %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>\n<script src="/javascripts/bank.js" type="text/javascript"></script>\n<script src="/javascripts/robber.js" type="text/javascript"></script>\n<script src="/javascripts/subdir/subdir.js" type="text/javascript"></script>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
0
+ javascript_include_tag(:all, :cache => "money", :recursive => true)
0
+ )
0
+
0
     assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
0
   end
0
 
0
@@ -432,6 +465,11 @@ class AssetTagHelperTest < ActionView::TestCase
0
       stylesheet_link_tag(:all, :cache => true)
0
     )
0
 
0
+ assert_dom_equal(
0
+ %(<link href="/stylesheets/bank.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/robber.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/subdir/subdir.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
0
+ stylesheet_link_tag(:all, :cache => true, :recursive => true)
0
+ )
0
+
0
     assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
0
     
0
     assert_dom_equal(
0
@@ -439,6 +477,11 @@ class AssetTagHelperTest < ActionView::TestCase
0
       stylesheet_link_tag(:all, :cache => "money")
0
     )
0
 
0
+ assert_dom_equal(
0
+ %(<link href="/stylesheets/bank.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/robber.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/subdir/subdir.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
0
+ stylesheet_link_tag(:all, :cache => "money", :recursive => true)
0
+ )
0
+
0
     assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
0
   end
0
 end

Comments

    No one has commented yet.