Created
August 15, 2008 06:41
-
-
Save carllerche/5546 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "a resource with extra actions" do | |
collection = { :one => :get, :two => :post, :three => :put, :four => :delete } | |
member = { :five => :get, :six => :post, :seven => :put, :eight => :delete } | |
before(:each) do | |
Router.prepare do |r| | |
r.resources :users, :collection => collection, :member => member | |
end | |
end | |
# Loop through each method declared on the collection and make sure that they | |
# are available only when the request is using the specified method | |
collection.each_pair do |action, method| | |
it "should be able to add extra #{method} methods on the collection with an optional :format" do | |
route_to("/users/#{action}", :method => method).should have_route(:controller => "users", :action => "#{action}", :id => nil, :format => nil) | |
route_to("/users/#{action}.xml", :method => method).should have_route(:controller => "users", :action => "#{action}", :id => nil, :format => "xml") | |
end | |
it "should still route /#{action} on get to show" do | |
route_to("/users/#{action}").should have_route(:controller => "users", :action => "show", :id => "#{action}") | |
end unless method == :get | |
it "should still route /#{action} on put to update" do | |
route_to("/users/#{action}", :method => :put).should have_route(:controller => "users", :action => "update", :id => "#{action}") | |
end unless method == :put | |
it "should still route /#{action} on delete to destroy" do | |
route_to("/users/#{action}", :method => :delete).should have_route(:controller => "users", :action => "destroy", :id => "#{action}") | |
end unless method == :delete | |
it "should not match /#{action} on post to anything" do | |
route_to("/users/#{action}", :method => :post).should be_empty | |
end unless method == :post | |
end | |
member.each_pair do |action, method| | |
it "should be able to add extra #{method} methods on the member with an optional :format" do | |
route_to("/users/2/#{action}", :method => method).should have_route(:controller => "users", :action => "#{action}", :id => "2", :format => nil) | |
route_to("/users/2/#{action}.xml", :method => method).should have_route(:controller => "users", :action => "#{action}", :id => "2", :format => "xml") | |
end | |
other_methods = [:get, :post, :put, :delete] - [method] | |
other_methods.each do |other| | |
it "should not route /#{action} on #{other} to anything" do | |
route_to("/users/2/#{action}", :method => other).should be_empty | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment