Ticket #10578: patch-resources-path-replace.diff
| File patch-resources-path-replace.diff, 5.7 kB (added by blj, 1 year ago) |
|---|
-
actionpack/test/controller/resources_test.rb
old new 609 609 end 610 610 end 611 611 612 def test_with_path_replace 613 with_restful_routing :messages, :path_replace => 'reviews' do 614 assert_simply_restful_for :messages, :path_replace => 'reviews' 615 end 616 end 617 618 def test_multiple_with_path_replace_and_controller 619 with_routing do |set| 620 set.draw do |map| 621 map.resources :products do |products| 622 products.resources :product_reviews, :path_replace=>'reviews', :controller=>'messages' 623 end 624 map.resources :tutors do |tutors| 625 tutors.resources :tutor_reviews, :path_replace=>'reviews', :controller=>'comments' 626 end 627 end 628 629 assert_simply_restful_for :product_reviews, :controller=>'messages', :path_replace => 'reviews', :name_prefix => 'product_', :path_prefix => 'products/1/', :options => {:product_id => '1'} 630 assert_simply_restful_for :tutor_reviews,:controller=>'comments', :path_replace => 'reviews', :name_prefix => 'tutor_', :path_prefix => 'tutors/1/', :options => {:tutor_id => '1'} 631 end 632 end 633 634 def test_with_path_replace_path_prefix_requirements 635 expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'} 636 with_restful_routing :messages, :path_replace=>'comments',:path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do 637 assert_recognizes(expected_options, :path => 'thread/1.1.1/comments/1', :method => :get) 638 end 639 end 640 612 641 protected 613 642 def with_restful_routing(*args) 614 643 with_routing do |set| … … 639 668 options[:options] ||= {} 640 669 options[:options][:controller] = options[:controller] || controller_name.to_s 641 670 642 collection_path = "/#{options[:path_prefix]}#{ controller_name}"671 collection_path = "/#{options[:path_prefix]}#{options[:path_replace] || controller_name}" 643 672 member_path = "#{collection_path}/1" 644 673 new_path = "#{collection_path}/new" 645 674 edit_member_path = "#{member_path}/edit" … … 692 721 get :index, options[:options] 693 722 options[:options].delete :action 694 723 695 full_prefix = "/#{options[:path_prefix]}#{ controller_name}"724 full_prefix = "/#{options[:path_prefix]}#{options[:path_replace] || controller_name}" 696 725 name_prefix = options[:name_prefix] 697 726 698 727 assert_named_route "#{full_prefix}", "#{name_prefix}#{controller_name}_path", options[:options] … … 712 741 options[:options] ||= {} 713 742 options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize 714 743 715 full_path = "/#{options[:path_prefix]}#{ singleton_name}"744 full_path = "/#{options[:path_prefix]}#{options[:path_replace] || singleton_name}" 716 745 new_path = "#{full_path}/new" 717 746 edit_path = "#{full_path}/edit" 718 747 formatted_edit_path = "#{full_path}/edit.xml" … … 751 780 get :show, options[:options] 752 781 options[:options].delete :action 753 782 754 full_path = "/#{options[:path_prefix]}#{ singleton_name}"783 full_path = "/#{options[:path_prefix]}#{options[:path_replace] || singleton_name}" 755 784 name_prefix = options[:name_prefix] 756 785 757 786 assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options] -
actionpack/lib/action_controller/resources.rb
old new 44 44 module Resources 45 45 class Resource #:nodoc: 46 46 attr_reader :collection_methods, :member_methods, :new_methods 47 attr_reader :path_prefix, :name_prefix 47 attr_reader :path_prefix, :name_prefix, :path_replace 48 48 attr_reader :plural, :singular 49 49 attr_reader :options 50 50 … … 57 57 arrange_actions 58 58 add_default_actions 59 59 set_prefixes 60 @path_replace = options.delete(:path_replace) 60 61 end 61 62 62 63 def controller … … 75 76 end 76 77 77 78 def path 78 @path ||= "#{path_prefix}/#{p lural}"79 @path ||= "#{path_prefix}/#{path_replace || plural}" 79 80 end 80 81 81 82 def new_path … … 236 237 # * <tt>:singular</tt> - specify the singular name used in the member routes. 237 238 # * <tt>:requirements</tt> - set custom routing parameter requirements. 238 239 # * <tt>:conditions</tt> - specify custom routing recognition conditions. Resources sets the :method value for the method-specific routes. 240 # * <tt>:path_replace</tt> - specify custom path that will replace the default path based on the resource name 241 # 242 # Use this if the path generated by the resources needs to be changed, for e.g.: 243 # 244 # map.resources :products, :path_replace=>'productos' do |product| 245 # product.resources :product_reviews, :path_replace => 'comentarios' 246 # end 247 # map.resources :tutors, :path_replace=>'tutores' do |tutor| 248 # product.resources :tutor_reviews, :path_replace => 'comentarios' 249 # end 250 # 251 # These will yield paths like the following: 252 # 253 # productos/:product_id/comentarios 254 # tutores/:tutor_id/comentarios 255 # 239 256 # * <tt>:path_prefix</tt> - set a prefix to the routes with required route variables. 240 257 # 241 258 # Weblog comments usually belong to a post, so you might use resources like: