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

Changeset 7415

Show
Ignore:
Timestamp:
09/06/07 14:28:32 (1 year ago)
Author:
xal
Message:

Remove deprecated named routes [pixeltrix]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/base.rb

    r7403 r7415  
    311311    cattr_accessor :ignore_missing_templates 
    312312 
     313    # Controls the resource action separator 
     314    @@resource_action_separator = "/" 
     315    cattr_accessor :resource_action_separator 
     316 
    313317    # Holds the request object that's primarily used to get environment variables through access like 
    314318    # <tt>request.env["REQUEST_URI"]</tt>. 
     
    606610        self.class.controller_path 
    607611      end 
    608        
     612 
    609613      def session_enabled? 
    610614        request.session_options && request.session_options[:disabled] != false 
    611615      end 
    612        
     616 
    613617      # View load paths for controller. 
    614618      def view_paths 
  • trunk/actionpack/lib/action_controller/resources.rb

    r7234 r7415  
    9595      end 
    9696 
     97      def action_separator 
     98        @action_separator ||= Base.resource_action_separator 
     99      end 
    97100 
    98101      protected 
     
    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 
     
    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 
     
    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 
  • trunk/actionpack/lib/action_controller/routing.rb

    r7411 r7415  
    248248  # 
    249249  module Routing 
    250     SEPARATORS = %w( / . ? ) 
     250    SEPARATORS = %w( / ; . , ? ) 
    251251 
    252252    HTTP_METHODS = [:get, :head, :post, :put, :delete] 
     
    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 
     
    562562        nil 
    563563      end 
    564    
     564 
    565565      # Continue generating string for the prior segments. 
    566566      def continue_string_structure(prior_segments) 
     
    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: 
     
    10581053        include Enumerable 
    10591054 
    1060         attr_reader :routes, :helpers, :deprecated_named_routes 
     1055        attr_reader :routes, :helpers 
    10611056 
    10621057        def initialize 
     
    10671062          @routes = {} 
    10681063          @helpers = [] 
    1069           @deprecated_named_routes = {} 
    10701064           
    10711065          @module ||= Module.new 
     
    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 
     
    12601248      end 
    12611249   
    1262       def add_deprecated_named_route(name, deprecated_name, path, options = {}) 
    1263         add_named_route(deprecated_name, path, options) 
    1264         named_routes.deprecated_named_routes[deprecated_name.to_sym] = name 
    1265       end 
    1266    
    12671250      def options_as_params(options) 
    12681251        # If an explicit :controller was given, always make :action explicit 
  • trunk/actionpack/test/controller/resources_test.rb

    r7234 r7415  
    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 
     
    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 
     
    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 
     
    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 
     
    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 
     
    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| 
     
    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') 
     
    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') 
     
    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}" 
  • trunk/actionpack/test/controller/routing_test.rb

    r7411 r7415  
    2323    end 
    2424 
    25     safe, unsafe = %w(: @ & = + $ , ;), %w(^ / ? # [ ]
     25    safe, unsafe = %w(: @ & = + $), %w(^ / ? # [ ] , ;
    2626    hex = unsafe.map { |char| '%' + char.unpack('H2').first.upcase } 
    2727 
     
    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 
     
    12121176     
    12131177    assert_equal nil, builder.warn_output # should only warn on the :person segment 
    1214   end 
    1215    
    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 
    12301178  end 
    12311179