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

Ticket #8558: fix_resource_route_names.edge.diff

File fix_resource_route_names.edge.diff, 21.3 kB (added by pixeltrix, 1 year ago)
  • actionpack/test/controller/routing_test.rb

    old new  
    2222      map.connect ':controller/:action/:variable' 
    2323    end 
    2424 
    25     safe, unsafe = %w(: @ & = + $ , ;), %w(^ / ? # [ ]
     25    safe, unsafe = %w(: @ & = + $), %w(^ / ? # [ ] , ;
    2626    hex = unsafe.map { |char| '%' + char.unpack('H2').first.upcase } 
    2727 
    2828    @segment = "#{safe}#{unsafe}".freeze 
     
    10241024    end 
    10251025end 
    10261026 
    1027 class MapperDeprecatedRouteTest < Test::Unit::TestCase 
    1028   def setup 
    1029     @set = mock("set") 
    1030     @mapper = ActionController::Routing::RouteSet::Mapper.new(@set)     
    1031   end 
    1032    
    1033   def test_should_add_new_and_deprecated_named_routes 
    1034     @set.expects(:add_named_route).with("new", "path", {:a => "b"}) 
    1035     @set.expects(:add_deprecated_named_route).with("new", "old", "path", {:a => "b"}) 
    1036     @mapper.deprecated_named_route("new", "old", "path", {:a => "b"}) 
    1037   end 
    1038    
    1039   def test_should_not_add_deprecated_named_route_if_both_are_the_same 
    1040     @set.expects(:add_named_route).with("new", "path", {:a => "b"}) 
    1041     @set.expects(:add_deprecated_named_route).with("new", "new", "path", {:a => "b"}).never 
    1042     @mapper.deprecated_named_route("new", "new", "path", {:a => "b"}) 
    1043   end 
    1044 end 
    1045  
    1046 class RouteSetDeprecatedRouteTest < Test::Unit::TestCase 
    1047   def setup 
    1048     @set = ActionController::Routing::RouteSet.new 
    1049   end 
    1050  
    1051   def test_should_add_deprecated_route 
    1052     @set.expects(:add_named_route).with("old", "path", {:a => "b"}) 
    1053     @set.add_deprecated_named_route("new", "old", "path", {:a => "b"}) 
    1054   end 
    1055    
    1056   def test_should_fire_deprecation_warning_on_access 
    1057     @set.add_deprecated_named_route("new_outer_inner_path", "outer_new_inner_path", "/outers/:outer_id/inners/new", :controller => :inners) 
    1058     ActiveSupport::Deprecation.expects(:warn) 
    1059     @set.named_routes["outer_new_inner_path"] 
    1060   end 
    1061 end 
    1062  
    10631027end # uses_mocha 
    10641028 
    10651029class RouteBuilderTest < Test::Unit::TestCase 
     
    12131177    assert_equal nil, builder.warn_output # should only warn on the :person segment 
    12141178  end 
    12151179   
    1216   def test_comma_isnt_a_route_separator 
    1217     segments = builder.segments_for_route_path '/books/:id,:action' 
    1218     defaults = { :action => 'show' } 
    1219     assert_raise(ArgumentError) do 
    1220       builder.assign_route_options(segments, defaults, {}) 
    1221     end 
    1222   end 
    1223  
    1224   def test_semicolon_isnt_a_route_separator 
    1225     segments = builder.segments_for_route_path '/books/:id;:action' 
    1226     defaults = { :action => 'show' } 
    1227     assert_raise(ArgumentError) do 
    1228       builder.assign_route_options(segments, defaults, {}) 
    1229     end 
    1230   end 
    1231    
    12321180  def test_segmentation_of_dot_path 
    12331181    segments = builder.segments_for_route_path '/books/:action.rss' 
    12341182    assert builder.assign_route_options(segments, {}, {}).empty? 
  • actionpack/test/controller/resources_test.rb

    old new  
    149149   
    150150      assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
    151151        actions.keys.each do |action| 
    152           assert_deprecated_named_route /thread_#{action}_messages/, "/threads/1/messages/#{action}", "thread_#{action}_messages_path", :action => action 
    153152          assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action 
    154153        end 
    155154      end 
     
    168167   
    169168      assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
    170169        actions.keys.each do |action| 
    171           assert_deprecated_named_route /formatted_thread_#{action}_messages/, "/threads/1/messages/#{action}.xml", "formatted_thread_#{action}_messages_path", :action => action, :format => 'xml' 
    172170          assert_named_route "/threads/1/messages/#{action}.xml", "formatted_#{action}_thread_messages_path", :action => action, :format => 'xml' 
    173171        end 
    174172      end 
     
    232230      end 
    233231 
    234232      assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
    235         assert_deprecated_named_route /thread_preview_new_message/, preview_path, :thread_preview_new_message_path, preview_options 
    236233        assert_named_route preview_path, :preview_new_thread_message_path, preview_options 
    237234      end 
    238235    end 
     
    247244      end 
    248245 
    249246      assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options| 
    250         assert_deprecated_named_route /formatted_thread_preview_new_message/, preview_path, :formatted_thread_preview_new_message_path, preview_options 
    251247        assert_named_route preview_path, :formatted_preview_new_thread_message_path, preview_options 
    252248      end 
    253249    end 
     
    313309    end 
    314310  end 
    315311 
    316   # NOTE from dchelimsky: http://dev.rubyonrails.org/ticket/8251 changes some of the 
    317   # named routes. In order to play nice, I didn't delete the old ones, which means 
    318   # that this test fails because until the old ones are deprecated and/or removed, there 
    319   # must be room for two names for the same route. 
    320   # 
    321   # From what I can tell this shouldn't matter - all the other 
    322   # tests in actionpack pass without this one passing. But I could be wrong ;) 
    323   # 
    324   # def test_restful_routes_dont_generate_duplicates 
    325     # with_restful_routing :messages do 
    326     #   routes = ActionController::Routing::Routes.routes 
    327     #   routes.each do |route| 
    328     #     routes.each do |r| 
    329     #       next if route === r # skip the comparison instance 
    330     #       assert distinct_routes?(route, r), "Duplicate Route: #{route}" 
    331     #     end 
    332     #   end 
    333     # end 
    334   # end 
     312  def test_restful_routes_dont_generate_duplicates 
     313    with_restful_routing :messages do 
     314      routes = ActionController::Routing::Routes.routes 
     315      routes.each do |route| 
     316        routes.each do |r| 
     317          next if route === r # skip the comparison instance 
     318          assert distinct_routes?(route, r), "Duplicate Route: #{route}" 
     319        end 
     320      end 
     321    end 
     322  end 
    335323 
    336324  def test_should_create_singleton_resource_routes 
    337325    with_singleton_resources :account do 
     
    471459    end 
    472460  end 
    473461 
     462  def test_resource_action_separator 
     463    with_routing do |set| 
     464      set.draw do |map| 
     465        map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 
     466        map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 
     467      end 
     468       
     469      action_separator = ActionController::Base.resource_action_separator 
     470       
     471      assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 
     472      assert_named_route "/threads/1/messages#{action_separator}search", "search_thread_messages_path", {} 
     473      assert_named_route "/threads/1/messages/new", "new_thread_message_path", {} 
     474      assert_named_route "/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {} 
     475      assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 
     476      assert_named_route "/admin/account#{action_separator}login", "login_admin_account_path", {} 
     477      assert_named_route "/admin/account/new", "new_admin_account_path", {} 
     478      assert_named_route "/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {} 
     479    end 
     480  end 
     481 
     482  def test_new_style_named_routes_for_resource 
     483    with_routing do |set| 
     484      set.draw do |map| 
     485        map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 
     486      end 
     487      assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 
     488      assert_named_route "/threads/1/messages/search", "search_thread_messages_path", {} 
     489      assert_named_route "/threads/1/messages/new", "new_thread_message_path", {} 
     490      assert_named_route "/threads/1/messages/new/preview", "preview_new_thread_message_path", {} 
     491    end 
     492  end 
     493 
     494  def test_new_style_named_routes_for_singleton_resource 
     495    with_routing do |set| 
     496      set.draw do |map| 
     497        map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 
     498      end 
     499      assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 
     500      assert_named_route "/admin/account/login", "login_admin_account_path", {} 
     501      assert_named_route "/admin/account/new", "new_admin_account_path", {} 
     502      assert_named_route "/admin/account/new/preview", "preview_new_admin_account_path", {} 
     503    end 
     504  end 
     505 
    474506  def test_resources_in_namespace 
    475507    with_routing do |set| 
    476508      set.draw do |map| 
     
    624656      assert_named_route "#{full_prefix}/1",          "#{name_prefix}#{singular_name}_path",                options[:options].merge(:id => '1') 
    625657      assert_named_route "#{full_prefix}/1.xml",      "formatted_#{name_prefix}#{singular_name}_path",      options[:options].merge(:id => '1', :format => 'xml') 
    626658 
    627       assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}new_#{singular_name}/, "#{full_prefix}/new", "#{name_prefix}new_#{singular_name}_path", options[:options] 
    628       assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}new_#{singular_name}/, "#{full_prefix}/new.xml", "formatted_#{name_prefix}new_#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
    629       assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}edit_#{singular_name}/, "#{full_prefix}/1/edit",     "#{name_prefix}edit_#{singular_name}_path",           options[:options].merge(:id => '1') 
    630       assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}edit_#{singular_name}/, "#{full_prefix}/1/edit.xml", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 
    631  
    632659      assert_named_route "#{full_prefix}/new",        "new_#{name_prefix}#{singular_name}_path",            options[:options] 
    633660      assert_named_route "#{full_prefix}/new.xml",    "formatted_new_#{name_prefix}#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
    634661      assert_named_route "#{full_prefix}/1/edit",     "edit_#{name_prefix}#{singular_name}_path",           options[:options].merge(:id => '1') 
     
    686713      assert_named_route "#{full_path}",          "#{name_prefix}#{singleton_name}_path",                options[:options] 
    687714      assert_named_route "#{full_path}.xml",      "formatted_#{name_prefix}#{singleton_name}_path",      options[:options].merge(:format => 'xml') 
    688715 
    689       assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}new_#{singleton_name}/, "#{full_path}/new", "#{name_prefix}new_#{singleton_name}_path",            options[:options] 
    690       assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}new_#{singleton_name}/, "#{full_path}/new.xml",  "formatted_#{name_prefix}new_#{singleton_name}_path",  options[:options].merge(:format => 'xml') 
    691       assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}edit_#{singleton_name}/, "#{full_path}/edit",     "#{name_prefix}edit_#{singleton_name}_path",           options[:options] 
    692       assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}edit_#{singleton_name}/, "#{full_path}/edit.xml", "formatted_#{name_prefix}edit_#{singleton_name}_path", options[:options].merge(:format => 'xml') 
    693  
    694716      assert_named_route "#{full_path}/new",      "new_#{name_prefix}#{singleton_name}_path",            options[:options] 
    695717      assert_named_route "#{full_path}/new.xml",  "formatted_new_#{name_prefix}#{singleton_name}_path",  options[:options].merge(:format => 'xml') 
    696718      assert_named_route "#{full_path}/edit",     "edit_#{name_prefix}#{singleton_name}_path",           options[:options] 
     
    702724      assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})" 
    703725    end 
    704726     
    705     def assert_deprecated_named_route(message, expected, route, options) 
    706       assert_deprecated message do 
    707         assert_named_route expected, route, options 
    708       end 
    709     end 
    710  
    711     # These are only deprecated if they have a name_prefix. 
    712     # See http://dev.rubyonrails.org/ticket/8251 
    713     def assert_potentially_deprecated_named_route(name_prefix, message, expected, route, options) 
    714       if name_prefix 
    715         assert_deprecated_named_route(message, expected, route, options) 
    716       else 
    717         assert_named_route expected, route, options 
    718       end 
    719     end 
    720  
    721727    def assert_resource_methods(expected, resource, action_method, method) 
    722728      assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}" 
    723729      expected.each do |action| 
  • actionpack/lib/action_controller/base.rb

    old new  
    308308    # Turn on +ignore_missing_templates+ if you want to unit test actions without making the associated templates. 
    309309    cattr_accessor :ignore_missing_templates 
    310310 
     311    # Controls the resource action separator 
     312    @@resource_action_separator = "/" 
     313    cattr_accessor :resource_action_separator 
     314 
    311315    # Holds the request object that's primarily used to get environment variables through access like 
    312316    # <tt>request.env["REQUEST_URI"]</tt>. 
    313317    attr_internal :request 
     
    603607      def controller_path 
    604608        self.class.controller_path 
    605609      end 
    606        
     610 
    607611      def session_enabled? 
    608612        request.session_options && request.session_options[:disabled] != false 
    609613      end 
    610        
     614 
    611615      # View load paths for controller. 
    612616      def view_paths 
    613617        self.class.view_paths 
  • actionpack/lib/action_controller/resources.rb

    old new  
    9494        "#{name_prefix}#{singular}_" 
    9595      end 
    9696 
     97      def action_separator 
     98        @action_separator ||= Base.resource_action_separator 
     99      end 
    97100 
    98101      protected 
    99102        def arrange_actions 
     
    431434        resource.collection_methods.each do |method, actions| 
    432435          actions.each do |action| 
    433436            action_options = action_options_for(action, resource, method) 
    434  
    435             # See http://dev.rubyonrails.org/ticket/8251 
    436             map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}", action_options) 
    437             map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}.:format", action_options) 
     437            map.named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}", action_options) 
     438            map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}.:format", action_options) 
    438439          end 
    439440        end 
    440441      end 
     
    460461          actions.each do |action| 
    461462            action_options = action_options_for(action, resource, method) 
    462463            if action == :new 
    463               # See http://dev.rubyonrails.org/ticket/8251 
    464               map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options) 
    465               map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options) 
     464              map.named_route("new_#{resource.name_prefix}#{resource.singular}", resource.new_path, action_options) 
     465              map.named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}.:format", action_options) 
    466466            else 
    467               # See http://dev.rubyonrails.org/ticket/8251 
    468               map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}", action_options) 
    469               map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}.:format", action_options) 
     467              map.named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}", action_options) 
     468              map.named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}.:format", action_options) 
    470469            end 
    471470          end 
    472471        end 
     
    476475        resource.member_methods.each do |method, actions| 
    477476          actions.each do |action| 
    478477            action_options = action_options_for(action, resource, method) 
    479             # See http://dev.rubyonrails.org/ticket/8251 
    480             map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}", action_options) 
    481             map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}.:format",action_options) 
     478            map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}", action_options) 
     479            map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}.:format",action_options) 
    482480          end 
    483481        end 
    484482 
  • actionpack/lib/action_controller/routing.rb

    old new  
    247247  #  end 
    248248  # 
    249249  module Routing 
    250     SEPARATORS = %w( / . ? ) 
     250    SEPARATORS = %w( / ; . , ? ) 
    251251 
    252252    HTTP_METHODS = [:get, :head, :post, :put, :delete] 
    253253 
     
    548548    end 
    549549 
    550550    class Segment #:nodoc: 
    551       RESERVED_PCHAR = ':@&=+$,;
     551      RESERVED_PCHAR = ':@&=+$
    552552      UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze 
    553553 
    554554      attr_accessor :is_optional 
     
    561561      def extraction_code 
    562562        nil 
    563563      end 
    564    
     564 
    565565      # Continue generating string for the prior segments. 
    566566      def continue_string_structure(prior_segments) 
    567567        if prior_segments.empty? 
     
    10201020          @set.add_named_route(name, path, options) 
    10211021        end 
    10221022         
    1023         def deprecated_named_route(name, deprecated_name, path, options = {}) 
    1024           named_route(name, path, options) 
    1025           @set.add_deprecated_named_route(name, deprecated_name, path, options) unless deprecated_name == name 
    1026         end 
    1027          
    10281023        # Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model. 
    10291024        # Example: 
    10301025        # 
     
    10571052      class NamedRouteCollection #:nodoc: 
    10581053        include Enumerable 
    10591054 
    1060         attr_reader :routes, :helpers, :deprecated_named_routes 
     1055        attr_reader :routes, :helpers 
    10611056 
    10621057        def initialize 
    10631058          clear! 
     
    10661061        def clear! 
    10671062          @routes = {} 
    10681063          @helpers = [] 
    1069           @deprecated_named_routes = {} 
    10701064           
    10711065          @module ||= Module.new 
    10721066          @module.instance_methods.each do |selector| 
     
    10801074        end 
    10811075 
    10821076        def get(name) 
    1083           if @deprecated_named_routes.has_key?(name.to_sym) 
    1084             ActiveSupport::Deprecation.warn( 
    1085               "The named route \"#{name}\" uses a format that has been deprecated. " + 
    1086               "You should use \"#{@deprecated_named_routes[name]}\" instead", caller 
    1087             ) 
    1088           end 
    10891077          routes[name.to_sym] 
    10901078        end 
    10911079 
     
    12391227        named_routes[name.to_sym] = add_route(path, options) 
    12401228      end 
    12411229   
    1242       def add_deprecated_named_route(name, deprecated_name, path, options = {}) 
    1243         add_named_route(deprecated_name, path, options) 
    1244         named_routes.deprecated_named_routes[deprecated_name.to_sym] = name 
    1245       end 
    1246    
    12471230      def options_as_params(options) 
    12481231        # If an explicit :controller was given, always make :action explicit 
    12491232        # too, so that action expiry works as expected for things like