restful_controller 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +14 -0
- data/lib/restful/base.rb +36 -9
- data/lib/restful/version.rb +1 -1
- data/test/dummy/app/controllers/admin/prefix_controller.rb +4 -0
- data/test/dummy/config/routes.rb +9 -52
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +17281 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/prefix_controller_test.rb +43 -0
- metadata +28 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c635006c6bd149411f94320dea9d3ec324e935d
|
4
|
+
data.tar.gz: 2987656f85763c77dbece18e5d7ed263d10fdc81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e5b7b1a47b8a8d009baebfc3039268b0df25e8414b786381f081d3923c2606e11ce7217f3a54d8efe4d01c37d4e42cd2d92cf859362be009c7ab617083c2953
|
7
|
+
data.tar.gz: 32ec1f7af9df76e53e9f60b572fe309d13edaad386e4cdf504db16edd7d617c90870ce51ce2fa138cb29adc8e41e7397bb5cebd331b881c6d06eed77ea4eb543
|
data/README.rdoc
CHANGED
@@ -27,6 +27,7 @@ This macro accepts 2 params:
|
|
27
27
|
|
28
28
|
=== Params
|
29
29
|
* model: A requires parameter which is a symbol of the model name.
|
30
|
+
* route_prefix: A prefix string to be used with controller's url helper.
|
30
31
|
* actions: An array of actions that a controller should implement, if none is passed then all seven REST actions are defined.
|
31
32
|
|
32
33
|
=== Examples
|
@@ -44,6 +45,19 @@ This definition will create the seven REST actions for Document model,
|
|
44
45
|
this setup a single object instance variable @document and a collection
|
45
46
|
variable @documents.
|
46
47
|
|
48
|
+
Route prefix:
|
49
|
+
|
50
|
+
class DocumentsController < ApplicationController
|
51
|
+
include Restful::Base
|
52
|
+
respond_to :html
|
53
|
+
|
54
|
+
restful model: :document, route_prefix: 'admin'
|
55
|
+
end
|
56
|
+
|
57
|
+
With *route_prefix* param every URL helper method in our controller will have the defined prefix.
|
58
|
+
|
59
|
+
`edit_resource_path` helper method will call internally `admin_edit_resource_path`.
|
60
|
+
|
47
61
|
Listed actions variation:
|
48
62
|
|
49
63
|
The last parameter *actions* allows you to list in an array the actions
|
data/lib/restful/base.rb
CHANGED
@@ -44,43 +44,52 @@ module Restful
|
|
44
44
|
##
|
45
45
|
# Generic route path helper method to edit a model.
|
46
46
|
def edit_resource_path(object)
|
47
|
-
self.send "edit_#{class_name.model_name.singular_route_key}_path",
|
47
|
+
self.send route_prefix_to_method_name("edit_#{class_name.model_name.singular_route_key}_path"),
|
48
48
|
object
|
49
49
|
end
|
50
50
|
|
51
51
|
##
|
52
52
|
# Generic route url helper method to edit a model.
|
53
53
|
def edit_resource_url(object)
|
54
|
-
self.send "edit_#{class_name.model_name.singular_route_key}_url",
|
54
|
+
self.send route_prefix_to_method_name("edit_#{class_name.model_name.singular_route_key}_url"),
|
55
55
|
object
|
56
56
|
end
|
57
57
|
|
58
58
|
##
|
59
59
|
# Generic route path helper method for new model.
|
60
60
|
def new_resource_path
|
61
|
-
self.send "new_#{class_name.model_name.singular_route_key}_path"
|
61
|
+
self.send route_prefix_to_method_name("new_#{class_name.model_name.singular_route_key}_path")
|
62
62
|
end
|
63
63
|
|
64
64
|
##
|
65
65
|
# Generic route url helper method for new model.
|
66
66
|
def new_resource_url
|
67
|
-
self.send "new_#{class_name.model_name.singular_route_key}_url"
|
67
|
+
self.send route_prefix_to_method_name("new_#{class_name.model_name.singular_route_key}_url")
|
68
68
|
end
|
69
69
|
|
70
70
|
##
|
71
71
|
# This is a helper method to get the object collection path.
|
72
72
|
def collection_path
|
73
|
-
self.send "#{class_name.model_name.route_key}_path"
|
73
|
+
self.send route_prefix_to_method_name("#{class_name.model_name.route_key}_path")
|
74
74
|
end
|
75
75
|
|
76
76
|
##
|
77
77
|
# This is a helper method to get the object collection url.
|
78
78
|
def collection_url
|
79
|
-
self.send "#{class_name.model_name.route_key}_url"
|
79
|
+
self.send route_prefix_to_method_name("#{class_name.model_name.route_key}_url")
|
80
80
|
end
|
81
81
|
|
82
82
|
protected
|
83
83
|
##
|
84
|
+
# Return a url helper method name with additional route prefix if set.
|
85
|
+
# If route_prefix param is set to `admin` then the method name will be:
|
86
|
+
#
|
87
|
+
# edit_resource_path => admin_edit_resource_path
|
88
|
+
#
|
89
|
+
def route_prefix_to_method_name(method)
|
90
|
+
"#{route_prefix + '_' if route_prefix}#{method}"
|
91
|
+
end
|
92
|
+
##
|
84
93
|
# Return the instance variable name for a single object based on the model
|
85
94
|
# name defined in the restful macro, example:
|
86
95
|
#
|
@@ -243,7 +252,9 @@ module Restful
|
|
243
252
|
# This macro accepts 3 params:
|
244
253
|
#
|
245
254
|
# ==== Params
|
246
|
-
# * model: A
|
255
|
+
# * model: A required parameter which is a symbol of the model name.
|
256
|
+
# * route_prefix: A prefix string to be used with controller's url
|
257
|
+
# helper.
|
247
258
|
# * actions: An array of actions that a controller should implement, if
|
248
259
|
# none is passed then all seven REST actions are defined.
|
249
260
|
#
|
@@ -262,6 +273,21 @@ module Restful
|
|
262
273
|
# this setup a single object instance variable @document and a collection
|
263
274
|
# variable @documents.
|
264
275
|
#
|
276
|
+
# Route prefix:
|
277
|
+
#
|
278
|
+
# class DocumentsController < ApplicationController
|
279
|
+
# include Restful::Base
|
280
|
+
# respond_to :html
|
281
|
+
#
|
282
|
+
# restful model: :document, route_prefix: 'admin'
|
283
|
+
# end
|
284
|
+
#
|
285
|
+
# With *route_prefix* param every URL helper method in our controller
|
286
|
+
# will have the defined prefix.
|
287
|
+
#
|
288
|
+
# `edit_resource_path` helper method will call internally
|
289
|
+
# `admin_edit_resource_path`.
|
290
|
+
#
|
265
291
|
# Listed actions variation:
|
266
292
|
#
|
267
293
|
# The last parameter *actions* allows you to list in an array the actions
|
@@ -304,12 +330,13 @@ module Restful
|
|
304
330
|
# include it in your controllers and list the formats do you wish your
|
305
331
|
# controller to respond.
|
306
332
|
#
|
307
|
-
def restful(model: nil, actions: :all)
|
308
|
-
self.class_attribute :model_name, :class_name,
|
333
|
+
def restful(model: nil, route_prefix: nil, actions: :all)
|
334
|
+
self.class_attribute :model_name, :class_name, :route_prefix,
|
309
335
|
instance_writer: false
|
310
336
|
|
311
337
|
self.model_name = model
|
312
338
|
self.class_name = class_from_name
|
339
|
+
self.route_prefix = route_prefix
|
313
340
|
|
314
341
|
include InstanceMethods
|
315
342
|
include Restful::Actions
|
data/lib/restful/version.rb
CHANGED
data/test/dummy/config/routes.rb
CHANGED
@@ -4,59 +4,16 @@ Dummy::Application.routes.draw do
|
|
4
4
|
resources :custom_notices, only: [:create, :update]
|
5
5
|
resources :blocks, only: [:create, :update]
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# You can have the root of your site routed with "root"
|
12
|
-
# root 'welcome#index'
|
13
|
-
|
14
|
-
# Example of regular route:
|
15
|
-
# get 'products/:id' => 'catalog#view'
|
16
|
-
|
17
|
-
# Example of named route that can be invoked with purchase_url(id: product.id)
|
18
|
-
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
|
7
|
+
namespace :admin do
|
8
|
+
get '/new' => 'prefix#new', as: :new_document
|
9
|
+
get '/:id' => 'prefix#show', as: :document
|
19
10
|
|
20
|
-
|
21
|
-
# resources :products
|
11
|
+
get '/:id/edit' => 'prefix#edit', as: :edit_document
|
22
12
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
# post 'toggle'
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
# collection do
|
31
|
-
# get 'sold'
|
32
|
-
# end
|
33
|
-
# end
|
13
|
+
get '/' => 'prefix#index', as: :documents
|
14
|
+
post '/' => 'prefix#create'
|
15
|
+
put '/' => 'prefix#update'
|
16
|
+
end
|
34
17
|
|
35
|
-
#
|
36
|
-
# resources :products do
|
37
|
-
# resources :comments, :sales
|
38
|
-
# resource :seller
|
39
|
-
# end
|
40
|
-
|
41
|
-
# Example resource route with more complex sub-resources:
|
42
|
-
# resources :products do
|
43
|
-
# resources :comments
|
44
|
-
# resources :sales do
|
45
|
-
# get 'recent', on: :collection
|
46
|
-
# end
|
47
|
-
# end
|
48
|
-
|
49
|
-
# Example resource route with concerns:
|
50
|
-
# concern :toggleable do
|
51
|
-
# post 'toggle'
|
52
|
-
# end
|
53
|
-
# resources :posts, concerns: :toggleable
|
54
|
-
# resources :photos, concerns: :toggleable
|
55
|
-
|
56
|
-
# Example resource route within a namespace:
|
57
|
-
# namespace :admin do
|
58
|
-
# # Directs /admin/products/* to Admin::ProductsController
|
59
|
-
# # (app/controllers/admin/products_controller.rb)
|
60
|
-
# resources :products
|
61
|
-
# end
|
18
|
+
root 'home#index'
|
62
19
|
end
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|