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

Ticket #10350: custom_asset_expansions.5.diff

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

Fixed problem with :all expansion not keeping the default sources at the start

  • test/fixtures/public/javascripts/prototype.js

    old new  
  • test/fixtures/public/javascripts/effects.js

    old new  
  • test/fixtures/public/javascripts/dragdrop.js

    old new  
  • test/fixtures/public/javascripts/application.js

    old new  
  • test/fixtures/public/javascripts/controls.js

    old new  
  • test/template/asset_tag_helper_test.rb

    old new  
    8484    %(javascript_include_tag("xmlhr", :lang => "vbscript")) => %(<script lang="vbscript" src="/javascripts/xmlhr.js" type="text/javascript"></script>), 
    8585    %(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>), 
    8686    %(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>), 
    87     %(javascript_include_tag(:all)) => %(<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>), 
     87    %(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>), 
    8888    %(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>), 
    8989    %(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>) 
    9090  } 
     
    175175    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) 
    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   
    178194  def test_stylesheet_path 
    179195    StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    180196  end 
     
    188204    StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    189205  end 
    190206 
     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') } 
     215  end 
     216 
    191217  def test_image_path 
    192218    ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    193219  end 
     
    319345    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'cache', 'money.js')) 
    320346  end 
    321347   
     348  def test_caching_javascript_include_tag_with_all_puts_defaults_at_the_start_of_the_file 
     349    ENV["RAILS_ASSET_ID"] = "" 
     350    ActionController::Base.asset_host = 'http://a0.example.com' 
     351    ActionController::Base.perform_caching = true 
     352     
     353    assert_dom_equal( 
     354      %(<script src="http://a0.example.com/javascripts/combined.js" type="text/javascript"></script>), 
     355      javascript_include_tag(:all, :cache => "combined") 
     356    ) 
     357 
     358    assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js')) 
     359     
     360    assert_equal( 
     361      %(// 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),  
     362      IO.read(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js')) 
     363    ) 
     364 
     365  ensure 
     366    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js')) 
     367  end 
     368   
    322369  def test_caching_javascript_include_tag_when_caching_off 
    323370    ENV["RAILS_ASSET_ID"] = "" 
    324371    ActionController::Base.perform_caching = false 
    325372     
    326373    assert_dom_equal( 
    327       %(<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>), 
     374      %(<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>), 
    328375      javascript_include_tag(:all, :cache => true) 
    329376    ) 
    330377 
    331378    assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js')) 
    332379     
    333380    assert_dom_equal( 
    334       %(<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>), 
     381      %(<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>), 
    335382      javascript_include_tag(:all, :cache => "money") 
    336383    ) 
    337384 
  • lib/action_view/helpers/asset_tag_helper.rb

    old new  
    155155      alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route 
    156156 
    157157      JAVASCRIPT_DEFAULT_SOURCES = ['prototype', 'effects', 'dragdrop', 'controls'] unless const_defined?(:JAVASCRIPT_DEFAULT_SOURCES) 
    158       @@javascript_default_sources = JAVASCRIPT_DEFAULT_SOURCES.dup 
     158      @@javascript_expansions = { :defaults => JAVASCRIPT_DEFAULT_SOURCES.dup } 
     159      @@stylesheet_expansions = {} 
    159160 
    160161      # Returns an html script tag for each of the +sources+ provided. You 
    161162      # can pass in the filename (.js extension is optional) of javascript files 
     
    248249          expand_javascript_sources(sources).collect { |source| javascript_src_tag(source, options) }.join("\n") 
    249250        end 
    250251      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 
    251282 
    252283      # Register one or more additional JavaScript files to be included when 
    253284      # <tt>javascript_include_tag :defaults</tt> is called. This method is 
    254285      # typically intended to be called from plugin initialization to register additional 
    255286      # .js files that the plugin installed in <tt>public/javascripts</tt>. 
    256287      def self.register_javascript_include_default(*sources) 
    257         @@javascript_default_sources.concat(sources) 
     288        @@javascript_expansions[:defaults].concat(sources) 
    258289      end 
    259  
     290       
    260291      def self.reset_javascript_include_default #:nodoc: 
    261         @@javascript_default_sources = JAVASCRIPT_DEFAULT_SOURCES.dup 
     292        @@javascript_expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup 
    262293      end 
    263  
     294       
    264295      # Computes the path to a stylesheet asset in the public stylesheets directory. 
    265296      # If the +source+ filename has no extension, .css will be appended. 
    266297      # Full paths from the document root will be passed through. 
     
    534565        end 
    535566 
    536567        def expand_javascript_sources(sources) 
    537           case 
    538           when sources.include?(:all) 
     568          if sources.include?(:all) 
    539569            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")) 
     570            @@all_javascript_sources ||= ((determine_source(:defaults, @@javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq 
     571          else 
     572            expanded_sources = sources.collect do |source| 
     573              determine_source(source, @@javascript_expansions) 
     574            end.flatten 
     575            expanded_sources << "application" if sources.include?(:defaults) && file_exist?(File.join(JAVASCRIPTS_DIR, "application.js")) 
     576            expanded_sources 
    549577          end 
    550  
    551           sources 
    552578        end 
    553579 
    554580        def expand_stylesheet_sources(sources) 
    555581          if sources.first == :all 
    556             @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 1).first }.sort 
     582            @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 0).first }.sort 
    557583          else 
    558             sources 
     584            sources.collect do |source| 
     585              determine_source(source, @@stylesheet_expansions) 
     586            end.flatten 
    559587          end 
    560588        end 
     589         
     590        def determine_source(source, collection) 
     591          case source 
     592          when Symbol 
     593            collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}") 
     594          else 
     595            source 
     596          end 
     597        end 
    561598 
    562599        def join_asset_file_contents(paths) 
    563600          paths.collect { |path| File.read(File.join(ASSETS_DIR, path.split("?").first)) }.join("\n\n")