Changeset 6517
- Timestamp:
- 04/12/07 20:25:32 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/base.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller/mime_responds.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_controller/request.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_view/base.rb (modified) (7 diffs)
- trunk/actionpack/test/controller/mime_responds_test.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/render_test.rb (modified) (1 diff)
- trunk/actionpack/test/controller/request_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6516 r6517 1 1 *SVN* 2 3 * The default respond_to blocks don't set a specific extension anymore, so that both 'show.rjs' and 'show.js.rjs' will work. [Rick] 2 4 3 5 * Allow layouts with extension of .html.erb. Closes #8032 [Josh Knowles] trunk/actionpack/lib/action_controller/base.rb
r6499 r6517 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, "#{@template.send(:template_format)}.erb")1228 full_template_path = template_name.include?('.') ? template_name : @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_controller/mime_responds.rb
r6507 r6517 108 108 109 109 class Responder #:nodoc: 110 default_block_format = %(Proc.new { render :action => "\#{action_name}%s", :content_type => Mime::%s }) 111 DEFAULT_BLOCKS = {} 112 DEFAULT_BLOCKS[:html] = default_block_format % ['', 'HTML'] 113 DEFAULT_BLOCKS[:js] = default_block_format % ['.js.rjs', 'JS'] 114 DEFAULT_BLOCKS[:xml] = default_block_format % ['.xml.builder', 'XML'] 110 default_block_format = <<-END 111 Proc.new { 112 @template.template_format = '%s' 113 render :action => "\#{action_name}", :content_type => Mime::%s 114 } 115 END 116 117 DEFAULT_BLOCKS = [:html, :js, :xml].inject({}) do |memo, ext| 118 default_block = default_block_format % [ext, ext.to_s.upcase] 119 memo.update(ext => default_block) 120 end 115 121 116 122 def initialize(block_binding) … … 133 139 if block_given? 134 140 @responses[mime_type] = Proc.new do 135 eval "response.content_type = '#{mime_type.to_s}'", @block_binding 141 eval <<-END, @block_binding 142 @template.template_format = '#{mime_type.to_sym}' 143 response.content_type = '#{mime_type.to_s}' 144 END 136 145 block.call 137 146 end trunk/actionpack/lib/action_controller/request.rb
r6451 r6517 9 9 # such as { 'RAILS_ENV' => 'production' }. 10 10 attr_reader :env 11 12 attr_accessor :format 11 13 12 14 # Returns the HTTP request method as a lowercase symbol (:get, for example). Note, HEAD is returned as :get … … 92 94 # GET /posts/5 | request.format => request.accepts.first (usually Mime::HTML for browsers) 93 95 def format 94 parameters[:format] ? Mime::Type.lookup_by_extension(parameters[:format]) : accepts.first96 @format ||= parameters[:format] ? Mime::Type.lookup_by_extension(parameters[:format]) : accepts.first 95 97 end 96 98 trunk/actionpack/lib/action_view/base.rb
r6509 r6517 159 159 attr_reader :logger, :response, :headers, :view_paths 160 160 attr_internal :cookies, :flash, :headers, :params, :request, :response, :session 161 162 attr_writer :template_format 161 163 162 164 # Specify trim mode for the ERB compiler. Defaults to '-'. … … 208 210 @@cached_template_extension = {} 209 211 212 # Order of template handers checked by #file_exists? depending on the current #template_format 213 DEFAULT_TEMPLATE_HANDLER_PREFERENCE = %w(erb rhtml builder rxml javascript delegate) 214 TEMPLATE_HANDLER_PREFERENCES = { 215 :js => %w(javascript erb rhtml builder rxml delegate), 216 :xml => %w(builder rxml erb rhtml javascript delegate), 217 :delegate => %w(delegate) 218 } 219 210 220 class ObjectWrapper < Struct.new(:value) #:nodoc: 211 221 end … … 230 240 # return the rendered template as a string. 231 241 def self.register_template_handler(extension, klass) 242 TEMPLATE_HANDLER_PREFERENCES[extension.to_sym] = TEMPLATE_HANDLER_PREFERENCES[:delegate] 232 243 @@template_handlers[extension] = klass 233 244 end … … 332 343 333 344 def pick_template_extension(template_path)#:nodoc: 334 formatted_template_path = "#{template_path}.#{template_format}"335 345 if @@cache_template_extensions 336 @@cached_template_extension[formatted_template_path] ||= find_template_extension_for(template_path, formatted_template_path) 346 formatted_template_path = "#{template_path}.#{template_format}" 347 @@cached_template_extension[formatted_template_path] ||= find_template_extension_for(template_path) 337 348 else 338 find_template_extension_for(template_path , formatted_template_path)349 find_template_extension_for(template_path) 339 350 end 340 351 end 341 352 342 353 def delegate_template_exists?(template_path)#:nodoc: 343 @@template_handlers.find { |k,| template_exists?(template_path, k) } 344 end 345 346 def one_of(template_path, *extensions)#:nodoc: 347 extensions.detect{|ext| template_exists?(template_path, ext)} 348 end 349 354 delegate = @@template_handlers.find { |k,| template_exists?(template_path, k) } 355 delegate && delegate.first.to_sym 356 end 357 350 358 def erb_template_exists?(template_path)#:nodoc: 351 one_of(template_path, :erb, :rhtml) 352 end 353 alias :rhtml_template_exists? :erb_template_exists? 354 359 template_exists?(template_path, :erb) && :erb 360 end 361 355 362 def builder_template_exists?(template_path)#:nodoc: 356 one_of(template_path, :builder, :rxml) 357 end 358 alias :rxml_template_exists? :builder_template_exists? 359 363 template_exists?(template_path, :builder) && :builder 364 end 365 366 def rhtml_template_exists?(template_path)#:nodoc: 367 template_exists?(template_path, :rhtml) && :rhtml 368 end 369 370 def rxml_template_exists?(template_path)#:nodoc: 371 template_exists?(template_path, :rxml) && :rxml 372 end 373 360 374 def javascript_template_exists?(template_path)#:nodoc: 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 375 template_exists?(template_path, :rjs) && :rjs 369 376 end 370 377 … … 372 379 template_file_name, template_file_extension = path_and_extension(template_path) 373 380 if template_file_extension 374 template_exists?(template_file_name, template_file_extension) 381 template_exists?(template_file_name, template_file_extension) && template_file_extension 375 382 else 376 383 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)384 return true if cached_template_extension(formatted_template_path) 385 template_handler_preferences.each do |template_type| 386 if extension = send("#{template_type}_template_exists?", formatted_template_path) 387 return "#{template_format}.#{extension}" 381 388 end 389 end 390 template_handler_preferences.each do |template_type| 391 if extension = send("#{template_type}_template_exists?", template_path) 392 return extension 393 end 394 end 395 nil 382 396 end 383 397 end … … 388 402 end 389 403 404 # symbolized version of the :format parameter of the request, or :html by default. 390 405 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 406 @template_format ||= controller.request.parameters[:format].to_sym rescue :html 407 end 408 409 def template_handler_preferences 410 TEMPLATE_HANDLER_PREFERENCES[template_format] || DEFAULT_TEMPLATE_HANDLER_PREFERENCE 399 411 end 400 412 … … 435 447 436 448 # Determines the template's file extension, such as rhtml, rxml, or rjs. 437 def find_template_extension_for(template_path, formatted_template_path = nil) 438 formatted_template_path ||= "#{template_path}.#{template_format}" 439 if match = delegate_template_exists?(template_path) 440 match.first.to_sym 441 elsif extension = formatted_template_exists?(formatted_template_path): "#{template_format}.#{extension}" 442 elsif extension = erb_template_exists?(template_path): extension 443 elsif extension = builder_template_exists?(template_path): extension 444 elsif javascript_template_exists?(template_path): :rjs 449 def find_template_extension_for(template_path) 450 if extension = file_exists?(template_path) 451 return extension 445 452 else 446 453 raise ActionViewError, "No erb, builder, rhtml, rxml, rjs or delegate template found for #{template_path} in #{@view_paths.inspect}" trunk/actionpack/test/controller/mime_responds_test.rb
r6507 r6517 289 289 end 290 290 291 def test_ all_types_with_layout291 def test_rjs_type_skips_layout 292 292 @request.env["HTTP_ACCEPT"] = "text/javascript" 293 293 get :all_types_with_layout 294 294 assert_equal 'RJS for all_types_with_layout', @response.body 295 295 end 296 297 def test_html_type_with_layout 296 298 @request.env["HTTP_ACCEPT"] = "text/html" 297 299 get :all_types_with_layout … … 344 346 @action = args.first[:action] 345 347 end 346 response.body = @action348 response.body = "#{@action} - #{@template.template_format}" 347 349 end 348 350 end 349 351 350 352 get :using_defaults 351 assert_equal "using_defaults ", @response.body353 assert_equal "using_defaults - html", @response.body 352 354 353 355 get :using_defaults, :format => "xml" 354 assert_equal "using_defaults .xml.builder", @response.body356 assert_equal "using_defaults - xml", @response.body 355 357 end 356 358 end trunk/actionpack/test/controller/render_test.rb
r6499 r6517 362 362 assert_equal '<test>passed formatted html erb</test>', @response.body 363 363 end 364 365 def test_should_render_formatted_html_erb_template_with_faulty_accepts_header 366 @request.env["HTTP_ACCEPT"] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*" 367 get :formatted_xml_erb 368 assert_equal '<test>passed formatted html erb</test>', @response.body 369 end 364 370 365 371 protected trunk/actionpack/test/controller/request_test.rb
r6340 r6517 310 310 end 311 311 312 def test_ format312 def test_xml_format 313 313 @request.instance_eval { @parameters = { :format => 'xml' } } 314 314 assert_equal Mime::XML, @request.format 315 315 end 316 317 def test_xhtml_format 316 318 @request.instance_eval { @parameters = { :format => 'xhtml' } } 317 319 assert_equal Mime::HTML, @request.format 318 320 end 321 322 def test_txt_format 319 323 @request.instance_eval { @parameters = { :format => 'txt' } } 320 324 assert_equal Mime::TEXT, @request.format 321 325 end 326 327 def test_nil_format 322 328 @request.instance_eval { @parameters = { :format => nil } } 323 329 @request.env["HTTP_ACCEPT"] = "text/javascript"