Ticket #10350: custom_asset_expansions.3.diff
| File custom_asset_expansions.3.diff, 9.0 kB (added by lotswholetime, 2 months ago) |
|---|
-
test/template/asset_tag_helper_test.rb
old new 175 175 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) 176 176 end 177 177 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 178 189 def test_stylesheet_path 179 190 StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 180 191 end … … 188 199 StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 189 200 end 190 201 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 191 207 def test_image_path 192 208 ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 193 209 end -
lib/action_view/helpers/asset_tag_helper.rb
old new 155 155 alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route 156 156 157 157 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 = {} 159 160 160 161 # Returns an html script tag for each of the +sources+ provided. You 161 162 # can pass in the filename (.js extension is optional) of javascript files … … 248 249 expand_javascript_sources(sources).collect { |source| javascript_src_tag(source, options) }.join("\n") 249 250 end 250 251 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 251 282 252 283 # Register one or more additional JavaScript files to be included when 253 284 # <tt>javascript_include_tag :defaults</tt> is called. This method is 254 285 # typically intended to be called from plugin initialization to register additional 255 286 # .js files that the plugin installed in <tt>public/javascripts</tt>. 256 287 def self.register_javascript_include_default(*sources) 257 @@javascript_ default_sources.concat(sources)288 @@javascript_expansions[:defaults].concat(sources) 258 289 end 259 290 260 291 def self.reset_javascript_include_default #:nodoc: 261 @@javascript_ default_sources= JAVASCRIPT_DEFAULT_SOURCES.dup292 @@javascript_expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup 262 293 end 263 294 264 295 # Computes the path to a stylesheet asset in the public stylesheets directory. 265 296 # If the +source+ filename has no extension, .css will be appended. 266 297 # Full paths from the document root will be passed through. … … 534 565 end 535 566 536 567 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 549 576 end 550 551 sources552 577 end 553 578 554 579 def expand_stylesheet_sources(sources) 555 580 if sources.first == :all 556 @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 1).first }.sort581 @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 0).first }.sort 557 582 else 558 sources 583 sources.collect do |source| 584 @@stylesheet_expansions[source] || source 585 end.flatten 559 586 end 560 587 end 561 588