resources 0.1.3 → 0.2.0

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: bf722e722d2f471c57224f21a678dd0a054bf163
4
- data.tar.gz: b029230fa7ccb38892f8d2f1d1577633ae027b48
3
+ metadata.gz: 581f13986a09c2e59a9f9d1ef6a55e042d0ead84
4
+ data.tar.gz: 8f133e57f806c9a5f55e0cf73200e1108d1a94d9
5
5
  SHA512:
6
- metadata.gz: 8e177b3c014074f81c74f6b27597a7b539c1cb0373f2e16ae45cdef394644f19125c20f24c87b4f7327277160e6abfd92b1d1c3b8d7db600c86635ef62215979
7
- data.tar.gz: 95e8cbacafed2eaa4f97e8d5e60651a9650c8dd6449cdaea6a666c73f1e9a9b3f4de305996f4f92eb788d505bc90a1b5d462df089cf53428bc81889cedc7d5aa
6
+ metadata.gz: 68339a0d0a01806b4e7f4ef7f3705c9b063a4201a758102fb961bb38faec6707bd7bfe07ef3b222d707efd57e6fb594b409240ec87458543b3c138de4159db28
7
+ data.tar.gz: 9a63a999d2728284df527a2edb6a66e0f9a132903c751f84b6dfe288debd48de46442c2043dbc9cb2177be6417ee3859754ef66a00267128ee3a41135c5fe481
data/README.md CHANGED
@@ -269,6 +269,61 @@ If you want to override any of the REST actions, to add any extra logic that you
269
269
 
270
270
  **NOTE:** Remember that you can make use of the **`resource_saved?`** method to know if the record has been saved
271
271
 
272
+ #### **Using it with Grape **
273
+
274
+ If you are using `resources` with the [Grape](https://github.com/intridea/grape) gem fallow the next steps:
275
+
276
+ In your Gemfile add the `resources` **after** `grape`
277
+
278
+ ```
279
+ gem "grape"
280
+ gem 'grape-raketasks'
281
+ gem "hashie-forbidden_attributes"
282
+ gem "resources"
283
+ ```
284
+
285
+ Now in your api class add the **`helpers ::Resources::GrapeHelpers`** and in the execute the **`resource_for`** inside a before block.
286
+
287
+ ```
288
+ require "resources"
289
+ require "resources/grape_helpers" # only need to add this line if you didnt do the previous step
290
+ module Twitter
291
+ class API < Grape::API
292
+ version 'v1', using: :header, vendor: 'twitter'
293
+ format :json
294
+ prefix :api
295
+
296
+ helpers ::Resources::GrapeHelpers
297
+
298
+ before do
299
+ resource_for :"Country"
300
+ end
301
+
302
+ resource :countries do
303
+ desc "Return a list of countries."
304
+ get do
305
+ resources
306
+ end
307
+
308
+ desc "Return a country."
309
+ get ':id' do
310
+ resource
311
+ end
312
+
313
+ desc "Update a country."
314
+ put ':id' do
315
+ save_resource
316
+ end
317
+
318
+ end
319
+
320
+ end
321
+ end
322
+
323
+ ```
324
+ **NOTE:** Currently there is a limitation, the option **`resource_method_name`** doesn't work inside grape
325
+
326
+
272
327
  #### **GLOBAL CONFIGURATION **
273
328
 
274
329
  You can change the default parameters of any configuration by change the options in the `config/initializers/resources.rb` that was generated by running `rails g resources:install`
data/lib/loader.rb CHANGED
@@ -3,4 +3,8 @@ require "resources/rest_actions"
3
3
  require "resources/configuration"
4
4
  require "resources/controller"
5
5
  require "resources/routes"
6
- require "resources/manager"
6
+ require "resources/manager"
7
+
8
+ if defined?(Grape)
9
+ require "resources/grape_helpers"
10
+ end
data/lib/resources.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "loader"
2
2
  module Resources
3
-
3
+
4
4
  end
5
- ActionController::Base.send(:include, Resources::Controller)
5
+ ActionController::Base.send(:include, Resources::Controller)
@@ -0,0 +1,46 @@
1
+ module Resources
2
+ module GrapeHelpers
3
+
4
+ def resource_for name = nil, *args
5
+ options = Resources::Config.to_hash.deep_merge(args.extract_options!)
6
+ @resource_configuration = Resources::Configuration.new(options)
7
+ @resource_configuration.resource_class_name = name
8
+ end
9
+
10
+ def resource_configuration
11
+ @resource_configuration
12
+ end
13
+
14
+ def resources_search
15
+ @resources_search ||= resource_manager.resources_search
16
+ end
17
+
18
+ def resources
19
+ @resources ||= resource_manager.resources
20
+ end
21
+
22
+ def resource
23
+ @resource ||= resource_manager.resource
24
+ end
25
+
26
+ def resource_manager
27
+ @resource_manager ||= Resources::Manager.new(self, request)
28
+ end
29
+
30
+ def resource_saved?
31
+ @resource_saved
32
+ end
33
+
34
+ def save_resource &block
35
+ resource.assign_attributes(resource_manager.params_resource)
36
+ @resource_saved = resource.save
37
+ block_given? ? block.call(resource) : resource
38
+ end
39
+
40
+ def destroy_resource &block
41
+ @destroy_resource = resource.destroy
42
+ block_given? ? block.call(resource) : resource
43
+ end
44
+
45
+ end
46
+ end
@@ -5,7 +5,7 @@ module Resources
5
5
  def initialize controller, request, *args
6
6
  @controller = controller
7
7
  @request = request
8
- @settings = @controller.class.resource_configuration
8
+ @settings = grape? ? @controller.resource_configuration : @controller.class.resource_configuration
9
9
  end
10
10
 
11
11
  def resource_class
@@ -25,6 +25,10 @@ module Resources
25
25
  end
26
26
  end
27
27
 
28
+ def grape?
29
+ defined?(Grape) && !(controller.class < ActionController::Base)
30
+ end
31
+
28
32
  def pagination?
29
33
  settings.pagination.is_a?(Proc) ? settings.pagination.call(params, controller) : settings.pagination
30
34
  end
@@ -43,13 +47,17 @@ module Resources
43
47
  end
44
48
 
45
49
  def resource
46
- @resource ||=
47
- case controller.action_name
48
- when "new", "create"
49
- build_resource
50
- else
51
- resource_scope.where("#{resource_class.table_name}.id = ?",params[:id]).first rescue nil
52
- end
50
+ if grape?
51
+ @resource ||= params[:id].blank? ? build_resource : (resource_scope.where("#{resource_class.table_name}.id = ?",params[:id]).first rescue nil )
52
+ else
53
+ @resource ||=
54
+ case controller.action_name
55
+ when "new", "create"
56
+ build_resource
57
+ else
58
+ resource_scope.where("#{resource_class.table_name}.id = ?",params[:id]).first rescue nil
59
+ end
60
+ end
53
61
  end
54
62
 
55
63
  def settings
@@ -57,7 +65,7 @@ module Resources
57
65
  end
58
66
 
59
67
  def params
60
- controller.params
68
+ grape? ? request.params : controller.params
61
69
  end
62
70
 
63
71
  def params_search
@@ -90,16 +98,28 @@ module Resources
90
98
  end
91
99
  end
92
100
 
101
+ def forbidden_params_names
102
+ [settings.resource_method_name, settings.resources_method_name, :resource, :resources]
103
+ end
104
+
105
+ def forbidden_params_names? name
106
+ forbidden_params_names.map(&:to_s).include?(settings.send(name).to_s)
107
+ end
108
+
93
109
  def option_with_params name
110
+ result = {}
94
111
  if settings.send(name).is_a?(Proc)
95
- settings.send(name).call(params)
112
+ result = settings.send(name).call(params)
96
113
  else
97
- if Rails::VERSION::MAJOR >= 4
98
- controller.send(name)
114
+ if Rails::VERSION::MAJOR >= 4 && !forbidden_params_names?(name)
115
+ value = controller.send(name) rescue nil
116
+ result = value ? value : params[settings.send(name)]
99
117
  else
100
- params[settings.send(name)]
118
+ result = params[settings.send(name)]
101
119
  end
102
120
  end
121
+ result = result && result.is_a?(Hash) ? result : {}
122
+ result
103
123
  end
104
124
 
105
125
  def controller
@@ -31,6 +31,9 @@ module Resources
31
31
 
32
32
  end
33
33
 
34
+ def show
35
+ end
36
+
34
37
  def update
35
38
  save_resource
36
39
  end
@@ -1,3 +1,3 @@
1
1
  module Resources
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emilio Forrer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-08 00:00:00.000000000 Z
11
+ date: 2015-03-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: It's a gem that help's you to create CRUD controllers for your models
14
14
  in a very fast and flexible way.
@@ -34,6 +34,7 @@ files:
34
34
  - lib/resources/actions.rb
35
35
  - lib/resources/configuration.rb
36
36
  - lib/resources/controller.rb
37
+ - lib/resources/grape_helpers.rb
37
38
  - lib/resources/manager.rb
38
39
  - lib/resources/rest_actions.rb
39
40
  - lib/resources/routes.rb