rails_legacy_mapper 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,310 @@
1
+ require 'test_helper'
2
+
3
+ class RackMountIntegrationTest < ActiveSupport::TestCase
4
+ include RoutingTestHelpers
5
+
6
+ Model = Struct.new(:to_param)
7
+
8
+ Mapping = lambda { |map|
9
+ map.namespace :admin do |admin|
10
+ admin.resources :users
11
+ end
12
+
13
+ map.namespace 'api' do |api|
14
+ api.root :controller => 'users'
15
+ end
16
+
17
+ map.connect 'blog/:year/:month/:day',
18
+ :controller => 'posts',
19
+ :action => 'show_date',
20
+ :requirements => { :year => /(19|20)\d\d/, :month => /[01]?\d/, :day => /[0-3]?\d/},
21
+ :day => nil,
22
+ :month => nil
23
+
24
+ map.blog('archive/:year', :controller => 'archive', :action => 'index',
25
+ :defaults => { :year => nil },
26
+ :requirements => { :year => /\d{4}/ }
27
+ )
28
+
29
+ map.resources :people
30
+ map.connect 'legacy/people', :controller => 'people', :action => 'index', :legacy => 'true'
31
+
32
+ map.connect 'symbols', :controller => :symbols, :action => :show, :name => :as_symbol
33
+ map.connect 'id_default/:id', :controller => 'foo', :action => 'id_default', :id => 1
34
+ map.connect 'get_or_post', :controller => 'foo', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
35
+ map.connect 'optional/:optional', :controller => 'posts', :action => 'index'
36
+ map.project 'projects/:project_id', :controller => 'project'
37
+ map.connect 'clients', :controller => 'projects', :action => 'index'
38
+
39
+ map.connect 'ignorecase/geocode/:postalcode', :controller => 'geocode',
40
+ :action => 'show', :postalcode => /hx\d\d-\d[a-z]{2}/i
41
+ map.geocode 'extended/geocode/:postalcode', :controller => 'geocode',
42
+ :action => 'show',:requirements => {
43
+ :postalcode => /# Postcode format
44
+ \d{5} #Prefix
45
+ (-\d{4})? #Suffix
46
+ /x
47
+ }
48
+
49
+ map.connect '', :controller => 'news', :format => nil
50
+ map.connect 'news.:format', :controller => 'news'
51
+
52
+ map.connect 'comment/:id/:action', :controller => 'comments', :action => 'show'
53
+ map.connect 'ws/:controller/:action/:id', :ws => true
54
+ map.connect 'account/:action', :controller => :account, :action => :subscription
55
+ map.connect 'pages/:page_id/:controller/:action/:id'
56
+ map.connect ':controller/ping', :action => 'ping'
57
+ map.connect ':controller/:action/:id'
58
+ }
59
+
60
+ def setup
61
+ set.draw(&Mapping)
62
+ end
63
+
64
+ def test_add_route
65
+ set.clear!
66
+
67
+ assert_raise(ActionController::RoutingError) do
68
+ set.draw do |map|
69
+ map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
70
+ map.connect ':controller/:action/:id'
71
+ end
72
+ end
73
+ end
74
+
75
+ def test_recognize_path
76
+ assert_equal({:controller => 'admin/users', :action => 'index'}, recognize_path('/admin/users', :method => :get))
77
+ assert_equal({:controller => 'admin/users', :action => 'create'}, recognize_path('/admin/users', :method => :post))
78
+ assert_equal({:controller => 'admin/users', :action => 'new'}, recognize_path('/admin/users/new', :method => :get))
79
+ assert_equal({:controller => 'admin/users', :action => 'show', :id => '1'}, recognize_path('/admin/users/1', :method => :get))
80
+ assert_equal({:controller => 'admin/users', :action => 'update', :id => '1'}, recognize_path('/admin/users/1', :method => :put))
81
+ assert_equal({:controller => 'admin/users', :action => 'destroy', :id => '1'}, recognize_path('/admin/users/1', :method => :delete))
82
+ assert_equal({:controller => 'admin/users', :action => 'edit', :id => '1'}, recognize_path('/admin/users/1/edit', :method => :get))
83
+
84
+ assert_equal({:controller => 'admin/posts', :action => 'index'}, recognize_path('/admin/posts', :method => :get))
85
+ assert_equal({:controller => 'admin/posts', :action => 'new'}, recognize_path('/admin/posts/new', :method => :get))
86
+
87
+ assert_equal({:controller => 'api/users', :action => 'index'}, recognize_path('/api', :method => :get))
88
+ assert_equal({:controller => 'api/users', :action => 'index'}, recognize_path('/api/', :method => :get))
89
+
90
+ assert_equal({:controller => 'posts', :action => 'show_date', :year => '2009'}, recognize_path('/blog/2009', :method => :get))
91
+ assert_equal({:controller => 'posts', :action => 'show_date', :year => '2009', :month => '01'}, recognize_path('/blog/2009/01', :method => :get))
92
+ assert_equal({:controller => 'posts', :action => 'show_date', :year => '2009', :month => '01', :day => '01'}, recognize_path('/blog/2009/01/01', :method => :get))
93
+
94
+ assert_equal({:controller => 'archive', :action => 'index', :year => '2010'}, recognize_path('/archive/2010'))
95
+ assert_equal({:controller => 'archive', :action => 'index'}, recognize_path('/archive'))
96
+
97
+ assert_equal({:controller => 'people', :action => 'index'}, recognize_path('/people', :method => :get))
98
+ assert_equal({:controller => 'people', :action => 'index', :format => 'xml'}, recognize_path('/people.xml', :method => :get))
99
+ assert_equal({:controller => 'people', :action => 'create'}, recognize_path('/people', :method => :post))
100
+ assert_equal({:controller => 'people', :action => 'new'}, recognize_path('/people/new', :method => :get))
101
+ assert_equal({:controller => 'people', :action => 'show', :id => '1'}, recognize_path('/people/1', :method => :get))
102
+ assert_equal({:controller => 'people', :action => 'show', :id => '1', :format => 'xml'}, recognize_path('/people/1.xml', :method => :get))
103
+ assert_equal({:controller => 'people', :action => 'update', :id => '1'}, recognize_path('/people/1', :method => :put))
104
+ assert_equal({:controller => 'people', :action => 'destroy', :id => '1'}, recognize_path('/people/1', :method => :delete))
105
+ assert_equal({:controller => 'people', :action => 'edit', :id => '1'}, recognize_path('/people/1/edit', :method => :get))
106
+ assert_equal({:controller => 'people', :action => 'edit', :id => '1', :format => 'xml'}, recognize_path('/people/1/edit.xml', :method => :get))
107
+
108
+ assert_equal({:controller => 'symbols', :action => 'show', :name => :as_symbol}, recognize_path('/symbols'))
109
+ assert_equal({:controller => 'foo', :action => 'id_default', :id => '1'}, recognize_path('/id_default/1'))
110
+ assert_equal({:controller => 'foo', :action => 'id_default', :id => '2'}, recognize_path('/id_default/2'))
111
+ assert_equal({:controller => 'foo', :action => 'id_default', :id => '1'}, recognize_path('/id_default'))
112
+ assert_equal({:controller => 'foo', :action => 'get_or_post'}, recognize_path('/get_or_post', :method => :get))
113
+ assert_equal({:controller => 'foo', :action => 'get_or_post'}, recognize_path('/get_or_post', :method => :post))
114
+ assert_raise(ActionController::ActionControllerError) { recognize_path('/get_or_post', :method => :put) }
115
+ assert_raise(ActionController::ActionControllerError) { recognize_path('/get_or_post', :method => :delete) }
116
+
117
+ assert_equal({:controller => 'posts', :action => 'index', :optional => 'bar'}, recognize_path('/optional/bar'))
118
+ assert_raise(ActionController::ActionControllerError) { recognize_path('/optional') }
119
+
120
+ assert_equal({:controller => 'posts', :action => 'show', :id => '1', :ws => true}, recognize_path('/ws/posts/show/1', :method => :get))
121
+ assert_equal({:controller => 'posts', :action => 'list', :ws => true}, recognize_path('/ws/posts/list', :method => :get))
122
+ assert_equal({:controller => 'posts', :action => 'index', :ws => true}, recognize_path('/ws/posts', :method => :get))
123
+
124
+ assert_equal({:controller => 'account', :action => 'subscription'}, recognize_path('/account', :method => :get))
125
+ assert_equal({:controller => 'account', :action => 'subscription'}, recognize_path('/account/subscription', :method => :get))
126
+ assert_equal({:controller => 'account', :action => 'billing'}, recognize_path('/account/billing', :method => :get))
127
+
128
+ assert_equal({:page_id => '1', :controller => 'notes', :action => 'index'}, recognize_path('/pages/1/notes', :method => :get))
129
+ assert_equal({:page_id => '1', :controller => 'notes', :action => 'list'}, recognize_path('/pages/1/notes/list', :method => :get))
130
+ assert_equal({:page_id => '1', :controller => 'notes', :action => 'show', :id => '2'}, recognize_path('/pages/1/notes/show/2', :method => :get))
131
+
132
+ assert_equal({:controller => 'posts', :action => 'ping'}, recognize_path('/posts/ping', :method => :get))
133
+ assert_equal({:controller => 'posts', :action => 'index'}, recognize_path('/posts', :method => :get))
134
+ assert_equal({:controller => 'posts', :action => 'index'}, recognize_path('/posts/index', :method => :get))
135
+ assert_equal({:controller => 'posts', :action => 'show'}, recognize_path('/posts/show', :method => :get))
136
+ assert_equal({:controller => 'posts', :action => 'show', :id => '1'}, recognize_path('/posts/show/1', :method => :get))
137
+ assert_equal({:controller => 'posts', :action => 'create'}, recognize_path('/posts/create', :method => :post))
138
+
139
+ assert_equal({:controller => 'geocode', :action => 'show', :postalcode => 'hx12-1az'}, recognize_path('/ignorecase/geocode/hx12-1az'))
140
+ assert_equal({:controller => 'geocode', :action => 'show', :postalcode => 'hx12-1AZ'}, recognize_path('/ignorecase/geocode/hx12-1AZ'))
141
+ assert_equal({:controller => 'geocode', :action => 'show', :postalcode => '12345-1234'}, recognize_path('/extended/geocode/12345-1234'))
142
+ assert_equal({:controller => 'geocode', :action => 'show', :postalcode => '12345'}, recognize_path('/extended/geocode/12345'))
143
+
144
+ assert_equal({:controller => 'news', :action => 'index', :format => nil}, recognize_path('/', :method => :get))
145
+ assert_equal({:controller => 'news', :action => 'index', :format => 'rss'}, recognize_path('/news.rss', :method => :get))
146
+
147
+ assert_raise(ActionController::RoutingError) { recognize_path('/none', :method => :get) }
148
+ end
149
+
150
+ def test_generate
151
+ assert_equal '/admin/users', url_for(:use_route => 'admin_users')
152
+ assert_equal '/admin/users', url_for(:controller => 'admin/users')
153
+ assert_equal '/admin/users', url_for(:controller => 'admin/users', :action => 'index')
154
+ assert_equal '/admin/users', url_for({:action => 'index'}, {:controller => 'admin/users'})
155
+ assert_equal '/admin/users', url_for({:controller => 'users', :action => 'index'}, {:controller => 'admin/accounts'})
156
+ assert_equal '/people', url_for({:controller => '/people', :action => 'index'}, {:controller => 'admin/accounts'})
157
+
158
+ assert_equal '/admin/posts', url_for({:controller => 'admin/posts'})
159
+ assert_equal '/admin/posts/new', url_for({:controller => 'admin/posts', :action => 'new'})
160
+
161
+ assert_equal '/blog/2009', url_for(:controller => 'posts', :action => 'show_date', :year => 2009)
162
+ assert_equal '/blog/2009/1', url_for(:controller => 'posts', :action => 'show_date', :year => 2009, :month => 1)
163
+ assert_equal '/blog/2009/1/1', url_for(:controller => 'posts', :action => 'show_date', :year => 2009, :month => 1, :day => 1)
164
+
165
+ assert_equal '/archive/2010', url_for(:controller => 'archive', :action => 'index', :year => '2010')
166
+ assert_equal '/archive', url_for(:controller => 'archive', :action => 'index')
167
+ assert_equal '/archive?year=january', url_for(:controller => 'archive', :action => 'index', :year => 'january')
168
+
169
+ assert_equal '/people', url_for(:use_route => 'people')
170
+ assert_equal '/people', url_for(:use_route => 'people', :controller => 'people', :action => 'index')
171
+ assert_equal '/people.xml', url_for(:use_route => 'people', :controller => 'people', :action => 'index', :format => 'xml')
172
+ assert_equal '/people', url_for({:use_route => 'people', :controller => 'people', :action => 'index'}, {:controller => 'people', :action => 'index'})
173
+ assert_equal '/people', url_for(:controller => 'people')
174
+ assert_equal '/people', url_for(:controller => 'people', :action => 'index')
175
+ assert_equal '/people', url_for({:action => 'index'}, {:controller => 'people'})
176
+ assert_equal '/people', url_for({:action => 'index'}, {:controller => 'people', :action => 'show', :id => '1'})
177
+ assert_equal '/people', url_for({:controller => 'people', :action => 'index'}, {:controller => 'people', :action => 'show', :id => '1'})
178
+ assert_equal '/people', url_for({}, {:controller => 'people', :action => 'index'})
179
+ assert_equal '/people/1', url_for({:controller => 'people', :action => 'show'}, {:controller => 'people', :action => 'show', :id => '1'})
180
+ assert_equal '/people/new', url_for(:use_route => 'new_person')
181
+ assert_equal '/people/new', url_for(:controller => 'people', :action => 'new')
182
+ assert_equal '/people/1', url_for(:use_route => 'person', :id => '1')
183
+ assert_equal '/people/1', url_for(:controller => 'people', :action => 'show', :id => '1')
184
+ assert_equal '/people/1.xml', url_for(:controller => 'people', :action => 'show', :id => '1', :format => 'xml')
185
+ assert_equal '/people/1', url_for(:controller => 'people', :action => 'show', :id => 1)
186
+ assert_equal '/people/1', url_for(:controller => 'people', :action => 'show', :id => Model.new('1'))
187
+ assert_equal '/people/1', url_for({:action => 'show', :id => '1'}, {:controller => 'people', :action => 'index'})
188
+ assert_equal '/people/1', url_for({:action => 'show', :id => 1}, {:controller => 'people', :action => 'show', :id => '1'})
189
+ assert_equal '/people', url_for({:controller => 'people', :action => 'index'}, {:controller => 'people', :action => 'show', :id => '1'})
190
+ assert_equal '/people/1', url_for({}, {:controller => 'people', :action => 'show', :id => '1'})
191
+ assert_equal '/people/1', url_for({:controller => 'people', :action => 'show'}, {:controller => 'people', :action => 'index', :id => '1'})
192
+ assert_equal '/people/1/edit', url_for(:controller => 'people', :action => 'edit', :id => '1')
193
+ assert_equal '/people/1/edit.xml', url_for(:controller => 'people', :action => 'edit', :id => '1', :format => 'xml')
194
+ assert_equal '/people/1/edit', url_for(:use_route => 'edit_person', :id => '1')
195
+ assert_equal '/people/1?legacy=true', url_for(:controller => 'people', :action => 'show', :id => '1', :legacy => 'true')
196
+ assert_equal '/people?legacy=true', url_for(:controller => 'people', :action => 'index', :legacy => 'true')
197
+
198
+ assert_equal '/id_default/2', url_for(:controller => 'foo', :action => 'id_default', :id => '2')
199
+ assert_equal '/id_default', url_for(:controller => 'foo', :action => 'id_default', :id => '1')
200
+ assert_equal '/id_default', url_for(:controller => 'foo', :action => 'id_default', :id => 1)
201
+ assert_equal '/id_default', url_for(:controller => 'foo', :action => 'id_default')
202
+ assert_equal '/optional/bar', url_for(:controller => 'posts', :action => 'index', :optional => 'bar')
203
+ assert_equal '/posts', url_for(:controller => 'posts', :action => 'index')
204
+
205
+ assert_equal '/project', url_for({:controller => 'project', :action => 'index'})
206
+ assert_equal '/projects/1', url_for({:controller => 'project', :action => 'index', :project_id => '1'})
207
+ assert_equal '/projects/1', url_for({:controller => 'project', :action => 'index'}, {:project_id => '1'})
208
+ assert_raise(ActionController::RoutingError) { url_for({:use_route => 'project', :controller => 'project', :action => 'index'}) }
209
+ assert_equal '/projects/1', url_for({:use_route => 'project', :controller => 'project', :action => 'index', :project_id => '1'})
210
+ assert_equal '/projects/1', url_for({:use_route => 'project', :controller => 'project', :action => 'index'}, {:project_id => '1'})
211
+
212
+ assert_equal '/clients', url_for(:controller => 'projects', :action => 'index')
213
+ assert_equal '/clients?project_id=1', url_for(:controller => 'projects', :action => 'index', :project_id => '1')
214
+ assert_equal '/clients', url_for({:controller => 'projects', :action => 'index'}, {:project_id => '1'})
215
+ assert_equal '/clients', url_for({:action => 'index'}, {:controller => 'projects', :action => 'index', :project_id => '1'})
216
+
217
+ assert_equal '/comment/20', url_for({:id => 20}, {:controller => 'comments', :action => 'show'})
218
+ assert_equal '/comment/20', url_for(:controller => 'comments', :id => 20, :action => 'show')
219
+ assert_equal '/comments/boo', url_for(:controller => 'comments', :action => 'boo')
220
+
221
+ assert_equal '/ws/posts/show/1', url_for(:controller => 'posts', :action => 'show', :id => '1', :ws => true)
222
+ assert_equal '/ws/posts', url_for(:controller => 'posts', :action => 'index', :ws => true)
223
+
224
+ assert_equal '/account', url_for(:controller => 'account', :action => 'subscription')
225
+ assert_equal '/account/billing', url_for(:controller => 'account', :action => 'billing')
226
+
227
+ assert_equal '/pages/1/notes/show/1', url_for(:page_id => '1', :controller => 'notes', :action => 'show', :id => '1')
228
+ assert_equal '/pages/1/notes/list', url_for(:page_id => '1', :controller => 'notes', :action => 'list')
229
+ assert_equal '/pages/1/notes', url_for(:page_id => '1', :controller => 'notes', :action => 'index')
230
+ assert_equal '/pages/1/notes', url_for(:page_id => '1', :controller => 'notes')
231
+ assert_equal '/notes', url_for(:page_id => nil, :controller => 'notes')
232
+ assert_equal '/notes', url_for(:controller => 'notes')
233
+ assert_equal '/notes/print', url_for(:controller => 'notes', :action => 'print')
234
+ assert_equal '/notes/print', url_for({}, {:controller => 'notes', :action => 'print'})
235
+
236
+ assert_equal '/notes/index/1', url_for({:controller => 'notes'}, {:controller => 'notes', :id => '1'})
237
+ assert_equal '/notes/index/1', url_for({:controller => 'notes'}, {:controller => 'notes', :id => '1', :foo => 'bar'})
238
+ assert_equal '/notes/index/1', url_for({:controller => 'notes'}, {:controller => 'notes', :id => '1'})
239
+ assert_equal '/notes/index/1', url_for({:action => 'index'}, {:controller => 'notes', :id => '1'})
240
+ assert_equal '/notes/index/1', url_for({}, {:controller => 'notes', :id => '1'})
241
+ assert_equal '/notes/show/1', url_for({}, {:controller => 'notes', :action => 'show', :id => '1'})
242
+ assert_equal '/notes/index/1', url_for({:controller => 'notes', :id => '1'}, {:foo => 'bar'})
243
+ assert_equal '/posts', url_for({:controller => 'posts'}, {:controller => 'notes', :action => 'show', :id => '1'})
244
+ assert_equal '/notes/list', url_for({:action => 'list'}, {:controller => 'notes', :action => 'show', :id => '1'})
245
+
246
+ assert_equal '/posts/ping', url_for(:controller => 'posts', :action => 'ping')
247
+ assert_equal '/posts/show/1', url_for(:controller => 'posts', :action => 'show', :id => '1')
248
+ assert_equal '/posts', url_for(:controller => 'posts')
249
+ assert_equal '/posts', url_for(:controller => 'posts', :action => 'index')
250
+ assert_equal '/posts', url_for({:controller => 'posts'}, {:controller => 'posts', :action => 'index'})
251
+ assert_equal '/posts/create', url_for({:action => 'create'}, {:controller => 'posts'})
252
+ assert_equal '/posts?foo=bar', url_for(:controller => 'posts', :foo => 'bar')
253
+ assert_equal '/posts?foo%5B%5D=bar&foo%5B%5D=baz', url_for(:controller => 'posts', :foo => ['bar', 'baz'])
254
+ assert_equal '/posts?page=2', url_for(:controller => 'posts', :page => 2)
255
+ assert_equal '/posts?q%5Bfoo%5D%5Ba%5D=b', url_for(:controller => 'posts', :q => { :foo => { :a => 'b'}})
256
+
257
+ assert_equal '/', url_for(:controller => 'news', :action => 'index')
258
+ assert_equal '/', url_for(:controller => 'news', :action => 'index', :format => nil)
259
+ assert_equal '/news.rss', url_for(:controller => 'news', :action => 'index', :format => 'rss')
260
+
261
+ assert_raise(ActionController::RoutingError) { url_for({:action => 'index'}) }
262
+ end
263
+
264
+ def test_generate_extras
265
+ assert_equal ['/people', []], generate_extras(:controller => 'people')
266
+ assert_equal ['/people', [:foo]], generate_extras(:controller => 'people', :foo => 'bar')
267
+ assert_equal ['/people', []], generate_extras(:controller => 'people', :action => 'index')
268
+ assert_equal ['/people', [:foo]], generate_extras(:controller => 'people', :action => 'index', :foo => 'bar')
269
+ assert_equal ['/people/new', []], generate_extras(:controller => 'people', :action => 'new')
270
+ assert_equal ['/people/new', [:foo]], generate_extras(:controller => 'people', :action => 'new', :foo => 'bar')
271
+ assert_equal ['/people/1', []], generate_extras(:controller => 'people', :action => 'show', :id => '1')
272
+ assert_equal ['/people/1', [:bar, :foo]], sort_extras!(generate_extras(:controller => 'people', :action => 'show', :id => '1', :foo => '2', :bar => '3'))
273
+ assert_equal ['/people', [:person]], generate_extras(:controller => 'people', :action => 'create', :person => { :first_name => 'Josh', :last_name => 'Peek' })
274
+ assert_equal ['/people', [:people]], generate_extras(:controller => 'people', :action => 'create', :people => ['Josh', 'Dave'])
275
+
276
+ assert_equal ['/posts/show/1', []], generate_extras(:controller => 'posts', :action => 'show', :id => '1')
277
+ assert_equal ['/posts/show/1', [:bar, :foo]], sort_extras!(generate_extras(:controller => 'posts', :action => 'show', :id => '1', :foo => '2', :bar => '3'))
278
+ assert_equal ['/posts', []], generate_extras(:controller => 'posts', :action => 'index')
279
+ assert_equal ['/posts', [:foo]], generate_extras(:controller => 'posts', :action => 'index', :foo => 'bar')
280
+ end
281
+
282
+ def test_extras
283
+ params = {:controller => 'people'}
284
+ assert_equal [], extra_keys(params)
285
+ assert_equal({:controller => 'people'}, params)
286
+
287
+ params = {:controller => 'people', :foo => 'bar'}
288
+ assert_equal [:foo], extra_keys(params)
289
+ assert_equal({:controller => 'people', :foo => 'bar'}, params)
290
+
291
+ params = {:controller => 'people', :action => 'create', :person => { :name => 'Josh'}}
292
+ assert_equal [:person], extra_keys(params)
293
+ assert_equal({:controller => 'people', :action => 'create', :person => { :name => 'Josh'}}, params)
294
+ end
295
+
296
+ private
297
+ def sort_extras!(extras)
298
+ if extras.length == 2
299
+ extras[1].sort! { |a, b| a.to_s <=> b.to_s }
300
+ end
301
+ extras
302
+ end
303
+
304
+ def assert_raise(e)
305
+ result = yield
306
+ flunk "Did not raise #{e}, but returned #{result.inspect}"
307
+ rescue e
308
+ assert true
309
+ end
310
+ end
@@ -0,0 +1,1395 @@
1
+ require 'test_helper'
2
+ require 'active_support/core_ext/object/try'
3
+
4
+ class ResourcesController < ActionController::Base
5
+ def index() render :nothing => true end
6
+ alias_method :show, :index
7
+ def rescue_action(e) raise e end
8
+ end
9
+
10
+ class ThreadsController < ResourcesController; end
11
+ class MessagesController < ResourcesController; end
12
+ class CommentsController < ResourcesController; end
13
+ class AuthorsController < ResourcesController; end
14
+ class LogosController < ResourcesController; end
15
+
16
+ class AccountsController < ResourcesController; end
17
+ class AdminController < ResourcesController; end
18
+ class ProductsController < ResourcesController; end
19
+ class ImagesController < ResourcesController; end
20
+ class PreferencesController < ResourcesController; end
21
+
22
+ module Backoffice
23
+ class ProductsController < ResourcesController; end
24
+ class TagsController < ResourcesController; end
25
+ class ManufacturersController < ResourcesController; end
26
+ class ImagesController < ResourcesController; end
27
+
28
+ module Admin
29
+ class ProductsController < ResourcesController; end
30
+ class ImagesController < ResourcesController; end
31
+ end
32
+ end
33
+
34
+ class ResourcesTest < ActionController::TestCase
35
+ def test_should_arrange_actions
36
+ resource = RailsLegacyMapper::Mapper::Resource.new(:messages, {
37
+ :collection => { :rss => :get, :reorder => :post, :csv => :post },
38
+ :member => { :rss => :get, :atom => :get, :upload => :post, :fix => :post },
39
+ :new => { :preview => :get, :draft => :get }}, {})
40
+
41
+ assert_resource_methods [:rss], resource, :collection, :get
42
+ assert_resource_methods [:csv, :reorder], resource, :collection, :post
43
+ assert_resource_methods [:edit, :rss, :atom], resource, :member, :get
44
+ assert_resource_methods [:upload, :fix], resource, :member, :post
45
+ assert_resource_methods [:new, :preview, :draft], resource, :new, :get
46
+ end
47
+
48
+ def test_should_resource_controller_name_equal_resource_name_by_default
49
+ resource = RailsLegacyMapper::Mapper::Resource.new(:messages, {}, {})
50
+ assert_equal 'messages', resource.controller
51
+ end
52
+
53
+ def test_should_resource_controller_name_equal_controller_option
54
+ resource = RailsLegacyMapper::Mapper::Resource.new(:messages, {:controller => 'posts'}, {})
55
+ assert_equal 'posts', resource.controller
56
+ end
57
+
58
+ def test_should_all_singleton_paths_be_the_same
59
+ [ :path, :nesting_path_prefix, :member_path ].each do |method|
60
+ resource = RailsLegacyMapper::Mapper::SingletonResource.new(:messages, {:path_prefix => 'admin'}, {})
61
+ assert_equal 'admin/messages', resource.send(method)
62
+ end
63
+ end
64
+
65
+ def test_default_restful_routes
66
+ with_restful_routing :messages do
67
+ assert_simply_restful_for :messages
68
+ end
69
+ end
70
+
71
+ def test_override_paths_for_member_and_collection_methods
72
+ collection_methods = { 'rss' => :get, 'reorder' => :post, 'csv' => :post }
73
+ member_methods = { 'rss' => :get, :atom => :get, :upload => :post, :fix => :post }
74
+ path_names = {:new => 'nuevo', 'rss' => 'canal', :fix => 'corrigir' }
75
+
76
+ with_restful_routing :messages,
77
+ :collection => collection_methods,
78
+ :member => member_methods,
79
+ :path_names => path_names do
80
+
81
+ assert_restful_routes_for :messages,
82
+ :collection => collection_methods,
83
+ :member => member_methods,
84
+ :path_names => path_names do |options|
85
+ member_methods.each do |action, method|
86
+ assert_recognizes(options.merge(:action => action.to_s, :id => '1'),
87
+ :path => "/messages/1/#{path_names[action] || action}",
88
+ :method => method)
89
+ end
90
+
91
+ collection_methods.each do |action, method|
92
+ assert_recognizes(options.merge(:action => action),
93
+ :path => "/messages/#{path_names[action] || action}",
94
+ :method => method)
95
+ end
96
+ end
97
+
98
+ assert_restful_named_routes_for :messages,
99
+ :collection => collection_methods,
100
+ :member => member_methods,
101
+ :path_names => path_names do |options|
102
+
103
+ collection_methods.keys.each do |action|
104
+ assert_named_route "/messages/#{path_names[action] || action}", "#{action}_messages_path", :action => action
105
+ end
106
+
107
+ member_methods.keys.each do |action|
108
+ assert_named_route "/messages/1/#{path_names[action] || action}", "#{action}_message_path", :action => action, :id => "1"
109
+ end
110
+
111
+ end
112
+ end
113
+ end
114
+
115
+ def test_override_paths_for_default_restful_actions
116
+ resource = RailsLegacyMapper::Mapper::Resource.new(:messages, {
117
+ :path_names => {:new => 'nuevo', :edit => 'editar'}}, {})
118
+ assert_equal resource.new_path, "#{resource.path}/nuevo"
119
+ end
120
+
121
+ def test_multiple_default_restful_routes
122
+ with_restful_routing :messages, :comments do
123
+ assert_simply_restful_for :messages
124
+ assert_simply_restful_for :comments
125
+ end
126
+ end
127
+
128
+ def test_with_custom_conditions
129
+ with_restful_routing :messages, :conditions => { :subdomain => 'app' } do
130
+ assert @routes.recognize_path("/messages", :method => :get, :subdomain => 'app')
131
+ end
132
+ end
133
+
134
+ def test_irregular_id_with_no_requirements_should_raise_error
135
+ expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
136
+
137
+ with_restful_routing :messages do
138
+ assert_raise(ActionController::RoutingError) do
139
+ assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
140
+ end
141
+ end
142
+ end
143
+
144
+ def test_irregular_id_with_requirements_should_pass
145
+ expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
146
+
147
+ with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
148
+ assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
149
+ end
150
+ end
151
+
152
+ def test_with_path_prefix_requirements
153
+ expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
154
+ with_restful_routing :messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do
155
+ assert_recognizes(expected_options, :path => 'thread/1.1.1/messages/1', :method => :get)
156
+ end
157
+ end
158
+
159
+ def test_irregular_id_requirements_should_get_passed_to_member_actions
160
+ expected_options = {:controller => 'messages', :action => 'custom', :id => '1.1.1'}
161
+
162
+ with_restful_routing(:messages, :member => {:custom => :get}, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
163
+ assert_recognizes(expected_options, :path => 'messages/1.1.1/custom', :method => :get)
164
+ end
165
+ end
166
+
167
+ def test_with_path_prefix
168
+ with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do
169
+ assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
170
+ end
171
+ end
172
+
173
+ def test_multiple_with_path_prefix
174
+ with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do
175
+ assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
176
+ assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
177
+ end
178
+ end
179
+
180
+ def test_with_name_prefix
181
+ with_restful_routing :messages, :name_prefix => 'post_' do
182
+ assert_simply_restful_for :messages, :name_prefix => 'post_'
183
+ end
184
+ end
185
+
186
+ def test_with_collection_actions
187
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
188
+
189
+ with_restful_routing :messages, :collection => actions do
190
+ assert_restful_routes_for :messages do |options|
191
+ actions.each do |action, method|
192
+ assert_recognizes(options.merge(:action => action), :path => "/messages/#{action}", :method => method)
193
+ end
194
+ end
195
+
196
+ assert_restful_named_routes_for :messages do |options|
197
+ actions.keys.each do |action|
198
+ assert_named_route "/messages/#{action}", "#{action}_messages_path", :action => action
199
+ end
200
+ end
201
+ end
202
+ end
203
+
204
+ def test_with_collection_actions_and_name_prefix
205
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
206
+
207
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
208
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
209
+ actions.each do |action, method|
210
+ assert_recognizes(options.merge(:action => action), :path => "/threads/1/messages/#{action}", :method => method)
211
+ end
212
+ end
213
+
214
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
215
+ actions.keys.each do |action|
216
+ assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
217
+ end
218
+ end
219
+ end
220
+ end
221
+
222
+ def test_with_collection_actions_and_name_prefix_and_member_action_with_same_name
223
+ actions = { 'a' => :get }
224
+
225
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions, :member => actions do
226
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
227
+ actions.each do |action, method|
228
+ assert_recognizes(options.merge(:action => action), :path => "/threads/1/messages/#{action}", :method => method)
229
+ end
230
+ end
231
+
232
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
233
+ actions.keys.each do |action|
234
+ assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
235
+ end
236
+ end
237
+ end
238
+ end
239
+
240
+ def test_with_collection_action_and_name_prefix_and_formatted
241
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
242
+
243
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
244
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
245
+ actions.each do |action, method|
246
+ assert_recognizes(options.merge(:action => action, :format => 'xml'), :path => "/threads/1/messages/#{action}.xml", :method => method)
247
+ end
248
+ end
249
+
250
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
251
+ actions.keys.each do |action|
252
+ assert_named_route "/threads/1/messages/#{action}.xml", "#{action}_thread_messages_path", :action => action, :format => 'xml'
253
+ end
254
+ end
255
+ end
256
+ end
257
+
258
+ def test_with_member_action
259
+ [:put, :post].each do |method|
260
+ with_restful_routing :messages, :member => { :mark => method } do
261
+ mark_options = {:action => 'mark', :id => '1'}
262
+ mark_path = "/messages/1/mark"
263
+ assert_restful_routes_for :messages do |options|
264
+ assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
265
+ end
266
+
267
+ assert_restful_named_routes_for :messages do |options|
268
+ assert_named_route mark_path, :mark_message_path, mark_options
269
+ end
270
+ end
271
+ end
272
+ end
273
+
274
+ def test_with_member_action_and_requirement
275
+ expected_options = {:controller => 'messages', :action => 'mark', :id => '1.1.1'}
276
+
277
+ with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}, :member => { :mark => :get }) do
278
+ assert_recognizes(expected_options, :path => 'messages/1.1.1/mark', :method => :get)
279
+ end
280
+ end
281
+
282
+ def test_member_when_override_paths_for_default_restful_actions_with
283
+ [:put, :post].each do |method|
284
+ with_restful_routing :messages, :member => { :mark => method }, :path_names => {:new => 'nuevo'} do
285
+ mark_options = {:action => 'mark', :id => '1', :controller => "messages"}
286
+ mark_path = "/messages/1/mark"
287
+
288
+ assert_restful_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
289
+ assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
290
+ end
291
+
292
+ assert_restful_named_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
293
+ assert_named_route mark_path, :mark_message_path, mark_options
294
+ end
295
+ end
296
+ end
297
+ end
298
+
299
+ # def test_member_when_changed_default_restful_actions_and_path_names_not_specified
300
+ # default_path_names = ActionController::Base.resources_path_names
301
+ # ActionController::Base.resources_path_names = {:new => 'nuevo', :edit => 'editar'}
302
+ #
303
+ # with_restful_routing :messages do
304
+ # new_options = { :action => 'new', :controller => 'messages' }
305
+ # new_path = "/messages/nuevo"
306
+ # edit_options = { :action => 'edit', :id => '1', :controller => 'messages' }
307
+ # edit_path = "/messages/1/editar"
308
+ #
309
+ # assert_restful_routes_for :messages do |options|
310
+ # assert_recognizes(options.merge(new_options), :path => new_path, :method => :get)
311
+ # end
312
+ #
313
+ # assert_restful_routes_for :messages do |options|
314
+ # assert_recognizes(options.merge(edit_options), :path => edit_path, :method => :get)
315
+ # end
316
+ # end
317
+ # ensure
318
+ # ActionController::Base.resources_path_names = default_path_names
319
+ # end
320
+
321
+ def test_with_two_member_actions_with_same_method
322
+ [:put, :post].each do |method|
323
+ with_restful_routing :messages, :member => { :mark => method, :unmark => method } do
324
+ %w(mark unmark).each do |action|
325
+ action_options = {:action => action, :id => '1'}
326
+ action_path = "/messages/1/#{action}"
327
+ assert_restful_routes_for :messages do |options|
328
+ assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
329
+ end
330
+
331
+ assert_restful_named_routes_for :messages do |options|
332
+ assert_named_route action_path, "#{action}_message_path".to_sym, action_options
333
+ end
334
+ end
335
+ end
336
+ end
337
+ end
338
+
339
+ def test_array_as_collection_or_member_method_value
340
+ with_restful_routing :messages, :collection => { :search => [:get, :post] }, :member => { :toggle => [:get, :post] } do
341
+ assert_restful_routes_for :messages do |options|
342
+ [:get, :post].each do |method|
343
+ assert_recognizes(options.merge(:action => 'search'), :path => "/messages/search", :method => method)
344
+ end
345
+ [:get, :post].each do |method|
346
+ assert_recognizes(options.merge(:action => 'toggle', :id => '1'), :path => '/messages/1/toggle', :method => method)
347
+ end
348
+ end
349
+ end
350
+ end
351
+
352
+ def test_with_new_action
353
+ with_restful_routing :messages, :new => { :preview => :post } do
354
+ preview_options = {:action => 'preview'}
355
+ preview_path = "/messages/new/preview"
356
+ assert_restful_routes_for :messages do |options|
357
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
358
+ end
359
+
360
+ assert_restful_named_routes_for :messages do |options|
361
+ assert_named_route preview_path, :preview_new_message_path, preview_options
362
+ end
363
+ end
364
+ end
365
+
366
+ def test_with_new_action_with_name_prefix
367
+ with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
368
+ preview_options = {:action => 'preview', :thread_id => '1'}
369
+ preview_path = "/threads/1/messages/new/preview"
370
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
371
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
372
+ end
373
+
374
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
375
+ assert_named_route preview_path, :preview_new_thread_message_path, preview_options
376
+ end
377
+ end
378
+ end
379
+
380
+ def test_with_formatted_new_action_with_name_prefix
381
+ with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
382
+ preview_options = {:action => 'preview', :thread_id => '1', :format => 'xml'}
383
+ preview_path = "/threads/1/messages/new/preview.xml"
384
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
385
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
386
+ end
387
+
388
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
389
+ assert_named_route preview_path, :preview_new_thread_message_path, preview_options
390
+ end
391
+ end
392
+ end
393
+
394
+ def test_override_new_method
395
+ with_restful_routing :messages do
396
+ assert_restful_routes_for :messages do |options|
397
+ assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
398
+ assert_raise(ActionController::RoutingError) do
399
+ @routes.recognize_path("/messages/new", :method => :post)
400
+ end
401
+ end
402
+ end
403
+
404
+ with_restful_routing :messages, :new => { :new => :any } do
405
+ assert_restful_routes_for :messages do |options|
406
+ assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :post)
407
+ assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
408
+ end
409
+ end
410
+ end
411
+
412
+ def test_nested_restful_routes
413
+ with_routing do |set|
414
+ set.draw do |map|
415
+ map.resources :threads do |map|
416
+ map.resources :messages do |map|
417
+ map.resources :comments
418
+ end
419
+ end
420
+ end
421
+
422
+ assert_simply_restful_for :threads
423
+ assert_simply_restful_for :messages,
424
+ :name_prefix => 'thread_',
425
+ :path_prefix => 'threads/1/',
426
+ :options => { :thread_id => '1' }
427
+ assert_simply_restful_for :comments,
428
+ :name_prefix => 'thread_message_',
429
+ :path_prefix => 'threads/1/messages/2/',
430
+ :options => { :thread_id => '1', :message_id => '2' }
431
+ end
432
+ end
433
+
434
+ def test_nested_restful_routes_with_overwritten_defaults
435
+ with_routing do |set|
436
+ set.draw do |map|
437
+ map.resources :threads do |map|
438
+ map.resources :messages, :name_prefix => nil do |map|
439
+ map.resources :comments, :name_prefix => nil
440
+ end
441
+ end
442
+ end
443
+
444
+ assert_simply_restful_for :threads
445
+ assert_simply_restful_for :messages,
446
+ :path_prefix => 'threads/1/',
447
+ :options => { :thread_id => '1' }
448
+ assert_simply_restful_for :comments,
449
+ :path_prefix => 'threads/1/messages/2/',
450
+ :options => { :thread_id => '1', :message_id => '2' }
451
+ end
452
+ end
453
+
454
+ def test_shallow_nested_restful_routes
455
+ with_routing do |set|
456
+ set.draw do |map|
457
+ map.resources :threads, :shallow => true do |map|
458
+ map.resources :messages do |map|
459
+ map.resources :comments
460
+ end
461
+ end
462
+ end
463
+
464
+ assert_simply_restful_for :threads,
465
+ :shallow => true
466
+ assert_simply_restful_for :messages,
467
+ :name_prefix => 'thread_',
468
+ :path_prefix => 'threads/1/',
469
+ :shallow => true,
470
+ :options => { :thread_id => '1' }
471
+ assert_simply_restful_for :comments,
472
+ :name_prefix => 'message_',
473
+ :path_prefix => 'messages/2/',
474
+ :shallow => true,
475
+ :options => { :message_id => '2' }
476
+ end
477
+ end
478
+
479
+ def test_shallow_nested_restful_routes_with_namespaces
480
+ with_routing do |set|
481
+ set.draw do |map|
482
+ map.namespace :backoffice do |map|
483
+ map.namespace :admin do |map|
484
+ map.resources :products, :shallow => true do |map|
485
+ map.resources :images
486
+ end
487
+ end
488
+ end
489
+ end
490
+
491
+ assert_simply_restful_for :products,
492
+ :controller => 'backoffice/admin/products',
493
+ :namespace => 'backoffice/admin/',
494
+ :name_prefix => 'backoffice_admin_',
495
+ :path_prefix => 'backoffice/admin/',
496
+ :shallow => true
497
+ assert_simply_restful_for :images,
498
+ :controller => 'backoffice/admin/images',
499
+ :namespace => 'backoffice/admin/',
500
+ :name_prefix => 'backoffice_admin_product_',
501
+ :path_prefix => 'backoffice/admin/products/1/',
502
+ :shallow => true,
503
+ :options => { :product_id => '1' }
504
+ end
505
+ end
506
+
507
+ def test_restful_routes_dont_generate_duplicates
508
+ with_restful_routing :messages do
509
+ routes = @routes.routes
510
+ routes.each do |route|
511
+ routes.each do |r|
512
+ next if route === r # skip the comparison instance
513
+ assert distinct_routes?(route, r), "Duplicate Route: #{route}"
514
+ end
515
+ end
516
+ end
517
+ end
518
+
519
+ def test_should_create_singleton_resource_routes
520
+ with_singleton_resources :account do
521
+ assert_singleton_restful_for :account
522
+ end
523
+ end
524
+
525
+ def test_should_create_multiple_singleton_resource_routes
526
+ with_singleton_resources :account, :logo do
527
+ assert_singleton_restful_for :account
528
+ assert_singleton_restful_for :logo
529
+ end
530
+ end
531
+
532
+ def test_should_create_nested_singleton_resource_routes
533
+ with_routing do |set|
534
+ set.draw do |map|
535
+ map.resource :admin, :controller => 'admin' do |admin|
536
+ admin.resource :account
537
+ end
538
+ end
539
+
540
+ assert_singleton_restful_for :admin, :controller => 'admin'
541
+ assert_singleton_restful_for :account, :name_prefix => "admin_", :path_prefix => 'admin/'
542
+ end
543
+ end
544
+
545
+ def test_resource_has_many_should_become_nested_resources
546
+ with_routing do |set|
547
+ set.draw do |map|
548
+ map.resources :messages, :has_many => [ :comments, :authors ]
549
+ end
550
+
551
+ assert_simply_restful_for :messages
552
+ assert_simply_restful_for :comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
553
+ assert_simply_restful_for :authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
554
+ end
555
+ end
556
+
557
+ def test_resources_has_many_hash_should_become_nested_resources
558
+ with_routing do |set|
559
+ set.draw do |map|
560
+ map.resources :threads, :has_many => { :messages => [ :comments, { :authors => :threads } ] }
561
+ end
562
+
563
+ assert_simply_restful_for :threads
564
+ assert_simply_restful_for :messages, :name_prefix => "thread_", :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
565
+ assert_simply_restful_for :comments, :name_prefix => "thread_message_", :path_prefix => 'threads/1/messages/1/', :options => { :thread_id => '1', :message_id => '1' }
566
+ assert_simply_restful_for :authors, :name_prefix => "thread_message_", :path_prefix => 'threads/1/messages/1/', :options => { :thread_id => '1', :message_id => '1' }
567
+ assert_simply_restful_for :threads, :name_prefix => "thread_message_author_", :path_prefix => 'threads/1/messages/1/authors/1/', :options => { :thread_id => '1', :message_id => '1', :author_id => '1' }
568
+ end
569
+ end
570
+
571
+ def test_shallow_resource_has_many_should_become_shallow_nested_resources
572
+ with_routing do |set|
573
+ set.draw do |map|
574
+ map.resources :messages, :has_many => [ :comments, :authors ], :shallow => true
575
+ end
576
+
577
+ assert_simply_restful_for :messages, :shallow => true
578
+ assert_simply_restful_for :comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
579
+ assert_simply_restful_for :authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
580
+ end
581
+ end
582
+
583
+ def test_resource_has_one_should_become_nested_resources
584
+ with_routing do |set|
585
+ set.draw do |map|
586
+ map.resources :messages, :has_one => :logo
587
+ end
588
+
589
+ assert_simply_restful_for :messages
590
+ assert_singleton_restful_for :logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :options => { :message_id => '1' }
591
+ end
592
+ end
593
+
594
+ def test_shallow_resource_has_one_should_become_shallow_nested_resources
595
+ with_routing do |set|
596
+ set.draw do |map|
597
+ map.resources :messages, :has_one => :logo, :shallow => true
598
+ end
599
+
600
+ assert_simply_restful_for :messages, :shallow => true
601
+ assert_singleton_restful_for :logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
602
+ end
603
+ end
604
+
605
+ def test_singleton_resource_with_member_action
606
+ [:put, :post].each do |method|
607
+ with_singleton_resources :account, :member => { :reset => method } do
608
+ reset_options = {:action => 'reset'}
609
+ reset_path = "/account/reset"
610
+ assert_singleton_routes_for :account do |options|
611
+ assert_recognizes(options.merge(reset_options), :path => reset_path, :method => method)
612
+ end
613
+
614
+ assert_singleton_named_routes_for :account do |options|
615
+ assert_named_route reset_path, :reset_account_path, reset_options
616
+ end
617
+ end
618
+ end
619
+ end
620
+
621
+ def test_singleton_resource_with_two_member_actions_with_same_method
622
+ [:put, :post].each do |method|
623
+ with_singleton_resources :account, :member => { :reset => method, :disable => method } do
624
+ %w(reset disable).each do |action|
625
+ action_options = {:action => action}
626
+ action_path = "/account/#{action}"
627
+ assert_singleton_routes_for :account do |options|
628
+ assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
629
+ end
630
+
631
+ assert_singleton_named_routes_for :account do |options|
632
+ assert_named_route action_path, "#{action}_account_path".to_sym, action_options
633
+ end
634
+ end
635
+ end
636
+ end
637
+ end
638
+
639
+ def test_should_nest_resources_in_singleton_resource
640
+ with_routing do |set|
641
+ set.draw do |map|
642
+ map.resource :account do |account|
643
+ account.resources :messages
644
+ end
645
+ end
646
+
647
+ assert_singleton_restful_for :account
648
+ assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => 'account/'
649
+ end
650
+ end
651
+
652
+ def test_should_nest_resources_in_singleton_resource_with_path_prefix
653
+ with_routing do |set|
654
+ set.draw do |map|
655
+ map.resource(:account, :path_prefix => ':site_id') do |account|
656
+ account.resources :messages
657
+ end
658
+ end
659
+
660
+ assert_singleton_restful_for :account, :path_prefix => '7/', :options => { :site_id => '7' }
661
+ assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => '7/account/', :options => { :site_id => '7' }
662
+ end
663
+ end
664
+
665
+ def test_should_nest_singleton_resource_in_resources
666
+ with_routing do |set|
667
+ set.draw do |map|
668
+ map.resources :threads do |thread|
669
+ thread.resource :admin, :controller => 'admin'
670
+ end
671
+ end
672
+
673
+ assert_simply_restful_for :threads
674
+ assert_singleton_restful_for :admin, :controller => 'admin', :name_prefix => 'thread_', :path_prefix => 'threads/5/', :options => { :thread_id => '5' }
675
+ end
676
+ end
677
+
678
+ def test_should_not_allow_delete_or_put_on_collection_path
679
+ controller_name = :messages
680
+ with_restful_routing controller_name do
681
+ options = { :controller => controller_name.to_s }
682
+ collection_path = "/#{controller_name}"
683
+
684
+ assert_raise(ActionController::RoutingError) do
685
+ assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
686
+ end
687
+
688
+ assert_raise(ActionController::RoutingError) do
689
+ assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
690
+ end
691
+ end
692
+ end
693
+
694
+ def test_should_not_allow_invalid_head_method_for_member_routes
695
+ with_routing do |set|
696
+ assert_raise(ArgumentError) do
697
+ set.draw do |map|
698
+ map.resources :messages, :member => {:something => :head}
699
+ end
700
+ end
701
+ end
702
+ end
703
+
704
+ def test_should_not_allow_invalid_http_methods_for_member_routes
705
+ with_routing do |set|
706
+ assert_raise(ArgumentError) do
707
+ set.draw do |map|
708
+ map.resources :messages, :member => {:something => :invalid}
709
+ end
710
+ end
711
+ end
712
+ end
713
+
714
+ def test_resource_action_separator
715
+ with_routing do |set|
716
+ set.draw do |map|
717
+ map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
718
+ map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
719
+ end
720
+
721
+ action_separator = RailsLegacyMapper.resource_action_separator
722
+
723
+ assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
724
+ assert_named_route "/threads/1/messages#{action_separator}search", "search_thread_messages_path", {}
725
+ assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
726
+ assert_named_route "/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {}
727
+ assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
728
+ assert_named_route "/admin/account#{action_separator}login", "login_admin_account_path", {}
729
+ assert_named_route "/admin/account/new", "new_admin_account_path", {}
730
+ assert_named_route "/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {}
731
+ end
732
+ end
733
+
734
+ def test_new_style_named_routes_for_resource
735
+ with_routing do |set|
736
+ set.draw do |map|
737
+ map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
738
+ end
739
+ assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
740
+ assert_named_route "/threads/1/messages/search", "search_thread_messages_path", {}
741
+ assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
742
+ assert_named_route "/threads/1/messages/new/preview", "preview_new_thread_message_path", {}
743
+ end
744
+ end
745
+
746
+ def test_new_style_named_routes_for_singleton_resource
747
+ with_routing do |set|
748
+ set.draw do |map|
749
+ map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
750
+ end
751
+ assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
752
+ assert_named_route "/admin/account/login", "login_admin_account_path", {}
753
+ assert_named_route "/admin/account/new", "new_admin_account_path", {}
754
+ assert_named_route "/admin/account/new/preview", "preview_new_admin_account_path", {}
755
+ end
756
+ end
757
+
758
+ def test_resources_in_namespace
759
+ with_routing do |set|
760
+ set.draw do |map|
761
+ map.namespace :backoffice do |backoffice|
762
+ backoffice.resources :products
763
+ end
764
+ end
765
+
766
+ assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
767
+ end
768
+ end
769
+
770
+ def test_resource_has_many_in_namespace
771
+ with_routing do |set|
772
+ set.draw do |map|
773
+ map.namespace :backoffice do |backoffice|
774
+ backoffice.resources :products, :has_many => :tags
775
+ end
776
+ end
777
+
778
+ assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
779
+ assert_simply_restful_for :tags, :controller => "backoffice/tags", :name_prefix => "backoffice_product_", :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
780
+ end
781
+ end
782
+
783
+ def test_resource_has_one_in_namespace
784
+ with_routing do |set|
785
+ set.draw do |map|
786
+ map.namespace :backoffice do |backoffice|
787
+ backoffice.resources :products, :has_one => :manufacturer
788
+ end
789
+ end
790
+
791
+ assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
792
+ assert_singleton_restful_for :manufacturer, :controller => "backoffice/manufacturers", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
793
+ end
794
+ end
795
+
796
+ def test_resources_in_nested_namespace
797
+ with_routing do |set|
798
+ set.draw do |map|
799
+ map.namespace :backoffice do |backoffice|
800
+ backoffice.namespace :admin do |admin|
801
+ admin.resources :products
802
+ end
803
+ end
804
+ end
805
+
806
+ assert_simply_restful_for :products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
807
+ end
808
+ end
809
+
810
+ def test_resources_using_namespace
811
+ with_routing do |set|
812
+ set.draw do |map|
813
+ map.resources :products, :namespace => "backoffice/"
814
+ end
815
+
816
+ assert_simply_restful_for :products, :controller => "backoffice/products"
817
+ end
818
+ end
819
+
820
+ def test_nested_resources_using_namespace
821
+ with_routing do |set|
822
+ set.draw do |map|
823
+ map.namespace :backoffice do |backoffice|
824
+ backoffice.resources :products do |products|
825
+ products.resources :images
826
+ end
827
+ end
828
+ end
829
+
830
+ assert_simply_restful_for :images, :controller => "backoffice/images", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => {:product_id => '1'}
831
+ end
832
+ end
833
+
834
+ def test_nested_resources_in_nested_namespace
835
+ with_routing do |set|
836
+ set.draw do |map|
837
+ map.namespace :backoffice do |backoffice|
838
+ backoffice.namespace :admin do |admin|
839
+ admin.resources :products do |products|
840
+ products.resources :images
841
+ end
842
+ end
843
+ end
844
+ end
845
+
846
+ assert_simply_restful_for :images, :controller => "backoffice/admin/images", :name_prefix => 'backoffice_admin_product_', :path_prefix => 'backoffice/admin/products/1/', :options => {:product_id => '1'}
847
+ end
848
+ end
849
+
850
+ def test_with_path_segment
851
+ with_restful_routing :messages do
852
+ assert_simply_restful_for :messages
853
+ assert_recognizes({:controller => "messages", :action => "index"}, "/messages")
854
+ assert_recognizes({:controller => "messages", :action => "index"}, "/messages/")
855
+ end
856
+
857
+ with_restful_routing :messages, :as => 'reviews' do
858
+ assert_simply_restful_for :messages, :as => 'reviews'
859
+ assert_recognizes({:controller => "messages", :action => "index"}, "/reviews")
860
+ assert_recognizes({:controller => "messages", :action => "index"}, "/reviews/")
861
+ end
862
+ end
863
+
864
+ def test_multiple_with_path_segment_and_controller
865
+ with_routing do |set|
866
+ set.draw do |map|
867
+ map.resources :products do |products|
868
+ products.resources :product_reviews, :as => 'reviews', :controller => 'messages'
869
+ end
870
+ map.resources :tutors do |tutors|
871
+ tutors.resources :tutor_reviews, :as => 'reviews', :controller => 'comments'
872
+ end
873
+ end
874
+
875
+ assert_simply_restful_for :product_reviews, :controller=>'messages', :as => 'reviews', :name_prefix => 'product_', :path_prefix => 'products/1/', :options => {:product_id => '1'}
876
+ assert_simply_restful_for :tutor_reviews,:controller=>'comments', :as => 'reviews', :name_prefix => 'tutor_', :path_prefix => 'tutors/1/', :options => {:tutor_id => '1'}
877
+ end
878
+ end
879
+
880
+ def test_with_path_segment_path_prefix_requirements
881
+ expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
882
+ with_restful_routing :messages, :as => 'comments',:path_prefix => '/thread/:thread_id', :requirements => { :thread_id => /[0-9]\.[0-9]\.[0-9]/ } do
883
+ assert_recognizes(expected_options, :path => 'thread/1.1.1/comments/1', :method => :get)
884
+ end
885
+ end
886
+
887
+ def test_resource_has_only_show_action
888
+ with_routing do |set|
889
+ set.draw do |map|
890
+ map.resources :products, :only => :show
891
+ end
892
+
893
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, :show, [:index, :new, :create, :edit, :update, :destroy])
894
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :show, [:index, :new, :create, :edit, :update, :destroy])
895
+ end
896
+ end
897
+
898
+ def test_singleton_resource_has_only_show_action
899
+ with_routing do |set|
900
+ set.draw do |map|
901
+ map.resource :account, :only => :show
902
+ end
903
+
904
+ assert_singleton_resource_allowed_routes('accounts', {}, :show, [:index, :new, :create, :edit, :update, :destroy])
905
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :show, [:index, :new, :create, :edit, :update, :destroy])
906
+ end
907
+ end
908
+
909
+ def test_resource_does_not_have_destroy_action
910
+ with_routing do |set|
911
+ set.draw do |map|
912
+ map.resources :products, :except => :destroy
913
+ end
914
+
915
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, [:index, :new, :create, :show, :edit, :update], :destroy)
916
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [:index, :new, :create, :show, :edit, :update], :destroy)
917
+ end
918
+ end
919
+
920
+ def test_singleton_resource_does_not_have_destroy_action
921
+ with_routing do |set|
922
+ set.draw do |map|
923
+ map.resource :account, :except => :destroy
924
+ end
925
+
926
+ assert_singleton_resource_allowed_routes('accounts', {}, [:new, :create, :show, :edit, :update], :destroy)
927
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, [:new, :create, :show, :edit, :update], :destroy)
928
+ end
929
+ end
930
+
931
+ def test_resource_has_only_create_action_and_named_route
932
+ with_routing do |set|
933
+ set.draw do |map|
934
+ map.resources :products, :only => :create
935
+ end
936
+
937
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
938
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
939
+
940
+ assert_not_nil set.named_routes[:products]
941
+ end
942
+ end
943
+
944
+ def test_resource_has_only_update_action_and_named_route
945
+ with_routing do |set|
946
+ set.draw do |map|
947
+ map.resources :products, :only => :update
948
+ end
949
+
950
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
951
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
952
+
953
+ assert_not_nil set.named_routes[:product]
954
+ end
955
+ end
956
+
957
+ def test_resource_has_only_destroy_action_and_named_route
958
+ with_routing do |set|
959
+ set.draw do |map|
960
+ map.resources :products, :only => :destroy
961
+ end
962
+
963
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
964
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
965
+
966
+ assert_not_nil set.named_routes[:product]
967
+ end
968
+ end
969
+
970
+ def test_singleton_resource_has_only_create_action_and_named_route
971
+ with_routing do |set|
972
+ set.draw do |map|
973
+ map.resource :account, :only => :create
974
+ end
975
+
976
+ assert_singleton_resource_allowed_routes('accounts', {}, :create, [:new, :show, :edit, :update, :destroy])
977
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :create, [:new, :show, :edit, :update, :destroy])
978
+
979
+ assert_not_nil set.named_routes[:account]
980
+ end
981
+ end
982
+
983
+ def test_singleton_resource_has_only_update_action_and_named_route
984
+ with_routing do |set|
985
+ set.draw do |map|
986
+ map.resource :account, :only => :update
987
+ end
988
+
989
+ assert_singleton_resource_allowed_routes('accounts', {}, :update, [:new, :create, :show, :edit, :destroy])
990
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :update, [:new, :create, :show, :edit, :destroy])
991
+
992
+ assert_not_nil set.named_routes[:account]
993
+ end
994
+ end
995
+
996
+ def test_singleton_resource_has_only_destroy_action_and_named_route
997
+ with_routing do |set|
998
+ set.draw do |map|
999
+ map.resource :account, :only => :destroy
1000
+ end
1001
+
1002
+ assert_singleton_resource_allowed_routes('accounts', {}, :destroy, [:new, :create, :show, :edit, :update])
1003
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :destroy, [:new, :create, :show, :edit, :update])
1004
+
1005
+ assert_not_nil set.named_routes[:account]
1006
+ end
1007
+ end
1008
+
1009
+ def test_resource_has_only_collection_action
1010
+ with_routing do |set|
1011
+ set.draw do |map|
1012
+ map.resources :products, :except => :all, :collection => { :sale => :get }
1013
+ end
1014
+
1015
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
1016
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
1017
+
1018
+ assert_recognizes({ :controller => 'products', :action => 'sale' }, :path => 'products/sale', :method => :get)
1019
+ assert_recognizes({ :controller => 'products', :action => 'sale', :format => 'xml' }, :path => 'products/sale.xml', :method => :get)
1020
+ end
1021
+ end
1022
+
1023
+ def test_resource_has_only_member_action
1024
+ with_routing do |set|
1025
+ set.draw do |map|
1026
+ map.resources :products, :except => :all, :member => { :preview => :get }
1027
+ end
1028
+
1029
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
1030
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
1031
+
1032
+ assert_recognizes({ :controller => 'products', :action => 'preview', :id => '1' }, :path => 'products/1/preview', :method => :get)
1033
+ assert_recognizes({ :controller => 'products', :action => 'preview', :id => '1', :format => 'xml' }, :path => 'products/1/preview.xml', :method => :get)
1034
+ end
1035
+ end
1036
+
1037
+ def test_singleton_resource_has_only_member_action
1038
+ with_routing do |set|
1039
+ set.draw do |map|
1040
+ map.resource :account, :except => :all, :member => { :signup => :get }
1041
+ end
1042
+
1043
+ assert_singleton_resource_allowed_routes('accounts', {}, [], [:new, :create, :show, :edit, :update, :destroy])
1044
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, [], [:new, :create, :show, :edit, :update, :destroy])
1045
+
1046
+ assert_recognizes({ :controller => 'accounts', :action => 'signup' }, :path => 'account/signup', :method => :get)
1047
+ assert_recognizes({ :controller => 'accounts', :action => 'signup', :format => 'xml' }, :path => 'account/signup.xml', :method => :get)
1048
+ end
1049
+ end
1050
+
1051
+ def test_nested_resource_has_only_show_and_member_action
1052
+ with_routing do |set|
1053
+ set.draw do |map|
1054
+ map.resources :products, :only => [:index, :show] do |product|
1055
+ product.resources :images, :member => { :thumbnail => :get }, :only => :show
1056
+ end
1057
+ end
1058
+
1059
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
1060
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
1061
+
1062
+ assert_recognizes({ :controller => 'images', :action => 'thumbnail', :product_id => '1', :id => '2' }, :path => 'products/1/images/2/thumbnail', :method => :get)
1063
+ assert_recognizes({ :controller => 'images', :action => 'thumbnail', :product_id => '1', :id => '2', :format => 'jpg' }, :path => 'products/1/images/2/thumbnail.jpg', :method => :get)
1064
+ end
1065
+ end
1066
+
1067
+ def test_nested_resource_does_not_inherit_only_option
1068
+ with_routing do |set|
1069
+ set.draw do |map|
1070
+ map.resources :products, :only => :show do |product|
1071
+ product.resources :images, :except => :destroy
1072
+ end
1073
+ end
1074
+
1075
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
1076
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
1077
+ end
1078
+ end
1079
+
1080
+ def test_nested_resource_does_not_inherit_only_option_by_default
1081
+ with_routing do |set|
1082
+ set.draw do |map|
1083
+ map.resources :products, :only => :show do |product|
1084
+ product.resources :images
1085
+ end
1086
+ end
1087
+
1088
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destory], [], 'products/1/images')
1089
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
1090
+ end
1091
+ end
1092
+
1093
+ def test_nested_resource_does_not_inherit_except_option
1094
+ with_routing do |set|
1095
+ set.draw do |map|
1096
+ map.resources :products, :except => :show do |product|
1097
+ product.resources :images, :only => :destroy
1098
+ end
1099
+ end
1100
+
1101
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
1102
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
1103
+ end
1104
+ end
1105
+
1106
+ def test_nested_resource_does_not_inherit_except_option_by_default
1107
+ with_routing do |set|
1108
+ set.draw do |map|
1109
+ map.resources :products, :except => :show do |product|
1110
+ product.resources :images
1111
+ end
1112
+ end
1113
+
1114
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
1115
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
1116
+ end
1117
+ end
1118
+
1119
+ def test_default_singleton_restful_route_uses_get
1120
+ with_routing do |set|
1121
+ set.draw do |map|
1122
+ map.resource :product
1123
+ end
1124
+
1125
+ assert_routing '/product', :controller => 'products', :action => 'show'
1126
+ assert set.recognize_path("/product", :method => :get)
1127
+ end
1128
+ end
1129
+
1130
+ def test_singleton_resource_name_is_not_singularized
1131
+ with_singleton_resources(:preferences) do
1132
+ assert_singleton_restful_for :preferences
1133
+ end
1134
+ end
1135
+
1136
+ protected
1137
+ def with_restful_routing(*args)
1138
+ with_routing do |set|
1139
+ set.draw { |map| map.resources(*args) }
1140
+ yield
1141
+ end
1142
+ end
1143
+
1144
+ def with_singleton_resources(*args)
1145
+ with_routing do |set|
1146
+ set.draw { |map| map.resource(*args) }
1147
+ yield
1148
+ end
1149
+ end
1150
+
1151
+ # runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
1152
+ def assert_simply_restful_for(controller_name, options = {})
1153
+ assert_restful_routes_for controller_name, options
1154
+ assert_restful_named_routes_for controller_name, nil, options
1155
+ end
1156
+
1157
+ def assert_singleton_restful_for(singleton_name, options = {})
1158
+ assert_singleton_routes_for singleton_name, options
1159
+ assert_singleton_named_routes_for singleton_name, options
1160
+ end
1161
+
1162
+ def assert_restful_routes_for(controller_name, options = {})
1163
+ options[:options] ||= {}
1164
+ options[:options][:controller] = options[:controller] || controller_name.to_s
1165
+
1166
+ if options[:shallow]
1167
+ options[:shallow_options] ||= {}
1168
+ options[:shallow_options][:controller] = options[:options][:controller]
1169
+ else
1170
+ options[:shallow_options] = options[:options]
1171
+ end
1172
+
1173
+ new_action = @routes.resources_path_names[:new] || "new"
1174
+ edit_action = @routes.resources_path_names[:edit] || "edit"
1175
+
1176
+ if options[:path_names]
1177
+ new_action = options[:path_names][:new] if options[:path_names][:new]
1178
+ edit_action = options[:path_names][:edit] if options[:path_names][:edit]
1179
+ end
1180
+
1181
+ path = "#{options[:as] || controller_name}"
1182
+ collection_path = "/#{options[:path_prefix]}#{path}"
1183
+ shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
1184
+ member_path = "#{shallow_path}/1"
1185
+ new_path = "#{collection_path}/#{new_action}"
1186
+ edit_member_path = "#{member_path}/#{edit_action}"
1187
+ formatted_edit_member_path = "#{member_path}/#{edit_action}.xml"
1188
+
1189
+ with_options(options[:options]) do |controller|
1190
+ controller.assert_routing collection_path, :action => 'index'
1191
+ controller.assert_routing new_path, :action => 'new'
1192
+ controller.assert_routing "#{collection_path}.xml", :action => 'index', :format => 'xml'
1193
+ controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
1194
+ end
1195
+
1196
+ with_options(options[:shallow_options]) do |controller|
1197
+ controller.assert_routing member_path, :action => 'show', :id => '1'
1198
+ controller.assert_routing edit_member_path, :action => 'edit', :id => '1'
1199
+ controller.assert_routing "#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml'
1200
+ controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
1201
+ end
1202
+
1203
+ assert_recognizes(options[:options].merge(:action => 'index'), :path => collection_path, :method => :get)
1204
+ assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
1205
+ assert_recognizes(options[:options].merge(:action => 'create'), :path => collection_path, :method => :post)
1206
+ assert_recognizes(options[:shallow_options].merge(:action => 'show', :id => '1'), :path => member_path, :method => :get)
1207
+ assert_recognizes(options[:shallow_options].merge(:action => 'edit', :id => '1'), :path => edit_member_path, :method => :get)
1208
+ assert_recognizes(options[:shallow_options].merge(:action => 'update', :id => '1'), :path => member_path, :method => :put)
1209
+ assert_recognizes(options[:shallow_options].merge(:action => 'destroy', :id => '1'), :path => member_path, :method => :delete)
1210
+
1211
+ assert_recognizes(options[:options].merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
1212
+ assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
1213
+ assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
1214
+ assert_recognizes(options[:shallow_options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
1215
+ assert_recognizes(options[:shallow_options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
1216
+ assert_recognizes(options[:shallow_options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
1217
+ assert_recognizes(options[:shallow_options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
1218
+
1219
+ yield options[:options] if block_given?
1220
+ end
1221
+
1222
+ # test named routes like foo_path and foos_path map to the correct options.
1223
+ def assert_restful_named_routes_for(controller_name, singular_name = nil, options = {})
1224
+ if singular_name.is_a?(Hash)
1225
+ options = singular_name
1226
+ singular_name = nil
1227
+ end
1228
+ singular_name ||= controller_name.to_s.singularize
1229
+
1230
+ options[:options] ||= {}
1231
+ options[:options][:controller] = options[:controller] || controller_name.to_s
1232
+
1233
+ if options[:shallow]
1234
+ options[:shallow_options] ||= {}
1235
+ options[:shallow_options][:controller] = options[:options][:controller]
1236
+ else
1237
+ options[:shallow_options] = options[:options]
1238
+ end
1239
+
1240
+ @controller = "#{options[:options][:controller].camelize}Controller".constantize.new
1241
+ @controller.singleton_class.send(:include, @routes.url_helpers)
1242
+ @request = ActionController::TestRequest.new
1243
+ @response = ActionController::TestResponse.new
1244
+ get :index, options[:options]
1245
+ options[:options].delete :action
1246
+
1247
+ path = "#{options[:as] || controller_name}"
1248
+ shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
1249
+ full_path = "/#{options[:path_prefix]}#{path}"
1250
+ name_prefix = options[:name_prefix]
1251
+ shallow_prefix = options[:shallow] ? options[:namespace].try(:gsub, /\//, '_') : options[:name_prefix]
1252
+
1253
+ new_action = "new"
1254
+ edit_action = "edit"
1255
+ if options[:path_names]
1256
+ new_action = options[:path_names][:new] || "new"
1257
+ edit_action = options[:path_names][:edit] || "edit"
1258
+ end
1259
+
1260
+ assert_named_route "#{full_path}", "#{name_prefix}#{controller_name}_path", options[:options]
1261
+ assert_named_route "#{full_path}.xml", "#{name_prefix}#{controller_name}_path", options[:options].merge(:format => 'xml')
1262
+ assert_named_route "#{shallow_path}/1", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
1263
+ assert_named_route "#{shallow_path}/1.xml", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
1264
+
1265
+ assert_named_route "#{full_path}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", options[:options]
1266
+ assert_named_route "#{full_path}/#{new_action}.xml", "new_#{name_prefix}#{singular_name}_path", options[:options].merge(:format => 'xml')
1267
+ assert_named_route "#{shallow_path}/1/#{edit_action}", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
1268
+ assert_named_route "#{shallow_path}/1/#{edit_action}.xml", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
1269
+
1270
+ yield options[:options] if block_given?
1271
+ end
1272
+
1273
+ def assert_singleton_routes_for(singleton_name, options = {})
1274
+ options[:options] ||= {}
1275
+ options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize
1276
+
1277
+ full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
1278
+ new_path = "#{full_path}/new"
1279
+ edit_path = "#{full_path}/edit"
1280
+ formatted_edit_path = "#{full_path}/edit.xml"
1281
+
1282
+ with_options options[:options] do |controller|
1283
+ controller.assert_routing full_path, :action => 'show'
1284
+ controller.assert_routing new_path, :action => 'new'
1285
+ controller.assert_routing edit_path, :action => 'edit'
1286
+ controller.assert_routing "#{full_path}.xml", :action => 'show', :format => 'xml'
1287
+ controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
1288
+ controller.assert_routing formatted_edit_path, :action => 'edit', :format => 'xml'
1289
+ end
1290
+
1291
+ assert_recognizes(options[:options].merge(:action => 'show'), :path => full_path, :method => :get)
1292
+ assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
1293
+ assert_recognizes(options[:options].merge(:action => 'edit'), :path => edit_path, :method => :get)
1294
+ assert_recognizes(options[:options].merge(:action => 'create'), :path => full_path, :method => :post)
1295
+ assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put)
1296
+ assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete)
1297
+
1298
+ assert_recognizes(options[:options].merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
1299
+ assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
1300
+ assert_recognizes(options[:options].merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path, :method => :get)
1301
+ assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
1302
+ assert_recognizes(options[:options].merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
1303
+ assert_recognizes(options[:options].merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
1304
+
1305
+ yield options[:options] if block_given?
1306
+ end
1307
+
1308
+ def assert_singleton_named_routes_for(singleton_name, options = {})
1309
+ (options[:options] ||= {})[:controller] ||= singleton_name.to_s.pluralize
1310
+ @controller = "#{options[:options][:controller].camelize}Controller".constantize.new
1311
+ @controller.singleton_class.send(:include, @routes.url_helpers)
1312
+ @request = ActionController::TestRequest.new
1313
+ @response = ActionController::TestResponse.new
1314
+ get :show, options[:options]
1315
+ options[:options].delete :action
1316
+
1317
+ full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
1318
+ name_prefix = options[:name_prefix]
1319
+
1320
+ assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
1321
+ assert_named_route "#{full_path}.xml", "#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
1322
+
1323
+ assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options[:options]
1324
+ assert_named_route "#{full_path}/new.xml", "new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
1325
+ assert_named_route "#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options[:options]
1326
+ assert_named_route "#{full_path}/edit.xml", "edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
1327
+ end
1328
+
1329
+ def assert_named_route(expected, route, options)
1330
+ actual = @controller.send(route, options) rescue $!.class.name
1331
+ assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})"
1332
+ end
1333
+
1334
+ def assert_resource_methods(expected, resource, action_method, method)
1335
+ assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}"
1336
+ expected.each do |action|
1337
+ assert resource.send("#{action_method}_methods")[method].include?(action),
1338
+ "#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}"
1339
+ end
1340
+ end
1341
+
1342
+ def assert_resource_allowed_routes(controller, options, shallow_options, allowed, not_allowed, path = controller)
1343
+ shallow_path = "#{path}/#{shallow_options[:id]}"
1344
+ format = options[:format] && ".#{options[:format]}"
1345
+ options.merge!(:controller => controller)
1346
+ shallow_options.merge!(options)
1347
+
1348
+ assert_whether_allowed(allowed, not_allowed, options, 'index', "#{path}#{format}", :get)
1349
+ assert_whether_allowed(allowed, not_allowed, options, 'new', "#{path}/new#{format}", :get)
1350
+ assert_whether_allowed(allowed, not_allowed, options, 'create', "#{path}#{format}", :post)
1351
+ assert_whether_allowed(allowed, not_allowed, shallow_options, 'show', "#{shallow_path}#{format}", :get)
1352
+ assert_whether_allowed(allowed, not_allowed, shallow_options, 'edit', "#{shallow_path}/edit#{format}", :get)
1353
+ assert_whether_allowed(allowed, not_allowed, shallow_options, 'update', "#{shallow_path}#{format}", :put)
1354
+ assert_whether_allowed(allowed, not_allowed, shallow_options, 'destroy', "#{shallow_path}#{format}", :delete)
1355
+ end
1356
+
1357
+ def assert_singleton_resource_allowed_routes(controller, options, allowed, not_allowed, path = controller.singularize)
1358
+ format = options[:format] && ".#{options[:format]}"
1359
+ options.merge!(:controller => controller)
1360
+
1361
+ assert_whether_allowed(allowed, not_allowed, options, 'new', "#{path}/new#{format}", :get)
1362
+ assert_whether_allowed(allowed, not_allowed, options, 'create', "#{path}#{format}", :post)
1363
+ assert_whether_allowed(allowed, not_allowed, options, 'show', "#{path}#{format}", :get)
1364
+ assert_whether_allowed(allowed, not_allowed, options, 'edit', "#{path}/edit#{format}", :get)
1365
+ assert_whether_allowed(allowed, not_allowed, options, 'update', "#{path}#{format}", :put)
1366
+ assert_whether_allowed(allowed, not_allowed, options, 'destroy', "#{path}#{format}", :delete)
1367
+ end
1368
+
1369
+ def assert_whether_allowed(allowed, not_allowed, options, action, path, method)
1370
+ action = action.to_sym
1371
+ options = options.merge(:action => action.to_s)
1372
+ path_options = { :path => path, :method => method }
1373
+
1374
+ if Array(allowed).include?(action)
1375
+ assert_recognizes options, path_options
1376
+ elsif Array(not_allowed).include?(action)
1377
+ assert_not_recognizes options, path_options
1378
+ end
1379
+ end
1380
+
1381
+ def assert_not_recognizes(expected_options, path)
1382
+ assert_raise ActionController::RoutingError, Assertion do
1383
+ assert_recognizes(expected_options, path)
1384
+ end
1385
+ end
1386
+
1387
+ def distinct_routes? (r1, r2)
1388
+ if r1.conditions == r2.conditions and r1.requirements == r2.requirements then
1389
+ if r1.segments.collect(&:to_s) == r2.segments.collect(&:to_s) then
1390
+ return false
1391
+ end
1392
+ end
1393
+ true
1394
+ end
1395
+ end