Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9063

Show
Ignore:
Timestamp:
03/19/08 19:56:07 (2 months ago)
Author:
bitsweat
Message:

Revert [9106]. References #10350.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r9055 r9063  
    1010 
    1111* Fix more obscure nested parameter hash parsing bug.  #10797 [thomas.lee] 
    12  
    13 * Added ActionView::Helpers::register_javascript/stylesheet_expansion to make it easier for plugin developers to inject multiple assets #10350 [lotswholetime] 
    1412 
    1513* Fix nested parameter hash parsing bug.  #10797 [thomas.lee] 
  • trunk/actionpack/lib/action_view/helpers/asset_tag_helper.rb

    r9029 r9063  
    156156 
    157157      JAVASCRIPT_DEFAULT_SOURCES = ['prototype', 'effects', 'dragdrop', 'controls'] unless const_defined?(:JAVASCRIPT_DEFAULT_SOURCES) 
    158       @@javascript_expansions = { :defaults => JAVASCRIPT_DEFAULT_SOURCES.dup } 
    159       @@stylesheet_expansions = {} 
     158      @@javascript_default_sources = JAVASCRIPT_DEFAULT_SOURCES.dup 
    160159 
    161160      # Returns an html script tag for each of the +sources+ provided. You 
     
    250249        end 
    251250      end 
    252        
    253       # Register one or more javascript files to be included when <tt>symbol</tt> 
    254       # is passed to <tt>javascript_include_tag</tt>. This method is typically intended  
    255       # to be called from plugin initialization to register javascript files 
    256       # that the plugin installed in <tt>public/javascripts</tt>. 
    257       #  
    258       #   ActionView::Helpers::AssetTagHelper.register_javascript_expansion :monkey => ["head", "body", "tail"] 
    259       #  
    260       #   javascript_include_tag :monkey # => 
    261       #     <script type="text/javascript" src="/javascripts/head.js"></script> 
    262       #     <script type="text/javascript" src="/javascripts/body.js"></script> 
    263       #     <script type="text/javascript" src="/javascripts/tail.js"></script> 
    264       def self.register_javascript_expansion(expansions) 
    265         @@javascript_expansions.merge!(expansions) 
    266       end 
    267        
    268       # Register one or more stylesheet files to be included when <tt>symbol</tt> 
    269       # is passed to <tt>stylesheet_link_tag</tt>. This method is typically intended  
    270       # to be called from plugin initialization to register stylesheet files 
    271       # that the plugin installed in <tt>public/stylesheets</tt>. 
    272       #  
    273       #   ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion :monkey => ["head", "body", "tail"] 
    274       #  
    275       #   stylesheet_link_tag :monkey # => 
    276       #     <link href="/stylesheets/head.css"  media="screen" rel="stylesheet" type="text/css" /> 
    277       #     <link href="/stylesheets/body.css"  media="screen" rel="stylesheet" type="text/css" /> 
    278       #     <link href="/stylesheets/tail.css"  media="screen" rel="stylesheet" type="text/css" /> 
    279       def self.register_stylesheet_expansion(expansions) 
    280         @@stylesheet_expansions.merge!(expansions) 
    281       end 
    282251 
    283252      # Register one or more additional JavaScript files to be included when 
     
    286255      # .js files that the plugin installed in <tt>public/javascripts</tt>. 
    287256      def self.register_javascript_include_default(*sources) 
    288         @@javascript_expansions[:defaults].concat(sources) 
    289       end 
    290        
     257        @@javascript_default_sources.concat(sources) 
     258      end 
     259 
    291260      def self.reset_javascript_include_default #:nodoc: 
    292         @@javascript_expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup 
    293       end 
    294        
     261        @@javascript_default_sources = JAVASCRIPT_DEFAULT_SOURCES.dup 
     262      end 
     263 
    295264      # Computes the path to a stylesheet asset in the public stylesheets directory. 
    296265      # If the +source+ filename has no extension, .css will be appended. 
     
    566535 
    567536        def expand_javascript_sources(sources) 
    568          if sources.include?(:all) 
    569            @@all_javascript_sources ||= Dir[File.join(JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).split(".", 0).first }.sort 
    570          else 
    571            expanded_sources = sources.collect do |source| 
    572              determine_source(source, @@javascript_expansions) 
    573            end.flatten 
    574            expanded_sources << "application" if sources.include?(:defaults) && file_exist?(File.join(JAVASCRIPTS_DIR, "application.js")) 
    575            expanded_sources 
    576           end 
     537          case 
     538          when sources.include?(:all) 
     539            all_javascript_files = Dir[File.join(JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).split(".", 0).first }.sort 
     540            sources = ((@@javascript_default_sources.dup & all_javascript_files) + all_javascript_files).uniq 
     541 
     542          when sources.include?(:defaults) 
     543            sources = sources[0..(sources.index(:defaults))] +  
     544              @@javascript_default_sources.dup +  
     545              sources[(sources.index(:defaults) + 1)..sources.length] 
     546 
     547            sources.delete(:defaults) 
     548            sources << "application" if file_exist?(File.join(JAVASCRIPTS_DIR, "application.js")) 
     549          end 
     550 
     551          sources 
    577552        end 
    578553 
    579554        def expand_stylesheet_sources(sources) 
    580555          if sources.first == :all 
    581             @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 0).first }.sort 
     556            @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 1).first }.sort 
    582557          else 
    583             sources.collect do |source| 
    584               determine_source(source, @@stylesheet_expansions) 
    585             end.flatten 
    586           end 
    587         end 
    588          
    589         def determine_source(source, collection) 
    590           case source 
    591           when Symbol 
    592             collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}") 
    593           else 
    594             source 
     558            sources 
    595559          end 
    596560        end 
  • trunk/actionpack/test/template/asset_tag_helper_test.rb

    r9016 r9063  
    176176  end 
    177177   
    178   def test_custom_javascript_expansions 
    179     ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"] 
    180     assert_dom_equal  %(<script src="/javascripts/first.js" type="text/javascript"></script>\n<script src="/javascripts/head.js" type="text/javascript"></script>\n<script src="/javascripts/body.js" type="text/javascript"></script>\n<script src="/javascripts/tail.js" type="text/javascript"></script>\n<script src="/javascripts/last.js" type="text/javascript"></script>), javascript_include_tag('first', :monkey, 'last') 
    181   end 
    182    
    183   def test_custom_javascript_expansions_and_defaults_puts_application_js_at_the_end 
    184     ENV["RAILS_ASSET_ID"] = "" 
    185     ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"] 
    186     assert_dom_equal  %(<script src="/javascripts/first.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/head.js" type="text/javascript"></script>\n<script src="/javascripts/body.js" type="text/javascript"></script>\n<script src="/javascripts/tail.js" type="text/javascript"></script>\n<script src="/javascripts/last.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>), javascript_include_tag('first', :defaults, :monkey, 'last') 
    187   end 
    188    
    189   def test_custom_javascript_expansions_with_undefined_symbol 
    190     ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => nil 
    191     assert_raise(ArgumentError) { javascript_include_tag('first', :monkey, 'last') } 
    192   end 
    193    
    194178  def test_stylesheet_path 
    195179    StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
     
    203187    ENV["RAILS_ASSET_ID"] = "" 
    204188    StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    205   end 
    206  
    207   def test_custom_stylesheet_expansions 
    208     ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey => ["head", "body", "tail"] 
    209     assert_dom_equal  %(<link href="/stylesheets/first.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/head.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/body.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/tail.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/last.css" media="screen" rel="stylesheet" type="text/css" />), stylesheet_link_tag('first', :monkey, 'last') 
    210   end 
    211  
    212   def test_custom_stylesheet_expansions_with_undefined_symbol 
    213     ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey => nil 
    214     assert_raise(ArgumentError) { stylesheet_link_tag('first', :monkey, 'last') } 
    215189  end 
    216190