public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Adding the ability to set conditions on resource keys

When defining resource routes, conditions for the keys
can now be set by simply passing those conditions along
as extra keys of the options hash.

For example:

Merb::Router.prepare do
  resources :users, :id => /[a-z]+/
end

This will also work with Array keys, such as.

Merb::Router.prepare do
  resources :users, :keys => [:name, :age],
    :name => /[a-z]+/, :age => /\d+/
end
carllerche (author)
Sat Oct 11 17:22:14 -0700 2008
commit  3c1d2dcfc2fd64957ff0f9b65ca11bb8b52d11fd
tree    2eaa101dcea098db8e0bedd94171c47aaac72515
parent  e3ec4814766fc1dfbbdbf2f1b7bff1f13b08ecc8
...
70
71
72
 
 
73
74
75
...
118
119
120
121
 
122
123
124
125
126
127
 
128
129
130
131
132
 
133
134
135
136
 
137
138
139
140
141
142
 
 
 
143
144
145
146
147
148
149
 
150
151
152
153
154
 
155
156
157
158
159
 
160
161
162
...
226
227
228
 
229
230
231
...
270
271
272
 
 
 
 
 
273
274
275
...
70
71
72
73
74
75
76
77
...
120
121
122
 
123
124
125
126
127
128
 
129
130
131
132
133
 
134
135
136
137
 
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
 
154
155
156
157
158
 
159
160
161
162
163
 
164
165
166
167
...
231
232
233
234
235
236
237
...
276
277
278
279
280
281
282
283
284
285
286
0
@@ -70,6 +70,8 @@ module Merb
0
       def resources(name, *args, &block)
0
         name       = name.to_s
0
         options    = extract_options_from_args!(args) || {}
0
+        match_opts = options.except(*resource_options)
0
+        options    = options.only(*resource_options)
0
         singular   = options[:singular] ? options[:singular].to_s : Extlib::Inflection.singularize(name)
0
         klass_name = args.first ? args.first.to_s : Extlib::Inflection.classify(singular)
0
         klass      = Object.full_const_get(klass_name) rescue nil
0
@@ -118,45 +120,48 @@ module Merb
0
           end
0
 
0
           # => show
0
-          resource.match("/#{root_keys}(.:format)", :method => :get).to(:action => "show").
0
+          resource.match("/#{root_keys}(.:format)", match_opts.merge(:method => :get)).to(:action => "show").
0
             name(singular).register_resource(klass_name)
0
 
0
           # => user defined member routes
0
           member.each_pair do |action, method|
0
             action = action.to_s
0
-            resource.match("/#{root_keys}/#{action}(.:format)", :method => method).
0
+            resource.match("/#{root_keys}/#{action}(.:format)", match_opts.merge(:method => method)).
0
               to(:action => "#{action}").name(action, singular).register_resource(klass_name, action)
0
           end
0
 
0
           # => update
0
-          resource.match("/#{root_keys}(.:format)", :method => :put).
0
+          resource.match("/#{root_keys}(.:format)", match_opts.merge(:method => :put)).
0
             to(:action => "update")
0
             
0
           # => destroy
0
-          resource.match("/#{root_keys}(.:format)", :method => :delete).
0
+          resource.match("/#{root_keys}(.:format)", match_opts.merge(:method => :delete)).
0
             to(:action => "destroy")
0
 
0
           if block_given?
0
             nested_keys = keys.map do |k|
0
               k.to_s == "id" ? ":#{singular}_id" : ":#{k}"
0
             end.join("/")
0
+
0
+            nested_match_opts = match_opts.except(:id)
0
+            nested_match_opts["#{singular}_id".to_sym] = match_opts[:id] if match_opts[:id]
0
             
0
             # Procs for building the extra collection/member resource routes
0
             placeholder = Router.resource_routes[ [@options[:resource_prefix], klass_name].flatten.compact ]
0
             builders    = {}
0
             
0
             builders[:collection] = lambda do |action, to, method|
0
-              resource.before(placeholder).match("/#{action}(.:format)", :method => method).
0
+              resource.before(placeholder).match("/#{action}(.:format)", match_opts.merge(:method => method)).
0
                 to(:action => to).name(action, name).register_resource(name, action)
0
             end
0
             
0
             builders[:member] = lambda do |action, to, method|
0
-              resource.match("/#{root_keys}/#{action}(.:format)", :method => method).
0
+              resource.match("/#{root_keys}/#{action}(.:format)", match_opts.merge(:method => method)).
0
                 to(:action => to).name(action, singular).register_resource(klass_name, action)
0
             end
0
             
0
             resource.options(:name_prefix => singular, :resource_prefix => klass_name).
0
-              match("/#{nested_keys}").resource_block(builders, &block)
0
+              match("/#{nested_keys}", nested_match_opts).resource_block(builders, &block)
0
           end
0
         end # namespace
0
       end # resources
0
@@ -226,6 +231,7 @@ module Merb
0
 
0
         self.namespace(name, options).to(params) do |resource|
0
           # => show
0
+          
0
           resource.match("(.:format)", :method => :get).to(:action => "show").
0
             name(name).register_resource(name)
0
             
0
@@ -270,6 +276,11 @@ module Merb
0
         behavior = ResourceBehavior.new(builders, @proxy, @conditions, @params, @defaults, @identifiers, @options, @blocks)
0
         with_behavior_context(behavior, &block)
0
       end
0
+      
0
+      def resource_options
0
+        [:singular, :keys, :key, :controller, :member, :collection, :identify,
0
+          :name_prefix, :resource_prefix, :controller_prefix, :namespace, :path]
0
+      end
0
 
0
     end # Resources
0
     
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
...
140
141
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
144
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
0
@@ -1,93 +1,5 @@
0
 require File.join(File.dirname(__FILE__), "..", "spec_helper")
0
 
0
-def it_should_be_a_resource_collection_route(name, prefix, params = {})
0
-  it "should provide #{name} with an 'index' route" do
0
-    route_for("#{prefix}/#{name}").should have_route(params.merge(:action => "index", :controller => "#{name}"))
0
-  end
0
-  
0
-  it "should provide #{name} with an 'index' route when explicitly specified" do
0
-    route_for("#{prefix}/#{name}/index").should have_route(params.merge(:action => "index", :controller => "#{name}"))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'new' route" do
0
-    route_for("#{prefix}/#{name}/new").should have_route(params.merge(:action => "new", :controller => "#{name}"))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'create' route" do
0
-    route_for("#{prefix}/#{name}", :method => :post).should have_route(params.merge(:action => "create", :controller => "#{name}"))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'show' route" do
0
-    route_for("#{prefix}/#{name}/45").should have_route(params.merge(:action => "show", :controller => "#{name}", :id => "45"))
0
-  end
0
-  
0
-  it "should provide #{name} with an 'edit' route" do
0
-    route_for("#{prefix}/#{name}/45/edit").should have_route(params.merge(:action => "edit", :controller => "#{name}", :id => "45"))
0
-  end
0
-  
0
-  it "should provide #{name} with an 'update' route" do
0
-    route_for("#{prefix}/#{name}/45", :method => :put).should have_route(params.merge(:action => "update", :controller => "#{name}", :id => "45"))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'delete' route" do
0
-    route_for("#{prefix}/#{name}/45/delete").should have_route(params.merge(:action => "delete", :controller => "#{name}", :id => "45"))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'destroy' route" do
0
-    route_for("#{prefix}/#{name}/45", :method => :delete).should have_route(params.merge(:action => "destroy", :controller => "#{name}", :id => "45"))
0
-  end
0
-  
0
-  # --- I decided that all the routes here will have the following ---
0
-  
0
-  it "should provide #{name} with a 'one' collection route" do
0
-    route_for("#{prefix}/#{name}/one").should have_route(params.merge(:action => "one", :controller => "#{name}"))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'two' member route" do
0
-    route_for("#{prefix}/#{name}/45/two").should have_route(params.merge(:action => "two", :controller => "#{name}", :id => "45"))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'three' collection route that maps the 'awesome' method" do
0
-    route_for("#{prefix}/#{name}/three").should have_route(params.merge(:action => "awesome", :controller => "#{name}"))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'four' member route that maps to the 'awesome' method" do
0
-    route_for("#{prefix}/#{name}/45/four").should have_route(params.merge(:action => "awesome", :controller => "#{name}", :id => "45"))
0
-  end
0
-end
0
-
0
-def it_should_be_a_resource_object_route(name, prefix, params = {})
0
-  controller = "#{name}s"
0
-  
0
-  it "should provide #{name} with a 'show' route" do
0
-    route_for("#{prefix}/#{name}").should have_route(params.merge(:action => "show", :controller => controller))
0
-  end
0
-  
0
-  it "should provide #{name} with an 'edit' route" do
0
-    route_for("#{prefix}/#{name}/edit").should have_route(params.merge(:action => "edit", :controller => controller))
0
-  end
0
-  
0
-  it "should provide #{name} with an 'update' route" do
0
-    route_for("#{prefix}/#{name}", :method => :put).should have_route(params.merge(:action => "update", :controller => controller))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'delete' route" do
0
-    route_for("#{prefix}/#{name}/delete").should have_route(params.merge(:action => "delete", :controller => controller))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'destroy' route" do
0
-    route_for("#{prefix}/#{name}", :method => :delete).should have_route(params.merge(:action => "destroy", :controller => controller))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'one' member route" do
0
-    route_for("#{prefix}/#{name}/one").should have_route(params.merge(:action => "one", :controller => controller))
0
-  end
0
-  
0
-  it "should provide #{name} with a 'two' member route that maps to the 'awesome' method" do
0
-    route_for("#{prefix}/#{name}/two").should have_route(params.merge(:action => "awesome", :controller => controller))
0
-  end
0
-end
0
-
0
 describe "Recognizing requests for nested resources routes" do
0
   
0
   before(:each) do
0
@@ -140,4 +52,54 @@ describe "Recognizing requests for nested resources routes" do
0
   it "should match a get to /domains/merbivore_com/emails to the emails controller and index action with domain => 'merbivore_com" do
0
     route_for('/domains/merbivore_com/emails', :method => :get).should have_route(:controller => 'emails', :action => 'index', :username => nil, :domain => 'merbivore_com')
0
   end
0
+  
0
+end
0
+
0
+describe "Recognizing requests for nested resources routes with custom matchers" do
0
+  
0
+  it "should convert the :id condition to :user_id" do
0
+    Merb::Router.prepare do
0
+      resources :users, :id => /[a-z]+/ do
0
+        resources :comments
0
+      end
0
+    end
0
+    
0
+    route_for("/users/abc/comments/1").should have_route(:user_id => "abc")
0
+    lambda { route_for('/users/123/comments/1') }.should raise_not_found
0
+  end
0
+  
0
+  it "should leave single keys not named :id alone" do
0
+    Merb::Router.prepare do
0
+      resources :users, :key => :name, :name => /[a-z]+/ do
0
+        resources :comments
0
+      end
0
+    end
0
+    
0
+    route_for("/users/abc/comments/1").should have_route(:name => "abc")
0
+    lambda { route_for('/users/123/comments/1') }.should raise_not_found
0
+  end
0
+  
0
+  it "should work with multi-key resources that have an :id as part of the identifier" do
0
+    Merb::Router.prepare do
0
+      resources :users, :key => [:name, :id], :id => /[a-z]+/ do
0
+        resources :comments
0
+      end
0
+    end
0
+    
0
+    route_for("/users/abc/efg/comments/1").should have_route(:name => "abc", :user_id => "efg")
0
+    lambda { route_for('/users/abc/123/comments/1') }.should raise_not_found
0
+  end
0
+  
0
+  it "should work with mult-key resources" do
0
+    Merb::Router.prepare do
0
+      resources :users, :key => [:first, :last], :first => /[a-z]+/, :last => /[a-z]+\/[a-z]+/ do
0
+        resources :comments
0
+      end
0
+    end
0
+    
0
+    route_for("/users/abc/efg/hij/comments/1").should have_route(:first => "abc", :last => "efg/hij")
0
+    lambda { route_for('/users/abc/123/comments/1') }.should raise_not_found
0
+    lambda { route_for('/users/abc/efg/comments/1') }.should raise_not_found
0
+  end
0
+  
0
 end
0
\ No newline at end of file
...
1
2
3
4
5
6
 
 
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
 
 
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
65
66
67
68
69
70
71
72
73
74
75
...
77
78
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
81
82
...
267
268
269
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
271
272
...
1
2
3
 
 
4
5
6
7
8
9
10
11
 
 
 
 
 
 
 
 
 
 
 
 
 
12
13
14
15
16
17
18
19
20
 
 
 
 
 
21
22
23
24
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
27
28
...
30
31
32
 
 
 
 
 
33
34
35
...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
0
@@ -1,63 +1,28 @@
0
 require File.join(File.dirname(__FILE__), "..", "spec_helper")
0
 
0
 describe "When recognizing requests," do
0
-
0
-  describe "a basic resource collection route" do
0
   
0
+  describe "a basic resource collection route" do
0
+    
0
     before :each do
0
       Merb::Router.prepare do
0
         resources :blogposts
0
       end
0
     end
0
-  
0
-    it "should have an index action with an optional :format" do
0
-      route_for('/blogposts').should           have_route(:controller => 'blogposts', :action => 'index', :id => nil, :format => nil)
0
-      route_for('/blogposts/index').should     have_route(:controller => 'blogposts', :action => 'index', :id => nil, :format => nil)
0
-      route_for('/blogposts.js').should        have_route(:controller => 'blogposts', :action => 'index', :id => nil, :format => "js")
0
-      route_for('/blogposts/index.xml').should have_route(:controller => 'blogposts', :action => 'index', :id => nil, :format => "xml")
0
-    end
0
-  
0
-    it "should have a create action with an optional :format" do
0
-      route_for('/blogposts',    :method => :post).should have_route(:controller => 'blogposts', :action => 'create', :id => nil, :format => nil)
0
-      route_for('/blogposts.js', :method => :post).should have_route(:controller => 'blogposts', :action => 'create', :id => nil, :format => "js")
0
-    end
0
-
0
+    
0
+    it_should_be_a_resource_collection_route :blogposts, { :extra => false }, { }
0
+    
0
     it "should not match put or delete on the collection" do
0
       [:put, :delete].each do |method|
0
         lambda { route_for('/blogposts',    :method => method) }.should raise_not_found
0
         lambda { route_for('/blogposts.js', :method => method) }.should raise_not_found
0
       end
0
     end
0
-  
0
-    it "should have a new action with an optional :format" do
0
-      route_for('/blogposts/new',    :method => :get).should have_route(:controller => 'blogposts', :action => 'new', :id => nil, :format => nil)
0
-      route_for('/blogposts/new.js', :method => :get).should have_route(:controller => 'blogposts', :action => 'new', :id => nil, :format => "js")
0
-    end
0
     
0
     it "should not match post on the new action" do
0
       lambda { route_for('/blogposts/new',     :method => :post) }.should raise_not_found
0
       lambda { route_for('/blogposts/new.xml', :method => :post) }.should raise_not_found
0
     end
0
-  
0
-    it "should have a show action with an optional :format" do
0
-      route_for('/blogposts/1',     :method => :get).should have_route(:controller => 'blogposts', :action => 'show', :id => "1", :format => nil)
0
-      route_for('/blogposts/1.css', :method => :get).should have_route(:controller => 'blogposts', :action => 'show', :id => "1", :format => "css")
0
-    end
0
-  
0
-    it "should have an update action with an optional :format" do
0
-      route_for('/blogposts/1',     :method => :put).should have_route(:controller => 'blogposts', :action => 'update', :id => "1", :format => nil)
0
-      route_for('/blogposts/1.csv', :method => :put).should have_route(:controller => 'blogposts', :action => 'update', :id => "1", :format => "csv")
0
-    end
0
-  
0
-    it "should have a destroy action with an optional :format" do
0
-      route_for('/blogposts/1',     :method => :delete).should have_route(:controller => 'blogposts', :action => 'destroy', :id => "1", :format => nil)
0
-      route_for('/blogposts/1.xxl', :method => :delete).should have_route(:controller => 'blogposts', :action => 'destroy', :id => "1", :format => 'xxl')
0
-    end
0
-
0
-    it "should have an edit action with an optional :format" do
0
-      route_for('/blogposts/1/edit',     :method => :get).should have_route(:controller => 'blogposts', :action => 'edit', :id => "1", :format => nil)
0
-      route_for('/blogposts/1/edit.rss', :method => :get).should have_route(:controller => 'blogposts', :action => 'edit', :id => "1", :format => "rss")
0
-    end
0
     
0
     it "should not match post, put, or delete on the edit action" do
0
       [:put, :post, :delete].each do |method|
0
@@ -65,11 +30,6 @@ describe "When recognizing requests," do
0
         lambda { route_for('/blogposts/edit.hi', :method => :post) }.should raise_not_found
0
       end
0
     end
0
-  
0
-    it "should should have a delete action with an optional :format" do
0
-      route_for('/blogposts/1/delete',     :method => :get).should have_route(:controller => 'blogposts', :action => 'delete', :id => "1", :format => nil)
0
-      route_for('/blogposts/1/delete.mp3', :method => :get).should have_route(:controller => 'blogposts', :action => 'delete', :id => "1", :format => "mp3")
0
-    end
0
     
0
     it "should not match post, put, or delete on the delete action" do
0
       [:put, :post, :delete].each do |method|
0
@@ -77,6 +37,27 @@ describe "When recognizing requests," do
0
         lambda { route_for('/blogposts/delete.flv', :method => :post) }.should raise_not_found
0
       end
0
     end
0
+    
0
+  end
0
+  
0
+  describe "a basic resource collection with custom " do
0
+    
0
+    before :each do
0
+      Merb::Router.prepare do
0
+        resources :blogposts, :id => %r([a-z]+/\d+)
0
+      end
0
+    end
0
+    
0
+    it_should_be_a_resource_collection_route :blogposts, { :extra => false, :id => "abc/123" }, { }
0
+    
0
+    it "should not match a numeric ID for the routes" do
0
+      lambda { route_for("/blogposts/10") }.should                     raise_not_found
0
+      lambda { route_for("/blogposts/10", :method => :put) }.should    raise_not_found
0
+      lambda { route_for("/blogposts/10", :method => :delete) }.should raise_not_found
0
+      lambda { route_for("/blogposts/10/edit") }.should                raise_not_found
0
+      lambda { route_for("/blogposts/10/delete") }.should              raise_not_found
0
+    end
0
+    
0
   end
0
   
0
   describe "a customized resource collection route" do
0
@@ -267,6 +248,28 @@ describe "When recognizing requests," do
0
     it "should match a get to /emails/bidule/merbivore_com/delete to the emails controller and the delete action with username => 'bidule', domain => 'merbivore_com'" do
0
       route_for('/emails/bidule/merbivore_com/delete', :method => :get).should have_route(:controller => 'emails', :action => 'delete', :username => "bidule", :domain => "merbivore_com")
0
     end
0
+    
0
+    it "should be able to set matches on each key" do
0
+      Merb::Router.prepare do
0
+        resources :emails, :keys => ["username", "domain"], :username => /[a-z]+/, :domain => /[a-z]+\.com/
0
+      end
0
+      
0
+      route_for("/emails/abc/abc.com").should have_route(:username => "abc", :domain => "abc.com")
0
+      lambda { route_for("/emails/123/456")     }.should raise_not_found
0
+      lambda { route_for("/emails/abc/123")     }.should raise_not_found
0
+      lambda { route_for("/emails/123/abc.com") }.should raise_not_found
0
+    end
0
+    
0
+    it "should be able to set matches on a single key" do
0
+      Merb::Router.prepare do
0
+        resources :emails, :keys => ["username", "domain"], :username => /[a-z]+/
0
+      end
0
+      
0
+      route_for("/emails/abc/123").should have_route(:username => "abc", :domain => "123")
0
+      route_for("/emails/abc/abc").should have_route(:username => "abc", :domain => "abc")
0
+      lambda { route_for("/emails/123/456") }.should raise_not_found
0
+      lambda { route_for("/emails/123/abc") }.should raise_not_found
0
+    end
0
  
0
   end
0
   
...
157
158
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
161
162
...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
0
@@ -157,6 +157,123 @@ module Spec
0
   end
0
 end
0
 
0
+def it_should_be_a_resource_collection_route(name, *args)
0
+  params = extract_options_from_args!(args) || {}
0
+  prefix = args.first.is_a?(String) ? args.shift : ""
0
+  opts   = args.first.is_a?(Hash)   ? args.shift : {}
0
+  
0
+  id = opts[:id] || "45"
0
+  
0
+  it "should provide #{name} with an 'index' route" do
0
+    route_for("#{prefix}/#{name}").should          have_route({:action => "index", :controller => "#{name}",  :id => nil, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/index").should    have_route({:action => "index", :controller => "#{name}",  :id => nil, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}.js").should       have_route({:action => "index", :controller => "#{name}",  :id => nil, :format => "js"}.merge(params))
0
+    route_for("#{prefix}/#{name}/index.js").should have_route({:action => "index", :controller => "#{name}",  :id => nil, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with a 'new' route" do
0
+    route_for("#{prefix}/#{name}/new").should    have_route({:action => "new", :controller => "#{name}", :id => nil, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/new.js").should have_route({:action => "new", :controller => "#{name}", :id => nil, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with a 'create' route" do
0
+    route_for("#{prefix}/#{name}",    :method => :post).should have_route({:action => "create", :controller => "#{name}", :id => nil, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}.js", :method => :post).should have_route({:action => "create", :controller => "#{name}", :id => nil, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with a 'show' route" do
0
+    route_for("#{prefix}/#{name}/#{id}").should    have_route({:action => "show", :controller => "#{name}", :id => id, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/#{id}.js").should have_route({:action => "show", :controller => "#{name}", :id => id, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with an 'edit' route" do
0
+    route_for("#{prefix}/#{name}/#{id}/edit").should    have_route({:action => "edit", :controller => "#{name}", :id => id, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/#{id}/edit.js").should have_route({:action => "edit", :controller => "#{name}", :id => id, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with an 'update' route" do
0
+    route_for("#{prefix}/#{name}/#{id}",    :method => :put).should have_route({:action => "update", :controller => "#{name}", :id => id, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/#{id}.js", :method => :put).should have_route({:action => "update", :controller => "#{name}", :id => id, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with a 'delete' route" do
0
+    route_for("#{prefix}/#{name}/#{id}/delete").should    have_route({:action => "delete", :controller => "#{name}", :id => id, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/#{id}/delete.js").should have_route({:action => "delete", :controller => "#{name}", :id => id, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with a 'destroy' route" do
0
+    route_for("#{prefix}/#{name}/#{id}",    :method => :delete).should have_route({:action => "destroy", :controller => "#{name}", :id => id, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/#{id}.js", :method => :delete).should have_route({:action => "destroy", :controller => "#{name}", :id => id, :format => "js"}.merge(params))
0
+  end
0
+
0
+  # --- I decided that all the routes here will have the following ---
0
+  
0
+  if !opts.has_key?(:extra) || opts[:extra]
0
+    
0
+    it "should provide #{name} with a 'one' collection route" do
0
+      route_for("#{prefix}/#{name}/one").should    have_route({:action => "one", :controller => "#{name}", :format => nil }.merge(params))
0
+      route_for("#{prefix}/#{name}/one.js").should have_route({:action => "one", :controller => "#{name}", :format => "js"}.merge(params))
0
+    end
0
+
0
+    it "should provide #{name} with a 'two' member route" do
0
+      route_for("#{prefix}/#{name}/#{id}/two").should    have_route({:action => "two", :controller => "#{name}", :id => id, :format => nil }.merge(params))
0
+      route_for("#{prefix}/#{name}/#{id}/two.js").should have_route({:action => "two", :controller => "#{name}", :id => id, :format => "js"}.merge(params))
0
+    end
0
+
0
+    it "should provide #{name} with a 'three' collection route that maps the 'awesome' method" do
0
+      route_for("#{prefix}/#{name}/three").should    have_route({:action => "awesome", :controller => "#{name}", :format => nil }.merge(params))
0
+      route_for("#{prefix}/#{name}/three.js").should have_route({:action => "awesome", :controller => "#{name}", :format => "js"}.merge(params))
0
+    end
0
+
0
+    it "should provide #{name} with a 'four' member route that maps to the 'awesome' method" do
0
+      route_for("#{prefix}/#{name}/#{id}/four").should    have_route({:action => "awesome", :controller => "#{name}", :id => id, :format => nil }.merge(params))
0
+      route_for("#{prefix}/#{name}/#{id}/four.js").should have_route({:action => "awesome", :controller => "#{name}", :id => id, :format => "js"}.merge(params))
0
+    end
0
+  end
0
+end
0
+
0
+def it_should_be_a_resource_object_route(name, *args)
0
+  controller = "#{name}s"
0
+  params     = extract_options_from_args!(args) || {}
0
+  prefix     = args.first.is_a?(String) ? args.shift : ""
0
+  opts       = args.first.is_a?(Hash)   ? args.shift : {}
0
+
0
+  it "should provide #{name} with a 'show' route" do
0
+    route_for("#{prefix}/#{name}").should    have_route({:action => "show", :controller => controller, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}.js").should have_route({:action => "show", :controller => controller, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with an 'edit' route" do
0
+    route_for("#{prefix}/#{name}/edit").should    have_route({:action => "edit", :controller => controller, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/edit.js").should have_route({:action => "edit", :controller => controller, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with an 'update' route" do
0
+    route_for("#{prefix}/#{name}",    :method => :put).should have_route({:action => "update", :controller => controller, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}.js", :method => :put).should have_route({:action => "update", :controller => controller, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with a 'delete' route" do
0
+    route_for("#{prefix}/#{name}/delete").should    have_route({:action => "delete", :controller => controller, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/delete.js").should have_route({:action => "delete", :controller => controller, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with a 'destroy' route" do
0
+    route_for("#{prefix}/#{name}",    :method => :delete).should have_route({:action => "destroy", :controller => controller, :format => nil}.merge(params))
0
+    route_for("#{prefix}/#{name}.js", :method => :delete).should have_route({:action => "destroy", :controller => controller, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with a 'one' member route" do
0
+    route_for("#{prefix}/#{name}/one").should    have_route({:action => "one", :controller => controller, :format => nil}.merge(params))
0
+    route_for("#{prefix}/#{name}/one.js").should have_route({:action => "one", :controller => controller, :format => "js"}.merge(params))
0
+  end
0
+
0
+  it "should provide #{name} with a 'two' member route that maps to the 'awesome' method" do
0
+    route_for("#{prefix}/#{name}/two").should    have_route({:action => "awesome", :controller => controller, :format => nil }.merge(params))
0
+    route_for("#{prefix}/#{name}/two.js").should have_route({:action => "awesome", :controller => controller, :format => "js"}.merge(params))
0
+  end
0
+end
0
+
0
 Spec::Runner.configure do |config|
0
   config.include(Spec::Helpers)
0
   config.include(Spec::Matchers)

Comments