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

Ticket #10350: custom_asset_expansions.2.diff

File custom_asset_expansions.2.diff, 7.4 kB (added by lotswholetime, 5 months ago)

keep application.js at the end

  • test/template/asset_tag_helper_test.rb

    old new  
    172172    assert_dom_equal  %(<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/slider.js" type="text/javascript"></script>\n<script src="/javascripts/lib1.js" type="text/javascript"></script>\n<script src="/elsewhere/blub/lib2.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>), javascript_include_tag(:defaults) 
    173173  end 
    174174   
     175  def test_custom_javascript_expansions 
     176    ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey, "head", "body", "tail" 
     177    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') 
     178  end 
     179   
     180  def test_custom_javascript_expansions_and_defaults_puts_application_js_at_the_end 
     181    ENV["RAILS_ASSET_ID"] = "" 
     182    ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey, "head", "body", "tail" 
     183    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') 
     184  end 
     185   
    175186  def test_stylesheet_path 
    176187    StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    177188  end 
     
    185196    StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    186197  end 
    187198 
     199  def test_custom_stylesheet_expansions 
     200    ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey, "head", "body", "tail" 
     201    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') 
     202  end 
     203 
    188204  def test_image_path 
    189205    ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    190206  end 
  • lib/action_view/helpers/asset_tag_helper.rb

    old new  
    114114      alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route 
    115115 
    116116      JAVASCRIPT_DEFAULT_SOURCES = ['prototype', 'effects', 'dragdrop', 'controls'] unless const_defined?(:JAVASCRIPT_DEFAULT_SOURCES) 
    117       @@javascript_default_sources = JAVASCRIPT_DEFAULT_SOURCES.dup 
     117      @@javascript_expansions = { :defaults => JAVASCRIPT_DEFAULT_SOURCES.dup } 
     118      @@stylesheet_expansions = {} 
    118119 
    119120      # Returns an html script tag for each of the +sources+ provided. You 
    120121      # can pass in the filename (.js extension is optional) of javascript files 
     
    220221          end.join("\n") 
    221222        end 
    222223      end 
     224       
     225      def self.register_javascript_expansion(symbol, *sources) 
     226        @@javascript_expansions[symbol] = sources 
     227      end 
     228       
     229      def self.register_stylesheet_expansion(symbol, *sources) 
     230        @@stylesheet_expansions[symbol] = sources 
     231      end 
    223232 
    224233      # Register one or more additional JavaScript files to be included when 
    225234      # <tt>javascript_include_tag :defaults</tt> is called. This method is 
    226235      # typically intended to be called from plugin initialization to register additional 
    227236      # .js files that the plugin installed in <tt>public/javascripts</tt>. 
    228237      def self.register_javascript_include_default(*sources) 
    229         @@javascript_default_sources.concat(sources) 
     238        @@javascript_expansions[:defaults].concat(sources) 
    230239      end 
    231  
     240       
    232241      def self.reset_javascript_include_default #:nodoc: 
    233         @@javascript_default_sources = JAVASCRIPT_DEFAULT_SOURCES.dup 
     242        @@javascript_expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup 
    234243      end 
    235  
     244       
    236245      # Computes the path to a stylesheet asset in the public stylesheets directory. 
    237246      # If the +source+ filename has no extension, .css will be appended. 
    238247      # Full paths from the document root will be passed through. 
     
    493502        end 
    494503 
    495504        def expand_javascript_sources(sources)           
    496           case 
    497           when sources.include?(:all) 
    498             all_javascript_files = Dir[File.join(JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).split(".", 0).first }.sort 
    499             sources = ((@@javascript_default_sources.dup & all_javascript_files) + all_javascript_files).uniq 
    500  
    501           when sources.include?(:defaults) 
    502             sources = sources[0..(sources.index(:defaults))] +  
    503               @@javascript_default_sources.dup +  
    504               sources[(sources.index(:defaults) + 1)..sources.length] 
    505  
    506             sources.delete(:defaults) 
    507             sources << "application" if file_exist?(File.join(JAVASCRIPTS_DIR, "application.js")) 
     505          if sources.include?(:all) 
     506            @@all_javascript_sources ||= Dir[File.join(JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).split(".", 0).first }.sort 
     507          else 
     508            expanded_sources = sources.collect do |source| 
     509              @@javascript_expansions[source] || source 
     510            end.flatten 
     511            expanded_sources << "application" if sources.include?(:defaults) && file_exist?(File.join(JAVASCRIPTS_DIR, "application.js")) 
     512            expanded_sources 
    508513          end 
    509  
    510           sources 
    511514        end 
    512515 
    513516        def expand_stylesheet_sources(sources) 
    514517          if sources.first == :all 
    515             @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 1).first }.sort 
     518            @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 0).first }.sort 
    516519          else 
    517             sources 
     520            sources.collect do |source| 
     521              @@stylesheet_expansions[source] || source 
     522            end.flatten 
    518523          end 
    519524        end 
    520525