Ticket #10350: custom_asset_expansions.2.diff
| File custom_asset_expansions.2.diff, 7.4 kB (added by lotswholetime, 5 months ago) |
|---|
-
test/template/asset_tag_helper_test.rb
old new 172 172 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) 173 173 end 174 174 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 175 186 def test_stylesheet_path 176 187 StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 177 188 end … … 185 196 StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 186 197 end 187 198 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 188 204 def test_image_path 189 205 ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } 190 206 end -
lib/action_view/helpers/asset_tag_helper.rb
old new 114 114 alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route 115 115 116 116 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 = {} 118 119 119 120 # Returns an html script tag for each of the +sources+ provided. You 120 121 # can pass in the filename (.js extension is optional) of javascript files … … 220 221 end.join("\n") 221 222 end 222 223 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 223 232 224 233 # Register one or more additional JavaScript files to be included when 225 234 # <tt>javascript_include_tag :defaults</tt> is called. This method is 226 235 # typically intended to be called from plugin initialization to register additional 227 236 # .js files that the plugin installed in <tt>public/javascripts</tt>. 228 237 def self.register_javascript_include_default(*sources) 229 @@javascript_ default_sources.concat(sources)238 @@javascript_expansions[:defaults].concat(sources) 230 239 end 231 240 232 241 def self.reset_javascript_include_default #:nodoc: 233 @@javascript_ default_sources= JAVASCRIPT_DEFAULT_SOURCES.dup242 @@javascript_expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup 234 243 end 235 244 236 245 # Computes the path to a stylesheet asset in the public stylesheets directory. 237 246 # If the +source+ filename has no extension, .css will be appended. 238 247 # Full paths from the document root will be passed through. … … 493 502 end 494 503 495 504 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 508 513 end 509 510 sources511 514 end 512 515 513 516 def expand_stylesheet_sources(sources) 514 517 if sources.first == :all 515 @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 1).first }.sort518 @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 0).first }.sort 516 519 else 517 sources 520 sources.collect do |source| 521 @@stylesheet_expansions[source] || source 522 end.flatten 518 523 end 519 524 end 520 525