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

Ticket #8558: fix_resource_route_names.stable.diff

File fix_resource_route_names.stable.diff, 18.3 kB (added by pixeltrix, 1 year ago)
  • actionpack/test/controller/resources_test.rb

    old new  
    6363    end 
    6464  end 
    6565 
    66   def test_multile_with_path_prefix 
     66  def test_multiple_with_path_prefix 
    6767    with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do 
    6868      assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 
    6969      assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 
     
    312312    end 
    313313  end 
    314314 
     315  def test_resource_action_separator 
     316    with_routing do |set| 
     317      set.draw do |map| 
     318        map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 
     319        map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 
     320      end 
     321       
     322      action_separator = ActionController::Base.resource_action_separator 
     323       
     324      assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 
     325      assert_named_route "/threads/1/messages#{action_separator}search", "search_thread_messages_path", {} 
     326      assert_named_route "/threads/1/messages/new", "new_thread_message_path", {} 
     327      assert_named_route "/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {} 
     328      assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 
     329      assert_named_route "/admin/account#{action_separator}login", "login_admin_account_path", {} 
     330      assert_named_route "/admin/account/new", "new_admin_account_path", {} 
     331      assert_named_route "/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {} 
     332    end 
     333  end 
     334 
     335  def test_new_style_named_routes_for_resource 
     336    with_routing do |set| 
     337      set.draw do |map| 
     338        map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 
     339      end 
     340      assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 
     341      assert_named_route "/threads/1/messages;search", "search_thread_messages_path", {} 
     342      assert_named_route "/threads/1/messages/new", "new_thread_message_path", {} 
     343      assert_named_route "/threads/1/messages/new;preview", "preview_new_thread_message_path", {} 
     344    end 
     345  end 
     346 
     347  def test_new_style_named_routes_for_singleton_resource 
     348    with_routing do |set| 
     349      set.draw do |map| 
     350        map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 
     351      end 
     352      assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 
     353      assert_named_route "/admin/account;login", "login_admin_account_path", {} 
     354      assert_named_route "/admin/account/new", "new_admin_account_path", {} 
     355      assert_named_route "/admin/account/new;preview", "preview_new_admin_account_path", {} 
     356    end 
     357  end 
     358 
     359  def test_should_add_deprecated_named_routes_for_resource 
     360    with_routing do |set| 
     361      set.draw do |map| 
     362        map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id' 
     363      end 
     364      assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' } 
     365      assert_deprecated do 
     366        assert_named_route "/threads/1/messages;search", "thread_search_messages_path", {} 
     367        assert_named_route "/threads/1/messages/new", "thread_new_message_path", {} 
     368        assert_named_route "/threads/1/messages/new;preview", "thread_preview_new_message_path", {} 
     369      end 
     370    end 
     371  end 
     372 
     373  def test_should_add_deprecated_named_routes_for_singleton_resource 
     374    with_routing do |set| 
     375      set.draw do |map| 
     376        map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin' 
     377      end 
     378      assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/' 
     379      assert_deprecated do 
     380        assert_named_route "/admin/account;login", "admin_login_account_path", {} 
     381        assert_named_route "/admin/account/new", "admin_new_account_path", {} 
     382        assert_named_route "/admin/account/new;preview", "admin_preview_new_account_path", {} 
     383      end 
     384    end 
     385  end 
     386 
    315387  protected 
    316388    def with_restful_routing(*args) 
    317389      with_routing do |set| 
     
    345417      member_path                = "#{collection_path}/1" 
    346418      new_path                   = "#{collection_path}/new" 
    347419      edit_member_path           = "#{member_path};edit" 
    348       formatted_edit_member_path = "#{member_path}.xml;edit
     420      formatted_edit_member_path = "#{member_path};edit.xml
    349421 
    350422      with_options(options[:options]) do |controller| 
    351423        controller.assert_routing collection_path,            :action => 'index' 
     
    395467      name_prefix = options[:name_prefix] 
    396468 
    397469      assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options] 
    398       assert_named_route "#{full_prefix}/new",        "#{name_prefix}new_#{singular_name}_path",            options[:options] 
     470      assert_named_route "#{full_prefix}/new",        "new_#{name_prefix}#{singular_name}_path",            options[:options] 
    399471      assert_named_route "#{full_prefix}/1",          "#{name_prefix}#{singular_name}_path",                options[:options].merge(:id => '1') 
    400       assert_named_route "#{full_prefix}/1;edit",     "#{name_prefix}edit_#{singular_name}_path",           options[:options].merge(:id => '1') 
     472      assert_named_route "#{full_prefix}/1;edit",     "edit_#{name_prefix}#{singular_name}_path",           options[:options].merge(:id => '1') 
    401473      assert_named_route "#{full_prefix}.xml",        "formatted_#{name_prefix}#{controller_name}_path",    options[:options].merge(            :format => 'xml') 
    402       assert_named_route "#{full_prefix}/new.xml",    "formatted_#{name_prefix}new_#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
     474      assert_named_route "#{full_prefix}/new.xml",    "formatted_new_#{name_prefix}#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
    403475      assert_named_route "#{full_prefix}/1.xml",      "formatted_#{name_prefix}#{singular_name}_path",      options[:options].merge(:id => '1', :format => 'xml') 
    404       assert_named_route "#{full_prefix}/1.xml;edit", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 
     476      assert_named_route "#{full_prefix}/1;edit.xml", "formatted_edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 
    405477      yield options[:options] if block_given? 
    406478    end 
    407479 
     
    411483      full_path           = "/#{options[:path_prefix]}#{singleton_name}" 
    412484      new_path            = "#{full_path}/new" 
    413485      edit_path           = "#{full_path};edit" 
    414       formatted_edit_path = "#{full_path}.xml;edit
     486      formatted_edit_path = "#{full_path};edit.xml
    415487 
    416488      with_options options[:options] do |controller| 
    417489        controller.assert_routing full_path,           :action => 'show' 
     
    448520      options[:options].delete :action 
    449521 
    450522      full_path = "/#{options[:path_prefix]}#{singleton_name}" 
     523      full_name = "#{options[:name_prefix]}#{singleton_name}" 
    451524 
    452       assert_named_route "#{full_path}",          "#{singleton_name}_path",                options[:options] 
    453       assert_named_route "#{full_path}/new",      "new_#{singleton_name}_path",            options[:options] 
    454       assert_named_route "#{full_path};edit",     "edit_#{singleton_name}_path",           options[:options] 
    455       assert_named_route "#{full_path}.xml",      "formatted_#{singleton_name}_path",      options[:options].merge(:format => 'xml') 
    456       assert_named_route "#{full_path}/new.xml",  "formatted_new_#{singleton_name}_path",  options[:options].merge(:format => 'xml') 
    457       assert_named_route "#{full_path}.xml;edit", "formatted_edit_#{singleton_name}_path", options[:options].merge(:format => 'xml') 
     525      assert_named_route "#{full_path}",          "#{full_name}_path",                options[:options] 
     526      assert_named_route "#{full_path}/new",      "new_#{full_name}_path",            options[:options] 
     527      assert_named_route "#{full_path};edit",     "edit_#{full_name}_path",           options[:options] 
     528      assert_named_route "#{full_path}.xml",      "formatted_#{full_name}_path",      options[:options].merge(:format => 'xml') 
     529      assert_named_route "#{full_path}/new.xml",  "formatted_new_#{full_name}_path",  options[:options].merge(:format => 'xml') 
     530      assert_named_route "#{full_path};edit.xml", "formatted_edit_#{full_name}_path", options[:options].merge(:format => 'xml') 
    458531    end 
    459532 
    460533    def assert_named_route(expected, route, options) 
  • actionpack/lib/action_controller/base.rb

    old new  
    292292    # Turn on +ignore_missing_templates+ if you want to unit test actions without making the associated templates. 
    293293    cattr_accessor :ignore_missing_templates 
    294294 
     295    # Controls the resource action separator 
     296    @@resource_action_separator = ";" 
     297    cattr_accessor :resource_action_separator 
     298 
    295299    # Holds the request object that's primarily used to get environment variables through access like 
    296300    # <tt>request.env["REQUEST_URI"]</tt>. 
    297301    attr_internal :request 
  • actionpack/lib/action_controller/resources.rb

    old new  
    1616        add_default_actions 
    1717        set_prefixes 
    1818      end 
    19        
     19 
    2020      def controller 
    2121        @controller ||= (options[:controller] || plural).to_s 
    2222      end 
    23        
     23 
    2424      def path 
    2525        @path ||= "#{path_prefix}/#{plural}" 
    2626      end 
    27        
     27 
    2828      def new_path 
    2929        @new_path ||= "#{path}/new" 
    3030      end 
    31        
     31 
    3232      def member_path 
    3333        @member_path ||= "#{path}/:id" 
    3434      end 
    35        
     35 
    3636      def nesting_path_prefix 
    3737        @nesting_path_prefix ||= "#{path}/:#{singular}_id" 
    3838      end 
    39        
     39 
     40      def action_separator 
     41        @action_separator ||= Base.resource_action_separator 
     42      end 
     43 
    4044      protected 
    4145        def arrange_actions 
    4246          @collection_methods = arrange_actions_by_methods(options.delete(:collection)) 
     
    324328        resource.collection_methods.each do |method, actions| 
    325329          actions.each do |action| 
    326330            action_options = action_options_for(action, resource, method) 
    327             map.named_route("#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path};#{action}", action_options) 
    328             map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}.:format;#{action}", action_options) 
     331 
     332            unless resource.name_prefix.blank? 
     333              map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.name_prefix}#{action}_#{resource.plural}") 
     334              map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.name_prefix}#{action}_#{resource.plural}") 
     335            end 
     336 
     337            map.named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}", action_options) 
     338            map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}.:format", action_options) 
    329339          end 
    330340        end 
    331341      end 
     
    351361          actions.each do |action| 
    352362            action_options = action_options_for(action, resource, method) 
    353363            if action == :new 
    354               map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options) 
    355               map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options) 
     364 
     365              unless resource.name_prefix.blank? 
     366                map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}new_#{resource.singular}") 
     367                map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}new_#{resource.singular}") 
     368              end 
     369 
     370              map.named_route("new_#{resource.name_prefix}#{resource.singular}", resource.new_path, action_options) 
     371              map.named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}.:format", action_options) 
     372 
    356373            else 
    357               map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", action_options) 
    358               map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", action_options) 
     374 
     375              unless resource.name_prefix.blank? 
     376                map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_new_#{resource.singular}") 
     377                map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}") 
     378              end 
     379 
     380              map.named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}", action_options) 
     381              map.named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}.:format", action_options) 
     382 
    359383            end 
    360384          end 
    361385        end 
     
    365389        resource.member_methods.each do |method, actions| 
    366390          actions.each do |action| 
    367391            action_options = action_options_for(action, resource, method) 
    368             map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path};#{action}", action_options) 
    369             map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}.:format;#{action}",action_options) 
     392 
     393            unless resource.name_prefix.blank? 
     394              map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_#{resource.singular}") 
     395              map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_#{resource.singular}") 
     396            end 
     397 
     398            map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}", action_options) 
     399            map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}.:format", action_options) 
     400 
    370401          end 
    371402        end 
    372403 
  • actionpack/lib/action_controller/routing.rb

    old new  
    989989        def named_route(name, path, options = {}) 
    990990          @set.add_named_route(name, path, options) 
    991991        end 
     992         
     993        def deprecated_named_route(name, deprecated_name, options = {}) 
     994          @set.add_deprecated_named_route(name, deprecated_name) 
     995        end 
    992996 
    993997        # Added deprecation notice for anyone who already added a named route called "root". 
    994998        # It'll be used as a shortcut for map.connect '' in Rails 2.0. 
     
    10191023        def clear! 
    10201024          @routes = {} 
    10211025          @helpers = [] 
    1022            
     1026 
    10231027          @module ||= Module.new 
    10241028          @module.instance_methods.each do |selector| 
    10251029            @module.send :remove_method, selector 
     
    10551059        def install(destinations = [ActionController::Base, ActionView::Base]) 
    10561060          Array(destinations).each { |dest| dest.send :include, @module } 
    10571061        end 
     1062         
     1063        def define_deprecated_named_route_methods(name, deprecated_name) 
    10581064 
     1065          [:url, :path].each do |kind| 
     1066            @module.send :module_eval, <<-end_eval # We use module_eval to avoid leaks 
     1067 
     1068              def #{url_helper_name(deprecated_name, kind)}(*args) 
     1069 
     1070                ActiveSupport::Deprecation.warn( 
     1071                  'The named route "#{url_helper_name(deprecated_name, kind)}" uses a format that has been deprecated. ' + 
     1072                  'You should use "#{url_helper_name(name, kind)}" instead.', caller 
     1073                ) 
     1074 
     1075                send :#{url_helper_name(name, kind)}, *args 
     1076 
     1077              end 
     1078 
     1079              def #{hash_access_name(deprecated_name, kind)}(*args) 
     1080 
     1081                ActiveSupport::Deprecation.warn( 
     1082                  'The named route "#{hash_access_name(deprecated_name, kind)}" uses a format that has been deprecated. ' + 
     1083                  'You should use "#{hash_access_name(name, kind)}" instead.', caller 
     1084                ) 
     1085 
     1086                send :#{hash_access_name(name, kind)}, *args 
     1087 
     1088              end 
     1089 
     1090            end_eval 
     1091          end 
     1092 
     1093        end 
     1094 
    10591095        private 
    10601096          def url_helper_name(name, kind = :url) 
    10611097            :"#{name}_#{kind}" 
     
    11771213      def add_named_route(name, path, options = {}) 
    11781214        named_routes[name] = add_route(path, options) 
    11791215      end 
     1216       
     1217      def add_deprecated_named_route(name, deprecated_name) 
     1218        named_routes.define_deprecated_named_route_methods(name, deprecated_name) 
     1219      end 
    11801220   
    11811221      def options_as_params(options) 
    11821222        # If an explicit :controller was given, always make :action explicit