GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

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
Added the ability to specify resource actions in the block

Custom resource actions can now be specified in the block passed to
Behavior#resources and Behavior#resource. Doing it this way allows
the action to be customized further than by specifying it in in the
options hash. The API is as follows:

Merb::Router.prepare do
  resources :users do
    collection :boom, :method => :get, :to => "boom_get"
    collection :boom, :method => :put, :to => "boom_put"
    member     :boom, :method => :put, :to => "boom_put"
  end
end

Also added the ability to customize resource actions on singular
resource routes. This works the same way as the plural method,
except that only #member is available.
carllerche (author)
Sat Oct 04 18:25:16 -0700 2008
commit  2d31e4b35f7698fc9667ccca5d5b3e9528486a90
tree    8ad7fccc514d67ad206c57028af1417bad085e50
parent  e35bcb69b95e2063ac45fa7d5695c6fdbaf16409
...
56
57
58
59
 
60
61
62
...
56
57
58
 
59
60
61
62
0
@@ -56,7 +56,7 @@ module Merb
0
       # Returns self to allow chaining of methods.
0
       def prepare(first = [], last = [], &block)
0
         @routes = []
0
- root_behavior.with_proxy(&block)
0
+ root_behavior._with_proxy(&block)
0
         @routes = first + @routes + last
0
         compile
0
         self
...
197
198
199
200
201
 
202
203
 
 
 
 
204
205
206
...
449
450
451
452
 
453
454
455
...
545
546
547
548
 
549
550
551
...
553
554
555
 
 
 
 
556
557
558
 
559
560
561
562
 
563
564
565
...
570
571
572
573
 
574
575
576
577
578
579
580
581
 
 
 
 
 
 
 
582
583
 
 
 
 
 
 
584
585
586
...
197
198
199
 
 
200
201
202
203
204
205
206
207
208
209
...
452
453
454
 
455
456
457
458
...
548
549
550
 
551
552
553
554
...
556
557
558
559
560
561
562
563
 
 
564
565
566
 
 
567
568
569
570
...
575
576
577
 
578
579
580
581
582
583
584
 
 
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
0
@@ -197,10 +197,13 @@ module Merb
0
       #---
0
       # @public
0
       def match(path = {}, conditions = {}, &block)
0
- path, conditions = path[:path], path if Hash === path
0
- conditions[:path] = merge_paths(path)
0
+ path, conditions = path[:path], path if path.is_a?(Hash)
0
 
0
         raise Error, "The route has already been committed. Further conditions cannot be specified" if @route
0
+
0
+
0
+ conditions.delete_if { |k, v| v.nil? }
0
+ conditions[:path] = merge_paths(path)
0
 
0
         behavior = Behavior.new(@proxy, @conditions.merge(conditions), @params, @defaults, @identifiers, @options, @blocks)
0
         with_behavior_context(behavior, &block)
0
@@ -449,7 +452,7 @@ module Merb
0
       #---
0
       # @public
0
       def defer_to(params = {}, &block)
0
- defer(block).to_route(params)
0
+ defer(block).to(params)
0
       end
0
       
0
       # Takes a Proc as a parameter and applies it as a deferred proc for all the
0
@@ -545,7 +548,7 @@ module Merb
0
       # So that Router can have a default route
0
       # ---
0
       # @private
0
- def with_proxy(&block) #:nodoc:
0
+ def _with_proxy(&block) #:nodoc:
0
         proxy = Proxy.new
0
         proxy.push Behavior.new(proxy, @conditions, @params, @defaults, @identifiers, @options, @blocks)
0
         proxy.instance_eval(&block)
0
@@ -553,13 +556,15 @@ module Merb
0
       end
0
       
0
     protected
0
+
0
+ def _route
0
+ @route
0
+ end
0
       
0
- def to_route(params = {}, &conditional_block) # :nodoc:
0
-
0
+ def to_route # :nodoc:
0
         raise Error, "The route has already been committed." if @route
0
 
0
- params = @params.merge(params)
0
- controller = params[:controller]
0
+ controller = @params[:controller]
0
 
0
         if prefixes = @options[:controller_prefix]
0
           controller ||= ":controller"
0
@@ -570,17 +575,28 @@ module Merb
0
           end
0
         end
0
         
0
- params.merge!(:controller => controller.to_s.gsub(%r{^/}, '')) if controller
0
+ @params.merge!(:controller => controller.to_s.gsub(%r{^/}, '')) if controller
0
         
0
         # Sorts the identifiers so that modules that are at the bottom of the
0
         # inheritance chain come first (more specific modules first). Object
0
         # should always be last.
0
         identifiers = @identifiers.sort { |(first,_),(sec,_)| first <=> sec || 1 }
0
         
0
- @route = Route.new(@conditions.dup, params, @blocks, :defaults => @defaults.dup, :identifiers => identifiers)
0
- @route.register
0
+ @route = Route.new(@conditions.dup,@params, @blocks, :defaults => @defaults.dup, :identifiers => identifiers)
0
+
0
+ if before = @options[:before] && @options[:before].last
0
+ @route.register_at(Router.routes.index(before))
0
+ else
0
+ @route.register
0
+ end
0
         self
0
       end
0
+
0
+ # Allows to insert the route at a certain spot in the list of routes
0
+ # instead of appending to the list.
0
+ def before(route, &block) #:nodoc:
0
+ options(:before => route, &block)
0
+ end
0
 
0
     private
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
94
95
96
 
 
 
 
 
 
97
98
99
100
101
102
103
 
 
 
 
 
 
104
105
106
107
 
 
 
108
109
110
111
112
113
114
 
 
 
 
 
 
115
116
117
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
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
...
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
94
95
96
 
 
 
 
 
 
97
98
99
100
101
102
103
 
 
 
104
105
106
107
 
 
 
 
 
 
108
109
110
111
112
113
114
 
 
 
 
 
 
 
115
116
117
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
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
280
281
282
283
284
285
286
287
288
289
290
0
@@ -1,220 +1,289 @@
0
 module Merb
0
   class Router
0
- class Behavior
0
- module Resources
0
-
0
- # Behavior#+resources+ is a route helper for defining a collection of
0
- # RESTful resources. It yields to a block for child routes.
0
- #
0
- # ==== Parameters
0
- # name<String, Symbol>:: The name of the resources
0
- # options<Hash>::
0
- # Ovverides and parameters to be associated with the route
0
- #
0
- # ==== Options (options)
0
- # :namespace<~to_s>: The namespace for this route.
0
- # :name_prefix<~to_s>:
0
- # A prefix for the named routes. If a namespace is passed and there
0
- # isn't a name prefix, the namespace will become the prefix.
0
- # :controller<~to_s>: The controller for this route
0
- # :collection<~to_s>: Special settings for the collections routes
0
- # :member<Hash>:
0
- # Special settings and resources related to a specific member of this
0
- # resource.
0
- # :keys<Array>:
0
- # A list of the keys to be used instead of :id with the resource in the order of the url.
0
- # :singular<Symbol>
0
- #
0
- # ==== Block parameters
0
- # next_level<Behavior>:: The child behavior.
0
- #
0
- # ==== Returns
0
- # Array::
0
- # Routes which will define the specified RESTful collection of resources
0
- #
0
- # ==== Examples
0
- #
0
- # r.resources :posts # will result in the typical RESTful CRUD
0
- # # lists resources
0
- # # GET /posts/?(\.:format)? :action => "index"
0
- # # GET /posts/index(\.:format)? :action => "index"
0
- #
0
- # # shows new resource form
0
- # # GET /posts/new :action => "new"
0
- #
0
- # # creates resource
0
- # # POST /posts/?(\.:format)?, :action => "create"
0
- #
0
- # # shows resource
0
- # # GET /posts/:id(\.:format)? :action => "show"
0
- #
0
- # # shows edit form
0
- # # GET /posts/:id/edit :action => "edit"
0
- #
0
- # # updates resource
0
- # # PUT /posts/:id(\.:format)? :action => "update"
0
- #
0
- # # shows deletion confirmation page
0
- # # GET /posts/:id/delete :action => "delete"
0
- #
0
- # # destroys resources
0
- # # DELETE /posts/:id(\.:format)? :action => "destroy"
0
- #
0
- # # Nesting resources
0
- # r.resources :posts do |posts|
0
- # posts.resources :comments
0
- # end
0
- #---
0
- # @public
0
- def resources(name, *args, &block)
0
- name = name.to_s
0
- options = extract_options_from_args!(args) || {}
0
- singular = options[:singular] ? options[:singular].to_s : Extlib::Inflection.singularize(name)
0
- klass = args.first ? args.first.to_s : Extlib::Inflection.classify(singular)
0
- keys = [ options.delete(:keys) || options.delete(:key) || :id ].flatten
0
- params = { :controller => options.delete(:controller) || name }
0
- collection = options.delete(:collection) || {}
0
- member = { :edit => :get, :delete => :get }.merge(options.delete(:member) || {})
0
+
0
+ module Resources
0
+ # Behavior#+resources+ is a route helper for defining a collection of
0
+ # RESTful resources. It yields to a block for child routes.
0
+ #
0
+ # ==== Parameters
0
+ # name<String, Symbol>:: The name of the resources
0
+ # options<Hash>::
0
+ # Ovverides and parameters to be associated with the route
0
+ #
0
+ # ==== Options (options)
0
+ # :namespace<~to_s>: The namespace for this route.
0
+ # :name_prefix<~to_s>:
0
+ # A prefix for the named routes. If a namespace is passed and there
0
+ # isn't a name prefix, the namespace will become the prefix.
0
+ # :controller<~to_s>: The controller for this route
0
+ # :collection<~to_s>: Special settings for the collections routes
0
+ # :member<Hash>:
0
+ # Special settings and resources related to a specific member of this
0
+ # resource.
0
+ # :keys<Array>:
0
+ # A list of the keys to be used instead of :id with the resource in the order of the url.
0
+ # :singular<Symbol>
0
+ #
0
+ # ==== Block parameters
0
+ # next_level<Behavior>:: The child behavior.
0
+ #
0
+ # ==== Returns
0
+ # Array::
0
+ # Routes which will define the specified RESTful collection of resources
0
+ #
0
+ # ==== Examples
0
+ #
0
+ # r.resources :posts # will result in the typical RESTful CRUD
0
+ # # lists resources
0
+ # # GET /posts/?(\.:format)? :action => "index"
0
+ # # GET /posts/index(\.:format)? :action => "index"
0
+ #
0
+ # # shows new resource form
0
+ # # GET /posts/new :action => "new"
0
+ #
0
+ # # creates resource
0
+ # # POST /posts/?(\.:format)?, :action => "create"
0
+ #
0
+ # # shows resource
0
+ # # GET /posts/:id(\.:format)? :action => "show"
0
+ #
0
+ # # shows edit form
0
+ # # GET /posts/:id/edit :action => "edit"
0
+ #
0
+ # # updates resource
0
+ # # PUT /posts/:id(\.:format)? :action => "update"
0
+ #
0
+ # # shows deletion confirmation page
0
+ # # GET /posts/:id/delete :action => "delete"
0
+ #
0
+ # # destroys resources
0
+ # # DELETE /posts/:id(\.:format)? :action => "destroy"
0
+ #
0
+ # # Nesting resources
0
+ # r.resources :posts do |posts|
0
+ # posts.resources :comments
0
+ # end
0
+ #---
0
+ # @public
0
+ def resources(name, *args, &block)
0
+ name = name.to_s
0
+ options = extract_options_from_args!(args) || {}
0
+ singular = options[:singular] ? options[:singular].to_s : Extlib::Inflection.singularize(name)
0
+ klass = args.first ? args.first.to_s : Extlib::Inflection.classify(singular)
0
+ keys = [ options.delete(:keys) || options.delete(:key) || :id ].flatten
0
+ params = { :controller => options.delete(:controller) || name }
0
+ collection = options.delete(:collection) || {}
0
+ member = { :edit => :get, :delete => :get }.merge(options.delete(:member) || {})
0
 
0
- # Try pulling :namespace out of options for backwards compatibility
0
- options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
0
- options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
0
- options[:controller_prefix] ||= options.delete(:namespace)
0
+ # Try pulling :namespace out of options for backwards compatibility
0
+ options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
0
+ options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
0
+ options[:controller_prefix] ||= options.delete(:namespace)
0
 
0
- self.namespace(name, options).to(params) do |resource|
0
- root_keys = keys.map { |k| ":#{k}" }.join("/")
0
-
0
- # => index
0
- resource.match("(/index)(.:format)", :method => :get).to(:action => "index").
0
- name(name).register_resource(name)
0
-
0
- # => create
0
- resource.match("(.:format)", :method => :post).to(:action => "create")
0
+ self.namespace(name, options).to(params) do |resource|
0
+ root_keys = keys.map { |k| ":#{k}" }.join("/")
0
+
0
+ # => index
0
+ resource.match("(/index)(.:format)", :method => :get).to(:action => "index").
0
+ name(name).register_resource(name)
0
             
0
- # => new
0
- resource.match("/new(.:format)", :method => :get).to(:action => "new").
0
- name("new", singular).register_resource(name, "new")
0
+ # => create
0
+ resource.match("(.:format)", :method => :post).to(:action => "create")
0
+
0
+ # => new
0
+ resource.match("/new(.:format)", :method => :get).to(:action => "new").
0
+ name("new", singular).register_resource(name, "new")
0
 
0
- # => user defined collection routes
0
- collection.each_pair do |action, method|
0
- action = action.to_s
0
- resource.match("/#{action}(.:format)", :method => method).to(:action => "#{action}").
0
- name(action, name).register_resource(name, action)
0
- end
0
+ # => user defined collection routes
0
+ collection.each_pair do |action, method|
0
+ action = action.to_s
0
+ resource.match("/#{action}(.:format)", :method => method).to(:action => "#{action}").
0
+ name(action, name).register_resource(name, action)
0
+ end
0
 
0
- # => show
0
- resource.match("/#{root_keys}(.:format)", :method => :get).to(:action => "show").
0
- name(singular).register_resource(klass)
0
+ # => show
0
+ resource.match("/#{root_keys}(.:format)", :method => :get).to(:action => "show").
0
+ name(singular).register_resource(klass)
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
- to(:action => "#{action}").name(action, singular).register_resource(klass, action)
0
- end
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
+ to(:action => "#{action}").name(action, singular).register_resource(klass, action)
0
+ end
0
 
0
- # => update
0
- resource.match("/#{root_keys}(.:format)", :method => :put).
0
- to(:action => "update")
0
-
0
- # => destroy
0
- resource.match("/#{root_keys}(.:format)", :method => :delete).
0
- to(:action => "destroy")
0
+ # => update
0
+ resource.match("/#{root_keys}(.:format)", :method => :put).
0
+ to(:action => "update")
0
+
0
+ # => destroy
0
+ resource.match("/#{root_keys}(.:format)", :method => :delete).
0
+ to(:action => "destroy")
0
 
0
- if block_given?
0
- nested_keys = keys.map { |k| k.to_s == "id" ? ":#{singular}_id" : ":#{k}" }.join("/")
0
- resource.options(:name_prefix => singular, :resource_prefix => klass).match("/#{nested_keys}", &block)
0
+ if block_given?
0
+ nested_keys = keys.map do |k|
0
+ k.to_s == "id" ? ":#{singular}_id" : ":#{k}"
0
+ end.join("/")
0
+
0
+ # Procs for building the extra collection/member resource routes
0
+ placeholder = Router.resource_routes[ [@options[:resource_prefix], klass].flatten.compact ]
0
+ builders = {}
0
+
0
+ builders[:collection] = lambda do |action, to, method|
0
+ resource.before(placeholder).match("/#{action}(.:format)", :method => method).
0
+ to(:action => to).name(action, name).register_resource(name, action)
0
             end
0
-
0
+
0
+ builders[:member] = lambda do |action, to, method|
0
+ resource.match("/#{root_keys}/#{action}(.:format)", :method => method).
0
+ to(:action => to).name(action, singular).register_resource(klass, action)
0
+ end
0
+
0
+ resource.options(:name_prefix => singular, :resource_prefix => klass).
0
+ match("/#{nested_keys}").resource_block(builders, &block)
0
           end
0
- end
0
+ end # namespace
0
+ end # resources
0
 
0
- # Behavior#+resource+ is a route helper for defining a singular RESTful
0
- # resource. It yields to a block for child routes.
0
- #
0
- # ==== Parameters
0
- # name<String, Symbol>:: The name of the resource.
0
- # options<Hash>::
0
- # Overides and parameters to be associated with the route.
0
- #
0
- # ==== Options (options)
0
- # :namespace<~to_s>: The namespace for this route.
0
- # :name_prefix<~to_s>:
0
- # A prefix for the named routes. If a namespace is passed and there
0
- # isn't a name prefix, the namespace will become the prefix.
0
- # :controller<~to_s>: The controller for this route
0
- #
0
- # ==== Block parameters
0
- # next_level<Behavior>:: The child behavior.
0
- #
0
- # ==== Returns
0
- # Array:: Routes which define a RESTful single resource.
0
- #
0
- # ==== Examples
0
- #
0
- # r.resource :account # will result in the typical RESTful CRUD
0
- # # shows new resource form
0
- # # GET /account/new :action => "new"
0
- #
0
- # # creates resource
0
- # # POST /account/?(\.:format)?, :action => "create"
0
- #
0
- # # shows resource
0
- # # GET /account/(\.:format)? :action => "show"
0
- #
0
- # # shows edit form
0
- # # GET /account//edit :action => "edit"
0
- #
0
- # # updates resource
0
- # # PUT /account/(\.:format)? :action => "update"
0
- #
0
- # # shows deletion confirmation page
0
- # # GET /account//delete :action => "delete"
0
- #
0
- # # destroys resources
0
- # # DELETE /account/(\.:format)? :action => "destroy"
0
- #
0
- # You can optionally pass :namespace and :controller to refine the routing
0
- # or pass a block to nest resources.
0
- #
0
- # r.resource :account, :namespace => "admin" do |account|
0
- # account.resources :preferences, :controller => "settings"
0
- # end
0
- # ---
0
- # @public
0
- def resource(name, *args, &block)
0
- name = name.to_s
0
- options = extract_options_from_args!(args) || {}
0
- params = { :controller => options.delete(:controller) || name.pluralize }
0
+ # Behavior#+resource+ is a route helper for defining a singular RESTful
0
+ # resource. It yields to a block for child routes.
0
+ #
0
+ # ==== Parameters
0
+ # name<String, Symbol>:: The name of the resource.
0
+ # options<Hash>::
0
+ # Overides and parameters to be associated with the route.
0
+ #
0
+ # ==== Options (options)
0
+ # :namespace<~to_s>: The namespace for this route.
0
+ # :name_prefix<~to_s>:
0
+ # A prefix for the named routes. If a namespace is passed and there
0
+ # isn't a name prefix, the namespace will become the prefix.
0
+ # :controller<~to_s>: The controller for this route
0
+ #
0
+ # ==== Block parameters
0
+ # next_level<Behavior>:: The child behavior.
0
+ #
0
+ # ==== Returns
0
+ # Array:: Routes which define a RESTful single resource.
0
+ #
0
+ # ==== Examples
0
+ #
0
+ # r.resource :account # will result in the typical RESTful CRUD
0
+ # # shows new resource form
0
+ # # GET /account/new :action => "new"
0
+ #
0
+ # # creates resource
0
+ # # POST /account/?(\.:format)?, :action => "create"
0
+ #
0
+ # # shows resource
0
+ # # GET /account/(\.:format)? :action => "show"
0
+ #
0
+ # # shows edit form
0
+ # # GET /account//edit :action => "edit"
0
+ #
0
+ # # updates resource
0
+ # # PUT /account/(\.:format)? :action => "update"
0
+ #
0
+ # # shows deletion confirmation page
0
+ # # GET /account//delete :action => "delete"
0
+ #
0
+ # # destroys resources
0
+ # # DELETE /account/(\.:format)? :action => "destroy"
0
+ #
0
+ # You can optionally pass :namespace and :controller to refine the routing
0
+ # or pass a block to nest resources.
0
+ #
0
+ # r.resource :account, :namespace => "admin" do |account|
0
+ # account.resources :preferences, :controller => "settings"
0
+ # end
0
+ # ---
0
+ # @public
0
+ def resource(name, *args, &block)
0
+ name = name.to_s
0
+ options = extract_options_from_args!(args) || {}
0
+ params = { :controller => options.delete(:controller) || name.pluralize }
0
+ member = { :new => :get, :edit => :get, :delete => :get }.merge(options.delete(:member) || {})
0
 
0
- options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
0
- options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
0
- options[:controller_prefix] ||= options.delete(:namespace)
0
+ options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
0
+ options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
0
+ options[:controller_prefix] ||= options.delete(:namespace)
0
 
0
- self.namespace(name, options).to(params) do |resource|
0
- resource.match("(.:format)", :method => :get ).to(:action => "show" ).name(name).register_resource(name)
0
- resource.match("(.:format)", :method => :post ).to(:action => "create" )
0
- resource.match("(.:format)", :method => :put ).to(:action => "update" )
0
- resource.match("(.:format)", :method => :delete).to(:action => "destroy")
0
- resource.match("/new(.:format)", :method => :get ).to(:action => "new" ).name(:new, name).register_resource(name, "new")
0
- resource.match("/edit(.:format)", :method => :get ).to(:action => "edit" ).name(:edit, name).register_resource(name, "edit")
0
- resource.match("/delete(.:format)", :method => :get ).to(:action => "delete" ).name(:delete, name).register_resource(name, "delete")
0
+ self.namespace(name, options).to(params) do |resource|
0
+ # => show
0
+ resource.match("(.:format)", :method => :get).to(:action => "show").
0
+ name(name).register_resource(name)
0
+
0
+ # => create
0
+ resource.match("(.:format)", :method => :post).to(:action => "create")
0
+
0
+ # => update
0
+ resource.match("(.:format)", :method => :put).to(:action => "update")
0
+
0
+ # => destroy
0
+ resource.match("(.:format)", :method => :delete).to(:action => "destroy")
0
+
0
+ member.each_pair do |action, method|
0
+ action = action.to_s
0
+ resource.match("/#{action}(.:format)", :method => method).to(:action => action).
0
+ name(action, name).register_resource(name, action)
0
+ end
0
 
0
- resource.options(:name_prefix => name, :resource_prefix => name, &block) if block_given?
0
+ if block_given?
0
+ builders = {}
0
+
0
+ builders[:member] = lambda do |action, to, method|
0
+ resource.match("/#{action}(.:format)", :method => method).to(:action => to).
0
+ name(action, name).register_resource(name, action)
0
+ end
0
+
0
+ resource.options(:name_prefix => name, :resource_prefix => name).
0
+ resource_block(builders, &block)
0
           end
0
         end
0
-
0
- protected
0
+ end
0
       
0
- def register_resource(*key)
0
- key = [@options[:resource_prefix], key].flatten.compact
0
- @route.resource = key
0
- self
0
- end
0
+ protected
0
+
0
+ def register_resource(*key)
0
+ key = [@options[:resource_prefix], key].flatten.compact
0
+ @route.resource = key
0
+ self
0
+ end
0
 
0
+ def resource_block(builders, &block)
0
+ behavior = ResourceBehavior.new(builders, @proxy, @conditions, @params, @defaults, @identifiers, @options, @blocks)
0
+ with_behavior_context(behavior, &block)
0
       end
0
 
0
+ end # Resources
0
+
0
+ class Behavior
0
       include Resources
0
     end
0
+
0
+ # Adding the collection and member methods to behavior
0
+ class ResourceBehavior < Behavior #:nodoc:
0
+
0
+ def initialize(builders, *args)
0
+ super(*args)
0
+ @collection = builders[:collection]
0
+ @member = builders[:member]
0
+ end
0
+
0
+ def collection(action, options = {})
0
+ action = action.to_s
0
+ method = options[:method]
0
+ to = options[:to] || action
0
+ @collection[action, to, method]
0
+ end
0
+
0
+ def member(action, options = {})
0
+ action = action.to_s
0
+ method = options[:method]
0
+ to = options[:to] || action
0
+ @member[action, to, method]
0
+ end
0
+
0
+ end
0
   end
0
 end
0
\ No newline at end of file
...
52
53
54
 
55
56
57
58
59
60
 
 
 
 
 
 
 
61
62
63
...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
0
@@ -52,12 +52,20 @@ module Merb
0
       
0
       alias_method :inspect, :to_s
0
 
0
+ # Appends self to Merb::Router.routes
0
       def register
0
         @index = Merb::Router.routes.size
0
         Merb::Router.routes << self
0
         self
0
       end
0
       
0
+ # Inserts self to Merb::Router.routes at the specified index.
0
+ def register_at(index)
0
+ @index = index
0
+ Merb::Router.routes.insert(index, self)
0
+ self
0
+ end
0
+
0
       # Sets the route as a resource route with the given key as the
0
       # lookup key.
0
       def resource=(key)
...
44
45
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
48
49
...
72
73
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
76
77
...
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
...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
0
@@ -44,6 +44,31 @@ describe Merb::Controller, " #resource" do
0
       @controller.resource(@user, :delete).should == "/users/5/delete"
0
     end
0
     
0
+ it "should be able to specify extra actions through the options" do
0
+ Merb::Router.prepare do
0
+ identify :id do
0
+ resources :users, :collection => { :hello => :get }, :member => { :goodbye => :post }
0
+ end</