diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 8a84afd315f0317ff0472b31959fc7d66e9da170..4e9112bc044f5827c2a5a707c8fea52ffd1dee29 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -194,6 +194,21 @@ def match(*args) self end + def mount(app, options = nil) + if options + path = options.delete(:at) + else + options = app + app, path = options.find { |k, v| k.respond_to?(:call) } + options.delete(app) if app + end + + raise "A rack application must be specified" unless path + + match(path, options.merge(:to => app, :anchor => false)) + self + end + def default_url_options=(options) @set.default_url_options = options end @@ -380,14 +395,13 @@ def self.default_actions [:index, :create, :new, :show, :update, :destroy, :edit] end - attr_reader :plural, :singular, :options + attr_reader :controller, :path, :options def initialize(entities, options = {}) - @name = entities.to_s - @options = options - - @plural = @name.pluralize - @singular = @name.singularize + @name = entities.to_s + @path = options.delete(:path) || @name + @controller = options.delete(:controller) || @name.to_s.pluralize + @options = options end def default_actions @@ -417,8 +431,12 @@ def name options[:as] || @name end - def controller - options[:controller] || plural + def plural + name.to_s.pluralize + end + + def singular + name.to_s.singularize end def member_name @@ -509,7 +527,7 @@ def resource(*resources, &block) resource = SingletonResource.new(resources.pop, options) - scope(:path => resource.name.to_s, :controller => resource.controller) do + scope(:path => resource.path, :controller => resource.controller) do with_scope_level(:resource, resource) do scope(:name_prefix => resource.name.to_s, :as => "") do @@ -539,7 +557,7 @@ def resources(*resources, &block) resource = Resource.new(resources.pop, options) - scope(:path => resource.name.to_s, :controller => resource.controller) do + scope(:path => resource.path, :controller => resource.controller) do with_scope_level(:resources, resource) do yield if block_given? @@ -603,21 +621,6 @@ def nested end end - def mount(app, options = nil) - if options - path = options.delete(:at) - else - options = app - app, path = options.find { |k, v| k.respond_to?(:call) } - options.delete(app) if app - end - - raise "A rack application must be specified" unless path - - match(path, options.merge(:to => app, :anchor => false)) - self - end - def match(*args) options = args.extract_options! diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index e58653cb8c6feb37bc09e4be107de3884cb03c23..19538cb88bf400822b70cf881efd732301b863ec 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -58,8 +58,9 @@ def self.matches?(request) get 'admin/accounts' => "queenbee#accounts" end - scope 'es' do - resources :projects, :path_names => { :edit => 'cambiar' }, :as => 'projeto' + scope 'pt', :name_prefix => 'pt' do + resources :projects, :path_names => { :edit => 'editar' }, :path => 'projetos' + resource :admin, :path_names => { :new => 'novo' }, :path => 'administrador' end resources :projects, :controller => :project do @@ -74,10 +75,14 @@ def self.matches?(request) resource :avatar, :controller => :avatar end - resources :images do + resources :images, :as => :funny_images do post :revise, :on => :member end + resource :manager, :as => :super_manager do + post :fire + end + resources :people do nested do scope "/:access_token" do @@ -144,7 +149,7 @@ def self.matches?(request) end namespace :forum do - resources :products, :as => '' do + resources :products, :path => '' do resources :questions end end @@ -430,15 +435,35 @@ def test_projects_companies end end + def test_project_manager + with_test_routes do + get '/projects/1/manager' + assert_equal 'managers#show', @response.body + assert_equal '/projects/1/manager', project_super_manager_path(:project_id => '1') + + get '/projects/1/manager/new' + assert_equal 'managers#new', @response.body + assert_equal '/projects/1/manager/new', new_project_super_manager_path(:project_id => '1') + + post '/projects/1/manager/fire' + assert_equal 'managers#fire', @response.body + assert_equal '/projects/1/manager/fire', fire_project_super_manager_path(:project_id => '1') + end + end + def test_project_images with_test_routes do get '/projects/1/images' assert_equal 'images#index', @response.body - assert_equal '/projects/1/images', project_images_path(:project_id => '1') + assert_equal '/projects/1/images', project_funny_images_path(:project_id => '1') + + get '/projects/1/images/new' + assert_equal 'images#new', @response.body + assert_equal '/projects/1/images/new', new_project_funny_image_path(:project_id => '1') post '/projects/1/images/1/revise' assert_equal 'images#revise', @response.body - assert_equal '/projects/1/images/1/revise', revise_project_image_path(:project_id => '1', :id => '1') + assert_equal '/projects/1/images/1/revise', revise_project_funny_image_path(:project_id => '1', :id => '1') end end @@ -552,11 +577,21 @@ def test_resources_for_uncountable_names def test_path_names with_test_routes do - get '/es/projeto' + get '/pt/projetos' assert_equal 'projects#index', @response.body + assert_equal '/pt/projetos', pt_projects_path - get '/es/projeto/1/cambiar' + get '/pt/projetos/1/editar' assert_equal 'projects#edit', @response.body + assert_equal '/pt/projetos/1/editar', edit_pt_project_path(1) + + get '/pt/administrador' + assert_equal 'admins#show', @response.body + assert_equal '/pt/administrador', pt_admin_path + + get '/pt/administrador/novo' + assert_equal 'admins#new', @response.body + assert_equal '/pt/administrador/novo', new_pt_admin_path end end