Changeset 7415
- Timestamp:
- 09/06/07 14:28:32 (1 year ago)
- Files:
-
- trunk/actionpack/lib/action_controller/base.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_controller/resources.rb (modified) (4 diffs)
- trunk/actionpack/lib/action_controller/routing.rb (modified) (8 diffs)
- trunk/actionpack/test/controller/resources_test.rb (modified) (9 diffs)
- trunk/actionpack/test/controller/routing_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_controller/base.rb
r7403 r7415 311 311 cattr_accessor :ignore_missing_templates 312 312 313 # Controls the resource action separator 314 @@resource_action_separator = "/" 315 cattr_accessor :resource_action_separator 316 313 317 # Holds the request object that's primarily used to get environment variables through access like 314 318 # <tt>request.env["REQUEST_URI"]</tt>. … … 606 610 self.class.controller_path 607 611 end 608 612 609 613 def session_enabled? 610 614 request.session_options && request.session_options[:disabled] != false 611 615 end 612 616 613 617 # View load paths for controller. 614 618 def view_paths trunk/actionpack/lib/action_controller/resources.rb
r7234 r7415 95 95 end 96 96 97 def action_separator 98 @action_separator ||= Base.resource_action_separator 99 end 97 100 98 101 protected … … 432 435 actions.each do |action| 433 436 action_options = action_options_for(action, resource, method) 434 435 # See http://dev.rubyonrails.org/ticket/8251 436 map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}", action_options) 437 map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}.:format", action_options) 437 map.named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}", action_options) 438 map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}.:format", action_options) 438 439 end 439 440 end … … 461 462 action_options = action_options_for(action, resource, method) 462 463 if action == :new 463 # See http://dev.rubyonrails.org/ticket/8251 464 map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options) 465 map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options) 464 map.named_route("new_#{resource.name_prefix}#{resource.singular}", resource.new_path, action_options) 465 map.named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}.:format", action_options) 466 466 else 467 # See http://dev.rubyonrails.org/ticket/8251 468 map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}", action_options) 469 map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}.:format", action_options) 467 map.named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}", action_options) 468 map.named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}.:format", action_options) 470 469 end 471 470 end … … 477 476 actions.each do |action| 478 477 action_options = action_options_for(action, resource, method) 479 # See http://dev.rubyonrails.org/ticket/8251 480 map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}", action_options) 481 map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}.:format",action_options) 478 map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}", action_options) 479 map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}.:format",action_options) 482 480 end 483 481 end trunk/actionpack/lib/action_controller/routing.rb
r7411 r7415 248 248 # 249 249 module Routing 250 SEPARATORS = %w( / .? )250 SEPARATORS = %w( / ; . , ? ) 251 251 252 252 HTTP_METHODS = [:get, :head, :post, :put, :delete] … … 549 549 550 550 class Segment #:nodoc: 551 RESERVED_PCHAR = ':@&=+$ ,;'551 RESERVED_PCHAR = ':@&=+$' 552 552 UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze 553 553 … … 562 562 nil 563 563 end 564 564 565 565 # Continue generating string for the prior segments. 566 566 def continue_string_structure(prior_segments) … … 1021 1021 end 1022 1022 1023 def deprecated_named_route(name, deprecated_name, path, options = {})1024 named_route(name, path, options)1025 @set.add_deprecated_named_route(name, deprecated_name, path, options) unless deprecated_name == name1026 end1027 1028 1023 # Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model. 1029 1024 # Example: … … 1058 1053 include Enumerable 1059 1054 1060 attr_reader :routes, :helpers , :deprecated_named_routes1055 attr_reader :routes, :helpers 1061 1056 1062 1057 def initialize … … 1067 1062 @routes = {} 1068 1063 @helpers = [] 1069 @deprecated_named_routes = {}1070 1064 1071 1065 @module ||= Module.new … … 1081 1075 1082 1076 def get(name) 1083 if @deprecated_named_routes.has_key?(name.to_sym)1084 ActiveSupport::Deprecation.warn(1085 "The named route \"#{name}\" uses a format that has been deprecated. " +1086 "You should use \"#{@deprecated_named_routes[name]}\" instead", caller1087 )1088 end1089 1077 routes[name.to_sym] 1090 1078 end … … 1260 1248 end 1261 1249 1262 def add_deprecated_named_route(name, deprecated_name, path, options = {})1263 add_named_route(deprecated_name, path, options)1264 named_routes.deprecated_named_routes[deprecated_name.to_sym] = name1265 end1266 1267 1250 def options_as_params(options) 1268 1251 # If an explicit :controller was given, always make :action explicit trunk/actionpack/test/controller/resources_test.rb
r7234 r7415 150 150 assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 151 151 actions.keys.each do |action| 152 assert_deprecated_named_route /thread_#{action}_messages/, "/threads/1/messages/#{action}", "thread_#{action}_messages_path", :action => action153 152 assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action 154 153 end … … 169 168 assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 170 169 actions.keys.each do |action| 171 assert_deprecated_named_route /formatted_thread_#{action}_messages/, "/threads/1/messages/#{action}.xml", "formatted_thread_#{action}_messages_path", :action => action, :format => 'xml'172 170 assert_named_route "/threads/1/messages/#{action}.xml", "formatted_#{action}_thread_messages_path", :action => action, :format => 'xml' 173 171 end … … 233 231 234 232 assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 235 assert_deprecated_named_route /thread_preview_new_message/, preview_path, :thread_preview_new_message_path, preview_options236 233 assert_named_route preview_path, :preview_new_thread_message_path, preview_options 237 234 end … … 248 245 249 246 assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 250 assert_deprecated_named_route /formatted_thread_preview_new_message/, preview_path, :formatted_thread_preview_new_message_path, preview_options251 247 assert_named_route preview_path, :formatted_preview_new_thread_message_path, preview_options 252 248 end … … 314 310 end 315 311 316 # NOTE from dchelimsky: http://dev.rubyonrails.org/ticket/8251 changes some of the 317 # named routes. In order to play nice, I didn't delete the old ones, which means 318 # that this test fails because until the old ones are deprecated and/or removed, there 319 # must be room for two names for the same route. 320 # 321 # From what I can tell this shouldn't matter - all the other 322 # tests in actionpack pass without this one passing. But I could be wrong ;) 323 # 324 # def test_restful_routes_dont_generate_duplicates 325 # with_restful_routing :messages do 326 # routes = ActionController::Routing::Routes.routes 327 # routes.each do |route| 328 # routes.each do |r| 329 # next if route === r # skip the comparison instance 330 # assert distinct_routes?(route, r), "Duplicate Route: #{route}" 331 # end 332 # end 333 # end 334 # end 312 def test_restful_routes_dont_generate_duplicates 313 with_restful_routing :messages do 314 routes = ActionController::Routing::Routes.routes 315 routes.each do |route| 316 routes.each do |r| 317 next if route === r # skip the comparison instance 318 assert distinct_routes?(route, r), "Duplicate Route: #{route}" 319 end 320 end 321 end 322 end 335 323 336 324 def test_should_create_singleton_resource_routes … … 472 460 end 473 461 462 def test_resource_action_separator 463 with_routing do |set| 464 set.draw do |map| 465 map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 466 map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 467 end 468 469 action_separator = ActionController::Base.resource_action_separator 470 471 assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 472 assert_named_route "/threads/1/messages#{action_separator}search", "search_thread_messages_path", {} 473 assert_named_route "/threads/1/messages/new", "new_thread_message_path", {} 474 assert_named_route "/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {} 475 assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 476 assert_named_route "/admin/account#{action_separator}login", "login_admin_account_path", {} 477 assert_named_route "/admin/account/new", "new_admin_account_path", {} 478 assert_named_route "/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {} 479 end 480 end 481 482 def test_new_style_named_routes_for_resource 483 with_routing do |set| 484 set.draw do |map| 485 map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 486 end 487 assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 488 assert_named_route "/threads/1/messages/search", "search_thread_messages_path", {} 489 assert_named_route "/threads/1/messages/new", "new_thread_message_path", {} 490 assert_named_route "/threads/1/messages/new/preview", "preview_new_thread_message_path", {} 491 end 492 end 493 494 def test_new_style_named_routes_for_singleton_resource 495 with_routing do |set| 496 set.draw do |map| 497 map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 498 end 499 assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 500 assert_named_route "/admin/account/login", "login_admin_account_path", {} 501 assert_named_route "/admin/account/new", "new_admin_account_path", {} 502 assert_named_route "/admin/account/new/preview", "preview_new_admin_account_path", {} 503 end 504 end 505 474 506 def test_resources_in_namespace 475 507 with_routing do |set| … … 625 657 assert_named_route "#{full_prefix}/1.xml", "formatted_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 626 658 627 assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}new_#{singular_name}/, "#{full_prefix}/new", "#{name_prefix}new_#{singular_name}_path", options[:options]628 assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}new_#{singular_name}/, "#{full_prefix}/new.xml", "formatted_#{name_prefix}new_#{singular_name}_path", options[:options].merge( :format => 'xml')629 assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}edit_#{singular_name}/, "#{full_prefix}/1/edit", "#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1')630 assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}edit_#{singular_name}/, "#{full_prefix}/1/edit.xml", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')631 632 659 assert_named_route "#{full_prefix}/new", "new_#{name_prefix}#{singular_name}_path", options[:options] 633 660 assert_named_route "#{full_prefix}/new.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge( :format => 'xml') … … 687 714 assert_named_route "#{full_path}.xml", "formatted_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml') 688 715 689 assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}new_#{singleton_name}/, "#{full_path}/new", "#{name_prefix}new_#{singleton_name}_path", options[:options]690 assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}new_#{singleton_name}/, "#{full_path}/new.xml", "formatted_#{name_prefix}new_#{singleton_name}_path", options[:options].merge(:format => 'xml')691 assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}edit_#{singleton_name}/, "#{full_path}/edit", "#{name_prefix}edit_#{singleton_name}_path", options[:options]692 assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}edit_#{singleton_name}/, "#{full_path}/edit.xml", "formatted_#{name_prefix}edit_#{singleton_name}_path", options[:options].merge(:format => 'xml')693 694 716 assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options[:options] 695 717 assert_named_route "#{full_path}/new.xml", "formatted_new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml') … … 703 725 end 704 726 705 def assert_deprecated_named_route(message, expected, route, options)706 assert_deprecated message do707 assert_named_route expected, route, options708 end709 end710 711 # These are only deprecated if they have a name_prefix.712 # See http://dev.rubyonrails.org/ticket/8251713 def assert_potentially_deprecated_named_route(name_prefix, message, expected, route, options)714 if name_prefix715 assert_deprecated_named_route(message, expected, route, options)716 else717 assert_named_route expected, route, options718 end719 end720 721 727 def assert_resource_methods(expected, resource, action_method, method) 722 728 assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}" trunk/actionpack/test/controller/routing_test.rb
r7411 r7415 23 23 end 24 24 25 safe, unsafe = %w(: @ & = + $ , ;), %w(^ / ? # [ ])25 safe, unsafe = %w(: @ & = + $), %w(^ / ? # [ ] , ;) 26 26 hex = unsafe.map { |char| '%' + char.unpack('H2').first.upcase } 27 27 … … 1025 1025 end 1026 1026 1027 class MapperDeprecatedRouteTest < Test::Unit::TestCase1028 def setup1029 @set = mock("set")1030 @mapper = ActionController::Routing::RouteSet::Mapper.new(@set)1031 end1032 1033 def test_should_add_new_and_deprecated_named_routes1034 @set.expects(:add_named_route).with("new", "path", {:a => "b"})1035 @set.expects(:add_deprecated_named_route).with("new", "old", "path", {:a => "b"})1036 @mapper.deprecated_named_route("new", "old", "path", {:a => "b"})1037 end1038 1039 def test_should_not_add_deprecated_named_route_if_both_are_the_same1040 @set.expects(:add_named_route).with("new", "path", {:a => "b"})1041 @set.expects(:add_deprecated_named_route).with("new", "new", "path", {:a => "b"}).never1042 @mapper.deprecated_named_route("new", "new", "path", {:a => "b"})1043 end1044 end1045 1046 class RouteSetDeprecatedRouteTest < Test::Unit::TestCase1047 def setup1048 @set = ActionController::Routing::RouteSet.new1049 end1050 1051 def test_should_add_deprecated_route1052 @set.expects(:add_named_route).with("old", "path", {:a => "b"})1053 @set.add_deprecated_named_route("new", "old", "path", {:a => "b"})1054 end1055 1056 def test_should_fire_deprecation_warning_on_access1057 @set.add_deprecated_named_route("new_outer_inner_path", "outer_new_inner_path", "/outers/:outer_id/inners/new", :controller => :inners)1058 ActiveSupport::Deprecation.expects(:warn)1059 @set.named_routes["outer_new_inner_path"]1060 end1061 end1062 1063 1027 end # uses_mocha 1064 1028 … … 1212 1176 1213 1177 assert_equal nil, builder.warn_output # should only warn on the :person segment 1214 end1215 1216 def test_comma_isnt_a_route_separator1217 segments = builder.segments_for_route_path '/books/:id,:action'1218 defaults = { :action => 'show' }1219 assert_raise(ArgumentError) do1220 builder.assign_route_options(segments, defaults, {})1221 end1222 end1223 1224 def test_semicolon_isnt_a_route_separator1225 segments = builder.segments_for_route_path '/books/:id;:action'1226 defaults = { :action => 'show' }1227 assert_raise(ArgumentError) do1228 builder.assign_route_options(segments, defaults, {})1229 end1230 1178 end 1231 1179