Changeset 1220

Show
Ignore:
Timestamp:
01/08/08 22:31:03 (11 months ago)
Author:
e.@brainspl.at
Message:

Custom member and collection resource routes should accept a format file extension like .xml etc.. closes #424 [brasten@gmail.com]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/router.rb

    r1175 r1220  
    366366          member.each_pair do |action, methods| 
    367367            behaviors << Behavior.new( 
    368               { :path => %r{^/:id[/;]#{action}$}, :method => /^(#{[methods].flatten * '|'})$/ }, 
     368              { :path => %r{^/:id[/;]#{action}(\.:format)?$}, :method => /^(#{[methods].flatten * '|'})$/ }, 
    369369              { :action => action.to_s }, next_level 
    370370            ) 
     
    376376          collection.each_pair do |action, methods| 
    377377            behaviors << Behavior.new( 
    378               { :path => %r{^[/;]#{action}$}, :method => /^(#{[methods].flatten * '|'})$/ }, 
     378              { :path => %r{^[/;]#{action}(\.:format)?$}, :method => /^(#{[methods].flatten * '|'})$/ }, 
    379379              { :action => action.to_s }, next_level 
    380380            ) 
  • trunk/spec/merb/router_spec.rb

    r1175 r1220  
    565565    generate(:random_flowers).should == '/flowers/random' 
    566566  end 
    567 end 
    568  
    569 describe Merb::Router, "with resources using a member action" do 
     567 
     568  it "should match GET to /flowers/random" do 
     569    index, route = Merb::Router.match(SimpleRequest.new(:path => '/flowers/random', :method => :get), {}) 
     570    route[:controller].should == 'flowers' 
     571    route[:action].should == 'random' 
     572  end 
     573 
     574  it "should match GET to /flowers/random.xml" do 
     575    index, route = Merb::Router.match(SimpleRequest.new(:path => '/flowers/random.xml', :method => :get), {}) 
     576    route[:controller].should == 'flowers' 
     577    route[:action].should == 'random' 
     578    route[:format].should == 'xml' 
     579  end 
     580 
     581end 
     582 
     583describe Merb::Router, "with resources using a member action { :pick => [:get] }" do 
    570584  before :each do 
    571585    Merb::Router.prepare do |r| 
     
    577591    generate(:pick_flower, :id => 1).should == '/flowers/1/pick' 
    578592  end 
     593   
     594  it "should match GET to /flowers/2/pick" do 
     595    index, route = Merb::Router.match(SimpleRequest.new(:path => '/flowers/2/pick', :method => :get), {}) 
     596    route[:controller].should == 'flowers' 
     597    route[:action].should == 'pick' 
     598    route[:id].should == '2' 
     599  end 
     600 
     601  it "should not match POST to /flowers/2/pick" do 
     602    index, route = Merb::Router.match(SimpleRequest.new(:path => '/flowers/2/pick', :method => :post), {}) 
     603    route[:controller].should_not == 'flowers' 
     604    route[:action].should_not == 'pick' 
     605    route[:id].should_not == '2' 
     606  end 
     607 
     608  it "should not match PUT to /flowers/2/pick" do 
     609    index, route = Merb::Router.match(SimpleRequest.new(:path => '/flowers/2/pick', :method => :put), {}) 
     610    route[:controller].should_not == 'flowers' 
     611    route[:action].should_not == 'pick' 
     612    route[:id].should_not == '2' 
     613  end 
     614 
     615  it "should not match DELETE to /flowers/2/pick" do 
     616    index, route = Merb::Router.match(SimpleRequest.new(:path => '/flowers/2/pick', :method => :delete), {}) 
     617    route[:controller].should_not == 'flowers' 
     618    route[:action].should_not == 'pick' 
     619    route[:id].should_not == '2' 
     620  end 
     621   
     622  it "should match GET to /flowers/2/pick.xml" do 
     623    index, route = Merb::Router.match(SimpleRequest.new(:path => '/flowers/2/pick.xml', :method => :get), {}) 
     624    route[:controller].should == 'flowers' 
     625    route[:action].should == 'pick' 
     626    route[:id].should == '2' 
     627    route[:format].should == 'xml' 
     628  end 
     629   
    579630end 
    580631