Changeset 6499
- Timestamp:
- 04/02/07 00:02:11 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/base.rb (modified) (1 diff)
- trunk/actionpack/lib/action_view/base.rb (modified) (8 diffs)
- trunk/actionpack/test/controller/render_test.rb (modified) (2 diffs)
- trunk/actionpack/test/fixtures/test/formatted_html_erb.html.erb (added)
- trunk/actionpack/test/fixtures/test/formatted_xml_erb.builder (added)
- trunk/actionpack/test/fixtures/test/formatted_xml_erb.html.erb (added)
- trunk/actionpack/test/fixtures/test/formatted_xml_erb.xml.erb (added)
- trunk/actionpack/test/template/base_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6493 r6499 1 1 *SVN* 2 3 * Change ActionView template defaults. Look for templates using the request format first, such as "show.html.erb" or "show.xml.builder", before looking for the old defaults like "show.erb" or "show.builder" [Rick] 2 4 3 5 * Highlight helper highlights one or many terms in a single pass. [Jeremy Kemper] trunk/actionpack/lib/action_controller/base.rb
r6435 r6499 1226 1226 def assert_existence_of_template_file(template_name) 1227 1227 unless template_exists?(template_name) || ignore_missing_templates 1228 full_template_path = @template.send(:full_template_path, template_name, 'erb')1228 full_template_path = @template.send(:full_template_path, template_name, "#{@template.send(:template_format)}.erb") 1229 1229 template_type = (template_name =~ /layouts/i) ? 'layout' : 'template' 1230 1230 raise(MissingTemplate, "Missing #{template_type} #{full_template_path}") trunk/actionpack/lib/action_view/base.rb
r6470 r6499 253 253 template_extension = pick_template_extension(template_path).to_s 254 254 template_file_name = full_template_path(template_path, template_extension) 255 template_extension.gsub!(/^\w+\./, '') # strip off any formats 255 256 end 256 257 else … … 331 332 332 333 def pick_template_extension(template_path)#:nodoc: 334 formatted_template_path = "#{template_path}.#{template_format}" 333 335 if @@cache_template_extensions 334 @@cached_template_extension[ template_path] ||= find_template_extension_for(template_path)336 @@cached_template_extension[formatted_template_path] ||= find_template_extension_for(template_path, formatted_template_path) 335 337 else 336 find_template_extension_for(template_path )338 find_template_extension_for(template_path, formatted_template_path) 337 339 end 338 340 end … … 358 360 def javascript_template_exists?(template_path)#:nodoc: 359 361 template_exists?(template_path, :rjs) 362 end 363 364 def formatted_template_exists?(formatted_template_exists) 365 [:erb, :builder, :rjs].each do |ext| 366 return ext if template_exists?(formatted_template_exists, ext) 367 end 368 nil 360 369 end 361 370 … … 365 374 template_exists?(template_file_name, template_file_extension) 366 375 else 367 cached_template_extension(template_path) || 368 %w(erb rhtml builder rxml javascript delegate).any? do |template_type| 369 send("#{template_type}_template_exists?", template_path) 370 end 376 formatted_template_path = "#{template_path}.#{template_format}" 377 cached_template_extension(formatted_template_path) || 378 formatted_template_exists?(formatted_template_path) || 379 %w(erb rhtml builder rxml javascript delegate).any? do |template_type| 380 send("#{template_type}_template_exists?", template_path) 381 end 371 382 end 372 383 end … … 375 386 def file_public?(template_path)#:nodoc: 376 387 template_path.split('/').last[0,1] != '_' 388 end 389 390 def template_format 391 if @template_format != false 392 # check controller.respond_to?(:request) in case its an ActionMailer::Base, or some other sneaky class. 393 @template_format = controller.respond_to?(:request) ? false : :html 394 if controller && controller.respond_to?(:request) && controller.request && controller.request.format 395 @template_format = controller.request.format == Mime::ALL ? :html : controller.request.format.to_sym 396 end 397 end 398 @template_format 377 399 end 378 400 … … 390 412 end 391 413 414 # Splits the path and extension from the given template_path and returns as an array. 392 415 def path_and_extension(template_path) 393 416 template_path_without_extension = template_path.sub(/\.(\w+)$/, '') … … 395 418 end 396 419 397 def cached_template_extension(template_path) 398 @@cache_template_extensions && @@cached_template_extension[template_path] 420 # Caches the extension for the given formatted template path. The extension may have the format 421 # too, such as 'html.erb'. 422 def cached_template_extension(formatted_template_path) 423 @@cache_template_extensions && @@cached_template_extension[formatted_template_path] 399 424 end 400 425 … … 405 430 406 431 # Determines the template's file extension, such as rhtml, rxml, or rjs. 407 def find_template_extension_for(template_path) 432 def find_template_extension_for(template_path, formatted_template_path = nil) 433 formatted_template_path ||= "#{template_path}.#{template_format}" 408 434 if match = delegate_template_exists?(template_path) 409 435 match.first.to_sym 410 elsif extension = erb_template_exists?(template_path): extension 411 elsif extension = builder_template_exists?(template_path): extension 436 elsif extension = formatted_template_exists?(formatted_template_path): "#{template_format}.#{extension}" 437 elsif extension = erb_template_exists?(template_path): extension 438 elsif extension = builder_template_exists?(template_path): extension 412 439 elsif javascript_template_exists?(template_path): :rjs 413 440 else trunk/actionpack/test/controller/render_test.rb
r6168 r6499 121 121 :locals => { "local_name" => name } 122 122 ActionView::Base.local_assigns_support_string_keys = false 123 end 124 125 def formatted_html_erb 126 end 127 128 def formatted_xml_erb 123 129 end 124 130 … … 342 348 end 343 349 350 def test_should_render_formatted_template 351 get :formatted_html_erb 352 assert_equal 'formatted html erb', @response.body 353 end 354 355 def test_should_render_formatted_xml_erb_template 356 get :formatted_xml_erb, :format => :xml 357 assert_equal '<test>passed formatted xml erb</test>', @response.body 358 end 359 360 def test_should_render_formatted_html_erb_template 361 get :formatted_xml_erb 362 assert_equal '<test>passed formatted html erb</test>', @response.body 363 end 344 364 345 365 protected trunk/actionpack/test/template/base_test.rb
r6498 r6499 15 15 end 16 16 17 def test_should_find_formatted_erb_extension 18 @template.expects(:delegate_template_exists?).with('foo').returns(nil) 19 @template.expects(:formatted_template_exists?).with('foo.html').returns("erb") 20 assert_equal "html.erb", @template.send(:find_template_extension_for, 'foo') 21 end 22 17 23 def test_should_find_erb_extension 18 24 @template.expects(:delegate_template_exists?).with('foo').returns(nil) 25 @template.expects(:formatted_template_exists?).with('foo.html').returns(nil) 19 26 @template.expects(:erb_template_exists?).with('foo').returns(:erb) 20 27 assert_equal :erb, @template.send(:find_template_extension_for, 'foo') … … 23 30 def test_should_find_builder_extension 24 31 @template.expects(:delegate_template_exists?).with('foo').returns(nil) 32 @template.expects(:formatted_template_exists?).with('foo.html').returns(nil) 25 33 @template.expects(:erb_template_exists?).with('foo').returns(nil) 26 34 @template.expects(:builder_template_exists?).with('foo').returns(:builder) … … 30 38 def test_should_find_javascript_extension 31 39 @template.expects(:delegate_template_exists?).with('foo').returns(nil) 40 @template.expects(:formatted_template_exists?).with('foo.html').returns(nil) 32 41 @template.expects(:erb_template_exists?).with('foo').returns(nil) 33 42 @template.expects(:builder_template_exists?).with('foo').returns(nil)