Ticket #7454: disambiguate_url_generators_for_uncountable_resources.patch
| File disambiguate_url_generators_for_uncountable_resources.patch, 6.3 kB (added by jkit, 2 years ago) |
|---|
-
test/controller/resources_test.rb
old new 9 9 class ThreadsController < ResourcesController; end 10 10 class MessagesController < ResourcesController; end 11 11 class CommentsController < ResourcesController; end 12 class SheepController < ResourcesController; end 12 13 13 14 class AccountController < ResourcesController; end 14 15 class AdminController < ResourcesController; end … … 51 52 end 52 53 53 54 def test_multiple_default_restful_routes 54 with_restful_routing :messages, :comments do55 with_restful_routing :messages, :comments, :sheep do 55 56 assert_simply_restful_for :messages 56 57 assert_simply_restful_for :comments 58 assert_simply_restful_for :sheep 57 59 end 58 60 end 59 61 … … 391 393 get :index, options[:options] 392 394 options[:options].delete :action 393 395 396 collection_name = controller_name.to_s + 397 if Inflector.inflections.uncountables.include?(controller_name.to_s) 398 ActionController::Resources::Resource::UNCOUNTABLE_COLLECTION_SUFFIX 399 else 400 "" 401 end 402 394 403 full_prefix = "/#{options[:path_prefix]}#{controller_name}" 395 404 name_prefix = options[:name_prefix] 396 405 397 assert_named_route "#{full_prefix}", "#{name_prefix}#{co ntroller_name}_path", options[:options]406 assert_named_route "#{full_prefix}", "#{name_prefix}#{collection_name}_path", options[:options] 398 407 assert_named_route "#{full_prefix}/new", "#{name_prefix}new_#{singular_name}_path", options[:options] 399 408 assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1') 400 409 assert_named_route "#{full_prefix}/1;edit", "#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1') 401 assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{co ntroller_name}_path", options[:options].merge( :format => 'xml')410 assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{collection_name}_path", options[:options].merge( :format => 'xml') 402 411 assert_named_route "#{full_prefix}/new.xml", "formatted_#{name_prefix}new_#{singular_name}_path", options[:options].merge( :format => 'xml') 403 412 assert_named_route "#{full_prefix}/1.xml", "formatted_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 404 413 assert_named_route "#{full_prefix}/1.xml;edit", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') -
lib/action_controller/resources.rb
old new 1 1 module ActionController 2 2 module Resources 3 3 class Resource #:nodoc: 4 5 UNCOUNTABLE_COLLECTION_SUFFIX = "_collection" 6 4 7 attr_reader :collection_methods, :member_methods, :new_methods 5 8 attr_reader :path_prefix, :name_prefix 6 9 attr_reader :plural, :singular … … 36 39 def nesting_path_prefix 37 40 @nesting_path_prefix ||= "#{path}/:#{singular}_id" 38 41 end 42 43 def collection_name 44 if @plural.to_s == @singular.to_s # uncountable 45 "#{@plural}#{UNCOUNTABLE_COLLECTION_SUFFIX}" 46 else 47 "#{@plural}" 48 end 49 end 39 50 40 51 protected 41 52 def arrange_actions … … 141 152 # edit_message edit_message_url(id), hash_for_edit_message_url(id), 142 153 # edit_message_path(id), hash_for_edit_message_path(id) 143 154 # 155 # Note that for resources with names that are uncountable, there must be a way to disambiguate 156 # a call to the collection url generators and the member url generators. Take for example the 157 # resource "sheep". Without disambiguation, <tt>sheep_url()</tt> can only refer to either the 158 # collection url generator, or the member url generator, leaving the other without a method to 159 # name. To resolve this, we make uncountable resources a special case, and append the 160 # resource name with ActiveController::Resources::Resource::UNCOUNTABLE_COLLECTION_SUFFIX 161 # (which by default is "_collection"). This results in the method <tt>sheep_collection_url</tt> 162 # as the generator for the collection, and <tt>sheep_url</tt> as the generator for the member. 163 # 144 164 # You can use these helpers instead of #url_for or methods that take #url_for parameters: 145 165 # 146 166 # redirect_to :controller => 'messages', :action => 'index' … … 324 344 resource.collection_methods.each do |method, actions| 325 345 actions.each do |action| 326 346 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)347 map.named_route("#{resource.name_prefix}#{action}_#{resource.collection_name}", "#{resource.path};#{action}", action_options) 348 map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.collection_name}", "#{resource.path}.:format;#{action}", action_options) 329 349 end 330 350 end 331 351 end 332 352 333 353 def map_default_collection_actions(map, resource) 334 354 index_action_options = action_options_for("index", resource) 335 map.named_route("#{resource.name_prefix}#{resource. plural}", resource.path, index_action_options)336 map.named_route("formatted_#{resource.name_prefix}#{resource. plural}", "#{resource.path}.:format", index_action_options)355 map.named_route("#{resource.name_prefix}#{resource.collection_name}", resource.path, index_action_options) 356 map.named_route("formatted_#{resource.name_prefix}#{resource.collection_name}", "#{resource.path}.:format", index_action_options) 337 357 338 358 create_action_options = action_options_for("create", resource) 339 359 map.connect(resource.path, create_action_options)