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

Ticket #10350: custom_asset_expansions.3.diff

File custom_asset_expansions.3.diff, 9.0 kB (added by lotswholetime, 2 months ago)

Added documentation

  • test/template/asset_tag_helper_test.rb

    old new  
    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   
    178189  def test_stylesheet_path 
    179190    StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    180191  end 
     
    188199    StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    189200  end 
    190201 
     202  def test_custom_stylesheet_expansions 
     203    ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey, "head", "body", "tail" 
     204    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') 
     205  end 
     206 
    191207  def test_image_path 
    192208    ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 
    193209  end 
  • 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(symbol, *sources) 
     265        @@javascript_expansions[symbol] = sources 
     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(symbol, *sources) 
     280        @@stylesheet_expansions[symbol] = sources 
     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) 
    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")) 
     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             @@javascript_expansions[source] || source 
     573           end.flatten 
     574           expanded_sources << "application" if sources.include?(:defaults) && file_exist?(File.join(JAVASCRIPTS_DIR, "application.js")) 
     575           expanded_sources 
    549576          end 
    550  
    551           sources 
    552577        end 
    553578 
    554579        def expand_stylesheet_sources(sources) 
    555580          if sources.first == :all 
    556             @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 1).first }.sort 
     581            @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 0).first }.sort 
    557582          else 
    558             sources 
     583            sources.collect do |source| 
     584              @@stylesheet_expansions[source] || source 
     585            end.flatten 
    559586          end 
    560587        end 
    561588