skinny_controllers 0.5.6 → 0.5.7

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: e421158402ef37ad4aedb544359ada894d6ba616
4
- data.tar.gz: 7c084cdbf5699bee2ca251abeec24ff98cfbb060
3
+ metadata.gz: 33cb5420fdc28bbd364bb6016c4d6aead5d6a0ae
4
+ data.tar.gz: 0e9f8d8868d29ae0dac511c30e82dd9f06120cc7
5
5
  SHA512:
6
- metadata.gz: 10dad383fd05c9c5454a583cb93555962833c389936ff1366ff193a567b5ac96f31fa5d8b2afbdd72c5cc1804247b2d7f020289ffb9262e5c5398dcb314fd903
7
- data.tar.gz: b5cd1fe9e58ef4168a6858e05da1e2ebaf89e723261c37e811d85657778b7b9f28c595167bd35a5e5ea97906b409d92369af3d27a64681cb1fe658c5d6abd517
6
+ metadata.gz: 557258742889bd97ad31e7c27db3a04fec56406d0eb08f59341f59c4736ce54e77f6d59bf7676009f5174cb50b984ec4cfbf1704411085efbb3f2ecb9683261c
7
+ data.tar.gz: 8986b7e99959fd999bd3a5891e73c78f55b06eb7ab0e3aec53d960d4393eadaec9e3d29de15e7e3e467f62247edd23379f347e6543420817750874f03c04dc22
data/README.md CHANGED
@@ -87,7 +87,7 @@ end
87
87
  private
88
88
 
89
89
  def host_from_subdomain
90
- @host ||= HostOperations::Read.new(current_user, host_params).run
90
+ @host ||= HostOperations::Read.new(current_user, params, host_params).run
91
91
  end
92
92
 
93
93
  def host_params
@@ -95,6 +95,26 @@ def host_params
95
95
  end
96
96
  ```
97
97
 
98
+ ### For JSON-API
99
+
100
+ Strong parameters must be used on create/update actions.
101
+
102
+ Here is an example params method
103
+
104
+ ```ruby
105
+ private
106
+
107
+ def event_params
108
+ params
109
+ .require(:data)
110
+ .require(:attributes)
111
+ .permit(:name)
112
+ end
113
+ ```
114
+
115
+ Note that we don't need the id under the data hash, because in a RESTful api, the id will be available to us through the top level params hash.
116
+
117
+
98
118
  ## Defining Operations
99
119
 
100
120
  Operations should be placed in `app/operations` of your rails app.
@@ -219,7 +239,6 @@ The following options are available:
219
239
  |`accessible_to_method`|`is_accessible_to?`| method to call an the object that the user might be able to access |
220
240
  |`accessible_to_scope`| `accessible_to`| scope / class method on an object that the user might be able to access |
221
241
  |`action_map`| see [skinny_controllers.rb](./lib/skinny_controllers.rb#L61)| |
222
- |`params_format`|`:json`| or `:json_api`|
223
242
 
224
243
  ## TODO
225
244
 
@@ -10,7 +10,7 @@ module SkinnyControllers
10
10
  #
11
11
  # @return an instance of the operation with default parameters
12
12
  def operation
13
- @operation ||= operation_class.new(current_user, params_for_action)
13
+ @operation ||= operation_class.new(current_user, params, params_for_action, action_name)
14
14
  end
15
15
 
16
16
  # Assumes the operation name from the controller name
@@ -15,22 +15,28 @@ module SkinnyControllers
15
15
  class Base
16
16
  include ModelHelpers
17
17
 
18
- attr_accessor :params, :current_user, :authorized_via_parent
18
+ attr_accessor :params, :current_user, :authorized_via_parent, :action, :params_for_action
19
19
 
20
20
  def self.run(current_user, params)
21
21
  object = new(current_user, params)
22
22
  object.run
23
23
  end
24
24
 
25
- def initialize(current_user, params)
26
- self.current_user = current_user
27
- self.params = params
25
+ # @param [Model] current_user the logged in user
26
+ # @param [Hash] controller_params the params hash raw from the controller
27
+ # @param [Hash] params_for_action optional params hash, generally the result of strong parameters
28
+ # @param [string] action the current action on the controller
29
+ def initialize(current_user, controller_params, params_for_action = nil, action = nil)
28
30
  self.authorized_via_parent = false
31
+ self.current_user = current_user
32
+ self.action = action || controller_params[:action]
33
+ self.params = controller_params
34
+ self.params_for_action = params_for_action || controller_params
29
35
  end
30
36
 
31
37
  def id_from_params
32
38
  unless @id_from_params
33
- @id_from_params = params[:data] ? params[:data][:id] : params[:id]
39
+ @id_from_params = params[:id]
34
40
  if filter = params[:filter]
35
41
  @id_from_params = filter[:id].split(',')
36
42
  end
@@ -27,15 +27,15 @@ module SkinnyControllers
27
27
  private
28
28
 
29
29
  def creating?
30
- params[:action] == 'create'
30
+ action == 'create'
31
31
  end
32
32
 
33
33
  def updating?
34
- params[:action] == 'update'
34
+ action == 'update'
35
35
  end
36
36
 
37
37
  def destroying?
38
- params[:action] == 'destroy'
38
+ action == 'destroy'
39
39
  end
40
40
  end
41
41
  end
@@ -28,20 +28,7 @@ module SkinnyControllers
28
28
  # for mass-assignment, rails doesn't accept
29
29
  # stringified keys.
30
30
  # TODO: why did the params hash lose its indifferent access
31
- @model_params ||= param_parser.model_params.symbolize_keys
32
- end
33
-
34
- def param_parser
35
- unless @param_parser
36
- @param_parser =
37
- if SkinnyControllers.params_format == :json
38
- Params::Json.new(params, model_param_name)
39
- else
40
- Params::JsonApi.new(params, model_param_name)
41
- end
42
- end
43
-
44
- @param_parser
31
+ @model_params ||= (params_for_action[model_param_name] || params_for_action).symbolize_keys
45
32
  end
46
33
 
47
34
  def model_param_name
@@ -1,3 +1,3 @@
1
1
  module SkinnyControllers
2
- VERSION = '0.5.6'
2
+ VERSION = '0.5.7'
3
3
  end
@@ -7,9 +7,6 @@ require 'active_support/core_ext/string/inflections'
7
7
 
8
8
  # files for this gem
9
9
  require 'skinny_controllers/default_verbs'
10
- require 'skinny_controllers/json_api_document'
11
- require 'skinny_controllers/params/json'
12
- require 'skinny_controllers/params/json_api'
13
10
  require 'skinny_controllers/lookup/controller'
14
11
  require 'skinny_controllers/lookup/model'
15
12
  require 'skinny_controllers/lookup/operation'
@@ -64,10 +61,6 @@ module SkinnyControllers
64
61
  :accessible_to
65
62
  end
66
63
 
67
- cattr_accessor :params_format do
68
- :json # or :json_api
69
- end
70
-
71
64
  # the diet uses ActionController::Base's
72
65
  # `action_name` method to get the current action.
73
66
  # From that action, we map what verb we want to use for our operation
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skinny_controllers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - L. Preston Sego III
@@ -189,7 +189,6 @@ files:
189
189
  - lib/skinny_controllers.rb
190
190
  - lib/skinny_controllers/default_verbs.rb
191
191
  - lib/skinny_controllers/diet.rb
192
- - lib/skinny_controllers/json_api_document.rb
193
192
  - lib/skinny_controllers/lookup/controller.rb
194
193
  - lib/skinny_controllers/lookup/model.rb
195
194
  - lib/skinny_controllers/lookup/operation.rb
@@ -197,8 +196,6 @@ files:
197
196
  - lib/skinny_controllers/operation/base.rb
198
197
  - lib/skinny_controllers/operation/default.rb
199
198
  - lib/skinny_controllers/operation/model_helpers.rb
200
- - lib/skinny_controllers/params/json.rb
201
- - lib/skinny_controllers/params/json_api.rb
202
199
  - lib/skinny_controllers/policy/allow_all.rb
203
200
  - lib/skinny_controllers/policy/base.rb
204
201
  - lib/skinny_controllers/policy/default.rb
@@ -227,5 +224,5 @@ rubyforge_project:
227
224
  rubygems_version: 2.4.8
228
225
  signing_key:
229
226
  specification_version: 4
230
- summary: SkinnyControllers-0.5.6
227
+ summary: SkinnyControllers-0.5.7
231
228
  test_files: []
@@ -1,29 +0,0 @@
1
- module SkinnyControllers
2
- # currently copied from NullVoxPopuli/json-api-document
3
- # Not sure if this should be its own gem or not
4
- class JsonApiDocument
5
- attr_accessor :data, :id, :attributes
6
-
7
- # @param [Hash] json the json api document
8
- # @param [Symbol] key_formatter converts the attribute keys
9
- def initialize(json, key_formatter: nil)
10
- json = JSON.parse(json) if json.is_a?(String)
11
-
12
- self.data = json['data']
13
- self.id = json['id'] || data.try(:[], 'id')
14
- self.attributes = format_keys(data.try(:[], 'attributes'), key_formatter)
15
- end
16
-
17
- private
18
-
19
- # TODO: how to handle nested hashes
20
- def format_keys(hash, key_format_method)
21
- return hash unless key_format_method
22
-
23
- hash.each_with_object({}) do | (k, v), result |
24
- new_key = k.send(key_format_method)
25
- result[new_key] = v
26
- end
27
- end
28
- end
29
- end
@@ -1,20 +0,0 @@
1
- module SkinnyControllers
2
- module Params
3
- class Json
4
- attr_accessor :document, :model_key
5
- # @param [Params] params from controller
6
- def initialize(params, model_key)
7
- self.document = params
8
- self.model_key = model_key
9
- end
10
-
11
- def model_params
12
- document[model_key].symbolize_keys
13
- end
14
-
15
- def id
16
- document[:id]
17
- end
18
- end
19
- end
20
- end
@@ -1,21 +0,0 @@
1
- module SkinnyControllers
2
- module Params
3
- class JsonApi
4
- attr_accessor :document, :model_key
5
-
6
- # @param [Params] params from controller
7
- def initialize(params, model_key)
8
- self.document = JsonApiDocument.new(params)
9
- self.model_key = model_key
10
- end
11
-
12
- def model_params
13
- document.attributes
14
- end
15
-
16
- def id
17
- document.id
18
- end
19
- end
20
- end
21
- end