Changeset 7414
- Timestamp:
- 09/06/07 14:26:52 (1 year ago)
- Files:
-
- branches/1-2-stable/actionpack/lib/action_controller/base.rb (modified) (1 diff)
- branches/1-2-stable/actionpack/lib/action_controller/resources.rb (modified) (4 diffs)
- branches/1-2-stable/actionpack/lib/action_controller/routing.rb (modified) (4 diffs)
- branches/1-2-stable/actionpack/test/controller/resources_test.rb (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1-2-stable/actionpack/lib/action_controller/base.rb
r6803 r7414 293 293 cattr_accessor :ignore_missing_templates 294 294 295 # Controls the resource action separator 296 @@resource_action_separator = ";" 297 cattr_accessor :resource_action_separator 298 295 299 # Holds the request object that's primarily used to get environment variables through access like 296 300 # <tt>request.env["REQUEST_URI"]</tt>. branches/1-2-stable/actionpack/lib/action_controller/resources.rb
r6114 r7414 17 17 set_prefixes 18 18 end 19 19 20 20 def controller 21 21 @controller ||= (options[:controller] || plural).to_s 22 22 end 23 23 24 24 def path 25 25 @path ||= "#{path_prefix}/#{plural}" 26 26 end 27 27 28 28 def new_path 29 29 @new_path ||= "#{path}/new" 30 30 end 31 31 32 32 def member_path 33 33 @member_path ||= "#{path}/:id" 34 34 end 35 35 36 36 def nesting_path_prefix 37 37 @nesting_path_prefix ||= "#{path}/:#{singular}_id" 38 38 end 39 39 40 def action_separator 41 @action_separator ||= Base.resource_action_separator 42 end 43 40 44 protected 41 45 def arrange_actions … … 325 329 actions.each do |action| 326 330 action_options = action_options_for(action, resource, method) 327 map.named_route("#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path};#{action}", action_options) 328 map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}.:format;#{action}", action_options) 331 332 unless resource.name_prefix.blank? 333 map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.name_prefix}#{action}_#{resource.plural}") 334 map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.name_prefix}#{action}_#{resource.plural}") 335 end 336 337 map.named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}", action_options) 338 map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}.:format", action_options) 329 339 end 330 340 end … … 352 362 action_options = action_options_for(action, resource, method) 353 363 if action == :new 354 map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options) 355 map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options) 364 365 unless resource.name_prefix.blank? 366 map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}new_#{resource.singular}") 367 map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}new_#{resource.singular}") 368 end 369 370 map.named_route("new_#{resource.name_prefix}#{resource.singular}", resource.new_path, action_options) 371 map.named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}.:format", action_options) 372 356 373 else 357 map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", action_options) 358 map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", action_options) 374 375 unless resource.name_prefix.blank? 376 map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_new_#{resource.singular}") 377 map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}") 378 end 379 380 map.named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}", action_options) 381 map.named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}.:format", action_options) 382 359 383 end 360 384 end … … 366 390 actions.each do |action| 367 391 action_options = action_options_for(action, resource, method) 368 map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path};#{action}", action_options) 369 map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}.:format;#{action}",action_options) 392 393 unless resource.name_prefix.blank? 394 map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_#{resource.singular}") 395 map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_#{resource.singular}") 396 end 397 398 map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}", action_options) 399 map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}.:format", action_options) 400 370 401 end 371 402 end branches/1-2-stable/actionpack/lib/action_controller/routing.rb
r6390 r7414 990 990 @set.add_named_route(name, path, options) 991 991 end 992 993 def deprecated_named_route(name, deprecated_name, options = {}) 994 @set.add_deprecated_named_route(name, deprecated_name) 995 end 992 996 993 997 # Added deprecation notice for anyone who already added a named route called "root". … … 1020 1024 @routes = {} 1021 1025 @helpers = [] 1022 1026 1023 1027 @module ||= Module.new 1024 1028 @module.instance_methods.each do |selector| … … 1055 1059 def install(destinations = [ActionController::Base, ActionView::Base]) 1056 1060 Array(destinations).each { |dest| dest.send :include, @module } 1061 end 1062 1063 def define_deprecated_named_route_methods(name, deprecated_name) 1064 1065 [:url, :path].each do |kind| 1066 @module.send :module_eval, <<-end_eval # We use module_eval to avoid leaks 1067 1068 def #{url_helper_name(deprecated_name, kind)}(*args) 1069 1070 ActiveSupport::Deprecation.warn( 1071 'The named route "#{url_helper_name(deprecated_name, kind)}" uses a format that has been deprecated. ' + 1072 'You should use "#{url_helper_name(name, kind)}" instead.', caller 1073 ) 1074 1075 send :#{url_helper_name(name, kind)}, *args 1076 1077 end 1078 1079 def #{hash_access_name(deprecated_name, kind)}(*args) 1080 1081 ActiveSupport::Deprecation.warn( 1082 'The named route "#{hash_access_name(deprecated_name, kind)}" uses a format that has been deprecated. ' + 1083 'You should use "#{hash_access_name(name, kind)}" instead.', caller 1084 ) 1085 1086 send :#{hash_access_name(name, kind)}, *args 1087 1088 end 1089 1090 end_eval 1091 end 1092 1057 1093 end 1058 1094 … … 1178 1214 named_routes[name] = add_route(path, options) 1179 1215 end 1216 1217 def add_deprecated_named_route(name, deprecated_name) 1218 named_routes.define_deprecated_named_route_methods(name, deprecated_name) 1219 end 1180 1220 1181 1221 def options_as_params(options) branches/1-2-stable/actionpack/test/controller/resources_test.rb
r6107 r7414 64 64 end 65 65 66 def test_multi le_with_path_prefix66 def test_multiple_with_path_prefix 67 67 with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do 68 68 assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } … … 313 313 end 314 314 315 def test_resource_action_separator 316 with_routing do |set| 317 set.draw do |map| 318 map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 319 map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 320 end 321 322 action_separator = ActionController::Base.resource_action_separator 323 324 assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 325 assert_named_route "/threads/1/messages#{action_separator}search", "search_thread_messages_path", {} 326 assert_named_route "/threads/1/messages/new", "new_thread_message_path", {} 327 assert_named_route "/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {} 328 assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 329 assert_named_route "/admin/account#{action_separator}login", "login_admin_account_path", {} 330 assert_named_route "/admin/account/new", "new_admin_account_path", {} 331 assert_named_route "/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {} 332 end 333 end 334 335 def test_new_style_named_routes_for_resource 336 with_routing do |set| 337 set.draw do |map| 338 map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 339 end 340 assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 341 assert_named_route "/threads/1/messages;search", "search_thread_messages_path", {} 342 assert_named_route "/threads/1/messages/new", "new_thread_message_path", {} 343 assert_named_route "/threads/1/messages/new;preview", "preview_new_thread_message_path", {} 344 end 345 end 346 347 def test_new_style_named_routes_for_singleton_resource 348 with_routing do |set| 349 set.draw do |map| 350 map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 351 end 352 assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 353 assert_named_route "/admin/account;login", "login_admin_account_path", {} 354 assert_named_route "/admin/account/new", "new_admin_account_path", {} 355 assert_named_route "/admin/account/new;preview", "preview_new_admin_account_path", {} 356 end 357 end 358 359 def test_should_add_deprecated_named_routes_for_resource 360 with_routing do |set| 361 set.draw do |map| 362 map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 363 end 364 assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 365 assert_deprecated do 366 assert_named_route "/threads/1/messages;search", "thread_search_messages_path", {} 367 assert_named_route "/threads/1/messages/new", "thread_new_message_path", {} 368 assert_named_route "/threads/1/messages/new;preview", "thread_preview_new_message_path", {} 369 end 370 end 371 end 372 373 def test_should_add_deprecated_named_routes_for_singleton_resource 374 with_routing do |set| 375 set.draw do |map| 376 map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 377 end 378 assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 379 assert_deprecated do 380 assert_named_route "/admin/account;login", "admin_login_account_path", {} 381 assert_named_route "/admin/account/new", "admin_new_account_path", {} 382 assert_named_route "/admin/account/new;preview", "admin_preview_new_account_path", {} 383 end 384 end 385 end 386 315 387 protected 316 388 def with_restful_routing(*args) … … 346 418 new_path = "#{collection_path}/new" 347 419 edit_member_path = "#{member_path};edit" 348 formatted_edit_member_path = "#{member_path} .xml;edit"420 formatted_edit_member_path = "#{member_path};edit.xml" 349 421 350 422 with_options(options[:options]) do |controller| … … 396 468 397 469 assert_named_route "#{full_prefix}", "#{name_prefix}#{controller_name}_path", options[:options] 398 assert_named_route "#{full_prefix}/new", " #{name_prefix}new_#{singular_name}_path", options[:options]470 assert_named_route "#{full_prefix}/new", "new_#{name_prefix}#{singular_name}_path", options[:options] 399 471 assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1') 400 assert_named_route "#{full_prefix}/1;edit", " #{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1')472 assert_named_route "#{full_prefix}/1;edit", "edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1') 401 473 assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge( :format => 'xml') 402 assert_named_route "#{full_prefix}/new.xml", "formatted_ #{name_prefix}new_#{singular_name}_path", options[:options].merge( :format => 'xml')474 assert_named_route "#{full_prefix}/new.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge( :format => 'xml') 403 475 assert_named_route "#{full_prefix}/1.xml", "formatted_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 404 assert_named_route "#{full_prefix}/1 .xml;edit", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')476 assert_named_route "#{full_prefix}/1;edit.xml", "formatted_edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 405 477 yield options[:options] if block_given? 406 478 end … … 412 484 new_path = "#{full_path}/new" 413 485 edit_path = "#{full_path};edit" 414 formatted_edit_path = "#{full_path} .xml;edit"486 formatted_edit_path = "#{full_path};edit.xml" 415 487 416 488 with_options options[:options] do |controller| … … 449 521 450 522 full_path = "/#{options[:path_prefix]}#{singleton_name}" 451 452 assert_named_route "#{full_path}", "#{singleton_name}_path", options[:options] 453 assert_named_route "#{full_path}/new", "new_#{singleton_name}_path", options[:options] 454 assert_named_route "#{full_path};edit", "edit_#{singleton_name}_path", options[:options] 455 assert_named_route "#{full_path}.xml", "formatted_#{singleton_name}_path", options[:options].merge(:format => 'xml') 456 assert_named_route "#{full_path}/new.xml", "formatted_new_#{singleton_name}_path", options[:options].merge(:format => 'xml') 457 assert_named_route "#{full_path}.xml;edit", "formatted_edit_#{singleton_name}_path", options[:options].merge(:format => 'xml') 523 full_name = "#{options[:name_prefix]}#{singleton_name}" 524 525 assert_named_route "#{full_path}", "#{full_name}_path", options[:options] 526 assert_named_route "#{full_path}/new", "new_#{full_name}_path", options[:options] 527 assert_named_route "#{full_path};edit", "edit_#{full_name}_path", options[:options] 528 assert_named_route "#{full_path}.xml", "formatted_#{full_name}_path", options[:options].merge(:format => 'xml') 529 assert_named_route "#{full_path}/new.xml", "formatted_new_#{full_name}_path", options[:options].merge(:format => 'xml') 530 assert_named_route "#{full_path};edit.xml", "formatted_edit_#{full_name}_path", options[:options].merge(:format => 'xml') 458 531 end 459 532