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

Ticket #6814: add_key_option_to_route_resources.diff

File add_key_option_to_route_resources.diff, 6.2 kB (added by tsaleh, 3 years ago)

Patch file

  • actionpack/test/controller/resources_test.rb

    old new  
    4343    end 
    4444  end 
    4545 
    46   def test_multile_with_path_prefix 
     46  def test_multiple_with_path_prefix 
    4747    with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do 
    4848      assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 
    4949      assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 
    5050    end 
    5151  end 
    5252 
     53  def test_multiple_with_key 
     54    with_restful_routing :messages, :comments, :key => :name do 
     55      assert_simply_restful_for :messages, :key => :name 
     56      assert_simply_restful_for :comments, :key => :name 
     57    end 
     58  end 
     59   
    5360  def test_with_collection_action 
    5461    rss_options = {:action => 'rss'} 
    5562    rss_path    = "/messages;rss" 
     
    107114    end 
    108115  end 
    109116 
    110  
    111117  def test_with_new_action 
    112118    with_restful_routing :messages, :new => { :preview => :post } do 
    113119      preview_options = {:action => 'preview'} 
     
    160166    end 
    161167  end 
    162168 
     169  def test_nested_restful_routes_with_key 
     170    with_routing do |set| 
     171      set.draw do |map| 
     172        map.resources :threads, :key => :thread_keyword do |map| 
     173          map.resources :messages, :key => :name do |map| 
     174            map.resources :comments, :key => :title 
     175          end 
     176        end 
     177      end 
     178 
     179      assert_simply_restful_for :threads, 
     180        :key => :thread_keyword 
     181      assert_simply_restful_for :messages, 
     182        :key => :name, 
     183        :path_prefix => 'threads/1/', 
     184        :options => { :thread_keyword => '1' } 
     185      assert_simply_restful_for :comments, 
     186        :key => :title, 
     187        :path_prefix => 'threads/1/messages/2/', 
     188        :options => { :thread_keyword => '1', :message_name => '2' } 
     189    end 
     190  end 
     191 
    163192  def test_restful_routes_dont_generate_duplicates 
    164193    with_restful_routing :messages do 
    165194      routes = ActionController::Routing::Routes.routes 
     
    192221      collection_path = "/#{options[:path_prefix]}#{controller_name}" 
    193222      member_path = "#{collection_path}/1" 
    194223      new_path = "#{collection_path}/new" 
     224      id_key = "#{options[:key] || :id}".to_sym 
    195225 
    196226      with_options(options[:options]) do |controller| 
    197227        controller.assert_routing collection_path,            :action => 'index' 
    198228        controller.assert_routing "#{collection_path}.xml" ,  :action => 'index', :format => 'xml' 
    199229        controller.assert_routing new_path,                   :action => 'new' 
    200         controller.assert_routing member_path,                :action => 'show', :id => '1' 
    201         controller.assert_routing "#{member_path};edit",      :action => 'edit', :id => '1' 
    202         controller.assert_routing "#{member_path}.xml",       :action => 'show', :id => '1', :format => 'xml' 
     230        controller.assert_routing member_path,                :action => 'show', id_key => '1' 
     231        controller.assert_routing "#{member_path};edit",      :action => 'edit', id_key => '1' 
     232        controller.assert_routing "#{member_path}.xml",       :action => 'show', id_key => '1', :format => 'xml' 
    203233      end 
    204234 
    205235      assert_recognizes( 
     
    207237        :path => collection_path, :method => :post) 
    208238 
    209239      assert_recognizes( 
    210         options[:options].merge(:action => 'update', :id => '1'), 
     240        options[:options].merge(:action => 'update', id_key => '1'), 
    211241        :path => member_path, :method => :put) 
    212242 
    213243      assert_recognizes( 
    214         options[:options].merge(:action => 'destroy', :id => '1'), 
     244        options[:options].merge(:action => 'destroy', id_key => '1'), 
    215245        :path => member_path, :method => :delete) 
    216246 
    217247      yield options[:options] if block_given? 
     
    225255      end 
    226256      singular_name ||= controller_name.to_s.singularize 
    227257      (options[:options] ||= {})[:controller] = controller_name.to_s 
     258      id_key = "#{options[:key] || :id}".to_sym 
    228259      @controller = "#{controller_name.to_s.camelize}Controller".constantize.new 
    229260      @request    = ActionController::TestRequest.new 
    230261      @response   = ActionController::TestResponse.new 
     
    236267      assert_named_route "#{full_prefix}",        "#{controller_name}_path",           options[:options] 
    237268      assert_named_route "#{full_prefix}.xml",    "formatted_#{controller_name}_path", options[:options].merge(:format => 'xml') 
    238269      assert_named_route "#{full_prefix}/new",    "new_#{singular_name}_path",         options[:options] 
    239       assert_named_route "#{full_prefix}/1",      "#{singular_name}_path",             options[:options].merge(:id => '1') 
    240       assert_named_route "#{full_prefix}/1;edit", "edit_#{singular_name}_path",        options[:options].merge(:id => '1') 
    241       assert_named_route "#{full_prefix}/1.xml",  "formatted_#{singular_name}_path",   options[:options].merge(:format => 'xml', :id => '1') 
     270      assert_named_route "#{full_prefix}/1",      "#{singular_name}_path",             options[:options].merge(id_key => '1') 
     271      assert_named_route "#{full_prefix}/1;edit", "edit_#{singular_name}_path",        options[:options].merge(id_key => '1') 
     272      assert_named_route "#{full_prefix}/1.xml",  "formatted_#{singular_name}_path",   options[:options].merge(:format => 'xml', id_key => '1') 
    242273      yield options[:options] if block_given? 
    243274    end 
    244275 
  • actionpack/lib/action_controller/resources.rb

    old new  
    99      def initialize(entities, options) 
    1010        @plural   = entities 
    1111        @singular = options[:singular] || plural.to_s.singularize 
     12        @key      = (options[:key] || "id").to_s 
    1213         
    1314        @options = options 
    1415 
     
    3031      end 
    3132       
    3233      def member_path 
    33         "#{path}/:id
     34        "#{path}/:#{@key}
    3435      end 
    3536       
    3637      def nesting_path_prefix 
    37         "#{path}/:#{singular}_id" 
     38        @nesting_path_prefix ||= ((@key =~ /^#{singular}/) ? "#{path}/:#{@key}" : "#{path}/:#{singular}_#{@key}") 
    3839      end 
    3940       
    4041      private