Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10578: patch-resources-path-replace.diff

File patch-resources-path-replace.diff, 5.7 kB (added by blj, 1 year ago)

Patch to actionpack/resources

  • actionpack/test/controller/resources_test.rb

    old new  
    609609    end 
    610610  end 
    611611 
     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   
    612641  protected 
    613642    def with_restful_routing(*args) 
    614643      with_routing do |set| 
     
    639668      options[:options] ||= {} 
    640669      options[:options][:controller] = options[:controller] || controller_name.to_s 
    641670 
    642       collection_path            = "/#{options[:path_prefix]}#{controller_name}" 
     671      collection_path            = "/#{options[:path_prefix]}#{options[:path_replace] || controller_name}" 
    643672      member_path                = "#{collection_path}/1" 
    644673      new_path                   = "#{collection_path}/new" 
    645674      edit_member_path           = "#{member_path}/edit" 
     
    692721      get :index, options[:options] 
    693722      options[:options].delete :action 
    694723 
    695       full_prefix = "/#{options[:path_prefix]}#{controller_name}" 
     724      full_prefix = "/#{options[:path_prefix]}#{options[:path_replace] || controller_name}" 
    696725      name_prefix = options[:name_prefix] 
    697726       
    698727      assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options] 
     
    712741      options[:options] ||= {} 
    713742      options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize 
    714743 
    715       full_path           = "/#{options[:path_prefix]}#{singleton_name}" 
     744      full_path           = "/#{options[:path_prefix]}#{options[:path_replace] || singleton_name}" 
    716745      new_path            = "#{full_path}/new" 
    717746      edit_path           = "#{full_path}/edit" 
    718747      formatted_edit_path = "#{full_path}/edit.xml" 
     
    751780      get :show, options[:options] 
    752781      options[:options].delete :action 
    753782 
    754       full_path = "/#{options[:path_prefix]}#{singleton_name}" 
     783      full_path = "/#{options[:path_prefix]}#{options[:path_replace] || singleton_name}" 
    755784      name_prefix = options[:name_prefix] 
    756785 
    757786      assert_named_route "#{full_path}",          "#{name_prefix}#{singleton_name}_path",                options[:options] 
  • actionpack/lib/action_controller/resources.rb

    old new  
    4444  module Resources 
    4545    class Resource #:nodoc: 
    4646      attr_reader :collection_methods, :member_methods, :new_methods 
    47       attr_reader :path_prefix, :name_prefix 
     47      attr_reader :path_prefix, :name_prefix, :path_replace 
    4848      attr_reader :plural, :singular 
    4949      attr_reader :options 
    5050 
     
    5757        arrange_actions 
    5858        add_default_actions 
    5959        set_prefixes 
     60        @path_replace = options.delete(:path_replace) 
    6061      end 
    6162 
    6263      def controller 
     
    7576      end 
    7677 
    7778      def path 
    78         @path ||= "#{path_prefix}/#{plural}" 
     79        @path ||= "#{path_prefix}/#{path_replace || plural}" 
    7980      end 
    8081 
    8182      def new_path 
     
    236237    # * <tt>:singular</tt> - specify the singular name used in the member routes. 
    237238    # * <tt>:requirements</tt> - set custom routing parameter requirements. 
    238239    # * <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    #  
    239256    # * <tt>:path_prefix</tt> - set a prefix to the routes with required route variables. 
    240257    # 
    241258    #   Weblog comments usually belong to a post, so you might use resources like: