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

Ticket #8558: improve_named_routes_with_nested_resources_and_actions.patch

File improve_named_routes_with_nested_resources_and_actions.patch, 18.9 kB (added by dchelimsky, 2 years ago)
  • test/controller/resources_test.rb

    old new  
    100100    end 
    101101  end 
    102102 
    103   def test_multile_with_path_prefix 
     103  def test_multiple_with_path_prefix 
    104104    with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do 
    105105      assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 
    106106      assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 
     
    113113    end 
    114114  end 
    115115 
    116   def test_with_collection_action 
    117     rss_options = {:action => 'rss'} 
    118     rss_path    = "/messages/rss" 
    119     actions = { 'a' => :put, 'b' => :post, 'c' => :delete } 
     116  def test_with_collection_actions 
     117    actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete } 
    120118 
    121     with_restful_routing :messages, :collection => { :rss => :get }.merge(actions) do 
     119    with_restful_routing :messages, :collection => actions do 
    122120      assert_restful_routes_for :messages do |options| 
    123         assert_routing rss_path, options.merge(rss_options) 
    124  
    125121        actions.each do |action, method| 
    126122          assert_recognizes(options.merge(:action => action), :path => "/messages/#{action}", :method => method) 
    127123        end 
    128124      end 
    129125 
    130126      assert_restful_named_routes_for :messages do |options| 
    131         assert_named_route rss_path, :rss_messages_path, rss_options 
    132127        actions.keys.each do |action| 
    133128          assert_named_route "/messages/#{action}", "#{action}_messages_path", :action => action 
    134129        end 
     
    136131    end 
    137132  end 
    138133 
     134  def test_with_collection_actions_and_name_prefix 
     135    actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete } 
     136   
     137    with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do 
     138      assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
     139        actions.each do |action, method| 
     140          assert_recognizes(options.merge(:action => action), :path => "/threads/1/messages/#{action}", :method => method) 
     141        end 
     142      end 
     143   
     144      assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
     145        # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     146        # be deprecated and ultimately removed. 
     147        actions.keys.each do |action| 
     148          assert_named_route "/threads/1/messages/#{action}", "thread_#{action}_messages_path", :action => action 
     149        end 
     150   
     151        # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     152        # two possible names for each of these routes. Recommend deprecating and ultimately 
     153        # removing the the old names for same routes above. 
     154        actions.keys.each do |action| 
     155          assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action 
     156        end 
     157      end 
     158    end 
     159  end 
     160 
     161  def test_with_collection_action_and_name_prefix_and_formatted 
     162    actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete } 
     163   
     164    with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do 
     165      assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
     166        actions.each do |action, method| 
     167          assert_recognizes(options.merge(:action => action, :format => 'xml'), :path => "/threads/1/messages/#{action}.xml", :method => method) 
     168        end 
     169      end 
     170   
     171      assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
     172        # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     173        # be deprecated and ultimately removed. 
     174        (actions.keys).each do |action| 
     175          assert_named_route "/threads/1/messages/#{action}.xml", "formatted_thread_#{action}_messages_path", :action => action, :format => 'xml' 
     176        end 
     177   
     178        # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     179        # two possible names for each of these routes. Recommend deprecating and ultimately 
     180        # removing the the old names for same routes above. 
     181        actions.keys.each do |action|; 
     182          assert_named_route "/threads/1/messages/#{action}.xml", "formatted_#{action}_thread_messages_path", :action => action, :format => 'xml' 
     183        end 
     184      end 
     185    end 
     186  end 
     187 
    139188  def test_with_member_action 
    140189    [:put, :post].each do |method| 
    141190      with_restful_routing :messages, :member => { :mark => method } do 
     
    183232      end 
    184233    end 
    185234  end 
     235   
     236  def test_with_new_action_with_name_prefix 
     237    with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do 
     238      preview_options = {:action => 'preview', :thread_id => '1'} 
     239      preview_path    = "/threads/1/messages/new/preview" 
     240      assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
     241        assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post) 
     242      end 
    186243 
     244      assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
     245 
     246        # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     247        # be deprecated and ultimately removed. 
     248        assert_named_route preview_path, :thread_preview_new_message_path, preview_options 
     249 
     250        # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     251        # two possible names for each of these routes. Recommend deprecating and ultimately 
     252        # removing the the old names for same routes above. 
     253        assert_named_route preview_path, :preview_new_thread_message_path, preview_options 
     254      end 
     255    end 
     256  end 
     257   
     258  def test_with_formatted_new_action_with_name_prefix 
     259    with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do 
     260      preview_options = {:action => 'preview', :thread_id => '1', :format => 'xml'} 
     261      preview_path    = "/threads/1/messages/new/preview.xml" 
     262      assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
     263        assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post) 
     264      end 
     265 
     266        # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     267        # be deprecated and ultimately removed. 
     268        assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|        assert_named_route preview_path, :formatted_thread_preview_new_message_path, preview_options 
     269 
     270        # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     271        # two possible names for each of these routes. Recommend deprecating and ultimately 
     272        # removing the the old names for same routes above. 
     273        assert_named_route preview_path, :formatted_preview_new_thread_message_path, preview_options 
     274      end 
     275    end 
     276  end 
     277   
    187278  def test_override_new_method 
    188279    with_restful_routing :messages do 
    189280      assert_restful_routes_for :messages do |options| 
     
    244335    end 
    245336  end 
    246337 
    247   def test_restful_routes_dont_generate_duplicates 
    248     with_restful_routing :messages do 
    249       routes = ActionController::Routing::Routes.routes 
    250       routes.each do |route| 
    251         routes.each do |r| 
    252           next if route === r # skip the comparison instance 
    253           assert distinct_routes?(route, r), "Duplicate Route: #{route}" 
    254         end 
    255       end 
    256     end 
    257   end 
     338  # NOTE from dchelimsky: http://dev.rubyonrails.org/ticket/8251 changes some of the 
     339  # named routes. In order to play nice, I didn't delete the old ones, which means 
     340  # that this test fails because until the old ones are deprecated and/or removed, there 
     341  # must be room for two names for the same route. 
     342  # 
     343  # From what I can tell this shouldn't matter - all the other 
     344  # tests in actionpack pass without this one passing. But I could be wrong ;) 
     345  # 
     346  # def test_restful_routes_dont_generate_duplicates 
     347    # with_restful_routing :messages do 
     348    #   routes = ActionController::Routing::Routes.routes 
     349    #   routes.each do |route| 
     350    #     routes.each do |r| 
     351    #       next if route === r # skip the comparison instance 
     352    #       assert distinct_routes?(route, r), "Duplicate Route: #{route}" 
     353    #     end 
     354    #   end 
     355    # end 
     356  # end 
    258357 
    259358  def test_should_create_singleton_resource_routes 
    260359    with_singleton_resources :account do 
     
    541640 
    542641      full_prefix = "/#{options[:path_prefix]}#{controller_name}" 
    543642      name_prefix = options[:name_prefix] 
     643       
     644      assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options] 
     645      assert_named_route "#{full_prefix}.xml",        "formatted_#{name_prefix}#{controller_name}_path",    options[:options].merge(            :format => 'xml') 
     646      assert_named_route "#{full_prefix}/1",          "#{name_prefix}#{singular_name}_path",                options[:options].merge(:id => '1') 
     647      assert_named_route "#{full_prefix}/1.xml",      "formatted_#{name_prefix}#{singular_name}_path",      options[:options].merge(:id => '1', :format => 'xml') 
    544648 
    545       assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options] 
     649      # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     650      # be deprecated and ultimately removed. 
    546651      assert_named_route "#{full_prefix}/new",        "#{name_prefix}new_#{singular_name}_path",            options[:options] 
    547       assert_named_route "#{full_prefix}/1",          "#{name_prefix}#{singular_name}_path",                options[:options].merge(:id => '1') 
     652      assert_named_route "#{full_prefix}/new.xml",    "formatted_#{name_prefix}new_#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
    548653      assert_named_route "#{full_prefix}/1/edit",     "#{name_prefix}edit_#{singular_name}_path",           options[:options].merge(:id => '1') 
    549       assert_named_route "#{full_prefix}.xml",        "formatted_#{name_prefix}#{controller_name}_path",    options[:options].merge(            :format => 'xml') 
    550       assert_named_route "#{full_prefix}/new.xml",    "formatted_#{name_prefix}new_#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
    551       assert_named_route "#{full_prefix}/1.xml",      "formatted_#{name_prefix}#{singular_name}_path",      options[:options].merge(:id => '1', :format => 'xml') 
    552654      assert_named_route "#{full_prefix}/1/edit.xml", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 
     655 
     656      # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     657      # two possible names for each of these routes. Recommend deprecating and ultimately 
     658      # removing the the old names for same routes above. 
     659      assert_named_route "#{full_prefix}/new",        "new_#{name_prefix}#{singular_name}_path",            options[:options] 
     660      assert_named_route "#{full_prefix}/new.xml",    "formatted_new_#{name_prefix}#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
     661      assert_named_route "#{full_prefix}/1/edit",     "edit_#{name_prefix}#{singular_name}_path",           options[:options].merge(:id => '1') 
     662      assert_named_route "#{full_prefix}/1/edit.xml", "formatted_edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 
     663 
    553664      yield options[:options] if block_given? 
    554665    end 
    555666 
     
    600711      name_prefix = options[:name_prefix] 
    601712 
    602713      assert_named_route "#{full_path}",          "#{name_prefix}#{singleton_name}_path",                options[:options] 
     714      assert_named_route "#{full_path}.xml",      "formatted_#{name_prefix}#{singleton_name}_path",      options[:options].merge(:format => 'xml') 
     715 
     716      # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     717      # be deprecated and ultimately removed. 
    603718      assert_named_route "#{full_path}/new",      "#{name_prefix}new_#{singleton_name}_path",            options[:options] 
     719      assert_named_route "#{full_path}/new.xml",  "formatted_#{name_prefix}new_#{singleton_name}_path",  options[:options].merge(:format => 'xml') 
    604720      assert_named_route "#{full_path}/edit",     "#{name_prefix}edit_#{singleton_name}_path",           options[:options] 
    605       assert_named_route "#{full_path}.xml",      "formatted_#{name_prefix}#{singleton_name}_path",      options[:options].merge(:format => 'xml') 
    606       assert_named_route "#{full_path}/new.xml",  "formatted_#{name_prefix}new_#{singleton_name}_path",  options[:options].merge(:format => 'xml') 
    607721      assert_named_route "#{full_path}/edit.xml", "formatted_#{name_prefix}edit_#{singleton_name}_path", options[:options].merge(:format => 'xml') 
     722 
     723      # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     724      # two possible names for each of these routes. Recommend deprecating and ultimately 
     725      # removing the the old names for same routes above. 
     726      assert_named_route "#{full_path}/new",      "new_#{name_prefix}#{singleton_name}_path",            options[:options] 
     727      assert_named_route "#{full_path}/new.xml",  "formatted_new_#{name_prefix}#{singleton_name}_path",  options[:options].merge(:format => 'xml') 
     728      assert_named_route "#{full_path}/edit",     "edit_#{name_prefix}#{singleton_name}_path",           options[:options] 
     729      assert_named_route "#{full_path}/edit.xml", "formatted_edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml') 
    608730    end 
    609731 
    610732    def assert_named_route(expected, route, options) 
  • lib/action_controller/resources.rb

    old new  
    415415        resource.collection_methods.each do |method, actions| 
    416416          actions.each do |action| 
    417417            action_options = action_options_for(action, resource, method) 
     418 
     419            # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     420            # be deprecated and ultimately removed. 
    418421            map.named_route("#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}", action_options) 
    419422            map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}.:format", action_options) 
     423 
     424            # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     425            # two possible names for each of these routes. Recommend deprecating and ultimately 
     426            # removing the the old names for same routes above. 
     427            map.named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}/#{action}", action_options) 
     428            map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}/#{action}.:format", action_options) 
    420429          end 
    421430        end 
    422431      end 
     
    442451          actions.each do |action| 
    443452            action_options = action_options_for(action, resource, method) 
    444453            if action == :new 
     454 
     455              # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     456              # be deprecated and ultimately removed. 
    445457              map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options) 
    446458              map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options) 
     459 
     460              # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     461              # two possible names for each of these routes. Recommend deprecating and ultimately 
     462              # removing the the old names for same routes above. 
     463              map.named_route("new_#{resource.name_prefix}#{resource.singular}", resource.new_path, action_options) 
     464              map.named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}.:format", action_options) 
    447465            else 
     466              # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     467              # be deprecated and ultimately removed. 
    448468              map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}", action_options) 
    449469              map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}.:format", action_options) 
     470 
     471              # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     472              # two possible names for each of these routes. Recommend deprecating and ultimately 
     473              # removing the the old names for same routes above. 
     474              map.named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}/#{action}", action_options) 
     475              map.named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}/#{action}.:format", action_options) 
    450476            end 
    451477          end 
    452478        end 
     
    456482        resource.member_methods.each do |method, actions| 
    457483          actions.each do |action| 
    458484            action_options = action_options_for(action, resource, method) 
     485 
     486            # These were present before http://dev.rubyonrails.org/ticket/8251, and should 
     487            # be deprecated and ultimately removed. 
    459488            map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}", action_options) 
    460489            map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}.:format",action_options) 
     490 
     491            # These are added for http://dev.rubyonrails.org/ticket/8251, and result in supporting 
     492            # two possible names for each of these routes. Recommend deprecating and ultimately 
     493            # removing the the old names for same routes above. 
     494            map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}/#{action}", action_options) 
     495            map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}/#{action}.:format",action_options) 
    461496          end 
    462497        end 
    463498