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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 96495291f7c6fc588ba0ab6b78abbd2f4bb770d9
4
- data.tar.gz: 29039ed3b70521b5300936d3f315416d97cb9263
3
+ metadata.gz: 8c635006c6bd149411f94320dea9d3ec324e935d
4
+ data.tar.gz: 2987656f85763c77dbece18e5d7ed263d10fdc81
5
5
  SHA512:
6
- metadata.gz: 17227ee0aa864e192759081eb36a00e3e874bfd7b46736a90a9c3a9faf41fef4e6f491423bc6bfd972bdb43e082121def309d670b98aeac605cd4a918d156a72
7
- data.tar.gz: 089528b0ea4dd812e6d89d50dfa24603cb0216821605ccf9ba13da65ec599de698d61d67d86e6013501e0a4ce97ea0e48fa384005e92568b000faf702e360baf
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 requires parameter which is a symbol of the model name.
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
@@ -3,5 +3,5 @@
3
3
  module Restful
4
4
  ##
5
5
  # Sets the library version
6
- VERSION = '0.1.4'
6
+ VERSION = '0.1.5'
7
7
  end
@@ -0,0 +1,4 @@
1
+ class Admin::PrefixController < BaseController
2
+
3
+ restful model: :document, route_prefix: 'admin'
4
+ end
@@ -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
- root 'home#index'
8
- # The priority is based upon order of creation: first created -> highest priority.
9
- # See how all your routes lay out with "rake routes".
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
- # Example resource route (maps HTTP verbs to controller actions automatically):
21
- # resources :products
11
+ get '/:id/edit' => 'prefix#edit', as: :edit_document
22
12
 
23
- # Example resource route with options:
24
- # resources :products do
25
- # member do
26
- # get 'short'
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
- # Example resource route with sub-resources:
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
Binary file