Browse files

Tack format onto resource routes

  • Loading branch information...
1 parent 2be5e08 commit 511cef296bd07fa43794e029e12e4cd1053aa8d1 @josh josh committed Dec 8, 2009
Showing with 37 additions and 25 deletions.
  1. +22 −22 actionpack/lib/action_dispatch/routing/mapper.rb
  2. +15 −3 actionpack/test/dispatch/routing_test.rb
View
44 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -46,8 +46,8 @@ def match(*args)
conditions, defaults = {}, {}
path = nil if path == ""
- path = Rack::Mount::Utils.normalize_path(path) if path
path = "#{@scope[:path]}#{path}" if @scope[:path]
+ path = Rack::Mount::Utils.normalize_path(path) if path
raise ArgumentError, "path is required" unless path
@@ -169,7 +169,7 @@ def scope(*args)
if path = options.delete(:path)
path_set = true
- path, @scope[:path] = @scope[:path], "#{@scope[:path]}#{Rack::Mount::Utils.normalize_path(path)}"
+ path, @scope[:path] = @scope[:path], Rack::Mount::Utils.normalize_path(@scope[:path].to_s + path.to_s)
else
path_set = false
end
@@ -214,7 +214,7 @@ def controller(controller)
end
def namespace(path)
- scope(path.to_s) { yield }
+ scope("/#{path}") { yield }
end
def constraints(constraints = {})
@@ -297,16 +297,16 @@ def resource(*resources, &block)
return self
end
- scope(:path => resource.name, :controller => resource.controller) do
+ scope(:path => "/#{resource.name}", :controller => resource.controller) do
with_scope_level(:resource, resource) do
yield if block_given?
- get "", :to => :show, :as => resource.member_name
- post "", :to => :create
- put "", :to => :update
- delete "", :to => :destroy
- get "new", :to => :new, :as => "new_#{resource.singular}"
- get "edit", :to => :edit, :as => "edit_#{resource.singular}"
+ get "(.:format)", :to => :show, :as => resource.member_name
+ post "(.:format)", :to => :create
+ put "(.:format)", :to => :update
+ delete "(.:format)", :to => :destroy
+ get "/new(.:format)", :to => :new, :as => "new_#{resource.singular}"
+ get "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}"
end
end
@@ -331,22 +331,22 @@ def resources(*resources, &block)
return self
end
- scope(:path => resource.name, :controller => resource.controller) do
+ scope(:path => "/#{resource.name}", :controller => resource.controller) do
with_scope_level(:resources, resource) do
yield if block_given?
with_scope_level(:collection) do
- get "", :to => :index, :as => resource.collection_name
- post "", :to => :create
- get "new", :to => :new, :as => "new_#{resource.singular}"
+ get "(.:format)", :to => :index, :as => resource.collection_name
+ post "(.:format)", :to => :create
+ get "/new(.:format)", :to => :new, :as => "new_#{resource.singular}"
end
with_scope_level(:member) do
- scope(":id") do
- get "", :to => :show, :as => resource.member_name
- put "", :to => :update
- delete "", :to => :destroy
- get "edit", :to => :edit, :as => "edit_#{resource.singular}"
+ scope("/:id") do
+ get "(.:format)", :to => :show, :as => resource.member_name
+ put "(.:format)", :to => :update
+ delete "(.:format)", :to => :destroy
+ get "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}"
end
end
end
@@ -373,7 +373,7 @@ def member
end
with_scope_level(:member) do
- scope(":id", :name_prefix => parent_resource.member_name, :as => "") do
+ scope("/:id", :name_prefix => parent_resource.member_name, :as => "") do
yield
end
end
@@ -385,7 +385,7 @@ def nested
end
with_scope_level(:nested) do
- scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do
+ scope("/#{parent_resource.id_segment}", :name_prefix => parent_resource.member_name) do
yield
end
end
@@ -402,7 +402,7 @@ def match(*args)
if args.first.is_a?(Symbol)
begin
old_name_prefix, @scope[:name_prefix] = @scope[:name_prefix], "#{args.first}_#{@scope[:name_prefix]}"
- return match(args.first.to_s, options.merge(:to => args.first.to_sym))
+ return match("/#{args.first}(.:format)", options.merge(:to => args.first.to_sym))
ensure
@scope[:name_prefix] = old_name_prefix
end
View
18 actionpack/test/dispatch/routing_test.rb
@@ -95,9 +95,9 @@ def self.matches?(request)
end
controller :articles do
- scope 'articles', :name_prefix => 'article' do
- scope :path => ':title', :title => /[a-z]+/, :as => :with_title do
- match ':id', :to => :with_id
+ scope '/articles', :name_prefix => 'article' do
+ scope :path => '/:title', :title => /[a-z]+/, :as => :with_title do
+ match '/:id', :to => :with_id
end
end
end
@@ -196,14 +196,26 @@ def test_projects
assert_equal 'projects#index', @response.body
assert_equal '/projects', projects_path
+ get '/projects.xml'
+ assert_equal 'projects#index', @response.body
+ assert_equal '/projects.xml', projects_path(:format => 'xml')
+
get '/projects/new'
assert_equal 'projects#new', @response.body
assert_equal '/projects/new', new_project_path
+ get '/projects/new.xml'
+ assert_equal 'projects#new', @response.body
+ assert_equal '/projects/new.xml', new_project_path(:format => 'xml')
+
get '/projects/1'
assert_equal 'projects#show', @response.body
assert_equal '/projects/1', project_path(:id => '1')
+ get '/projects/1.xml'
+ assert_equal 'projects#show', @response.body
+ assert_equal '/projects/1.xml', project_path(:id => '1', :format => 'xml')
+
get '/projects/1/edit'
assert_equal 'projects#edit', @response.body
assert_equal '/projects/1/edit', edit_project_path(:id => '1')

0 comments on commit 511cef2

Please sign in to comment.