skinny_controllers 0.5.3 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17d5297816d2a7c0b58893b33a13bbc9b3325298
4
- data.tar.gz: 1dc6ed173769df962b8a6716d5178b1abd07650a
3
+ metadata.gz: 37c23f99827b6f219c4d8c9639047679d50e71c5
4
+ data.tar.gz: ba943a28a5d56c8f61245d70466fbf071aeb1c98
5
5
  SHA512:
6
- metadata.gz: f109e9e339bacf50c204a3a925e1e6dbd04f8dcb53acefa4e9ec647b27ddc36eb8b1fb8ef068ad96046ea9d5b7c33543ae66bb9bf2713ef43c69d4d008f4abc4
7
- data.tar.gz: df06896ca96b9706afc6c91c43cd12d044e8bb028ebce4020dbbfe949d677a5ae537c68136ff493543cea87b4afdd40fa6e9d5cf50209bc1efd67752d0235392
6
+ metadata.gz: f17ce4c443b039f831b12eee469de045571fc963aa5f0657363965773427dec8476ea61a9e55cdebdbae8836e3d7d2c0d01ef546d8bbed10cc4e4e69f8d4ee4a
7
+ data.tar.gz: 3bd1b95c27f8b5fce730bfb7caafa2adf90e1a3b2a9ea0d418baa0411c89dc898cd1ad42a1fdbb186acbd7d20b2d85d9f9f97bfeb09cd5a9553223884bbad4e1
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # skinny-controllers
1
+ # skinny_controllers
2
2
  [![Gem Version](https://badge.fury.io/rb/skinny_controllers.svg)](https://badge.fury.io/rb/skinny_controllers)
3
3
  [![Build Status](https://travis-ci.org/NullVoxPopuli/skinny_controllers.svg?branch=master)](https://travis-ci.org/NullVoxPopuli/skinny_controllers)
4
4
  [![Code Climate](https://codeclimate.com/github/NullVoxPopuli/skinny_controllers/badges/gpa.svg)](https://codeclimate.com/github/NullVoxPopuli/skinny_controllers)
@@ -89,7 +89,7 @@ private
89
89
  def host_from_subdomain
90
90
  @host ||= HostOperations::Read.new(current_user, host_params).run
91
91
  end
92
-
92
+
93
93
  def host_params
94
94
  params.permit(:subdomain)
95
95
  end
@@ -149,7 +149,7 @@ end
149
149
  ### Updating
150
150
  ```ruby
151
151
  module UserOperations
152
- class Create < SkinnyControllers::Operation::Base
152
+ class Update < SkinnyControllers::Operation::Base
153
153
  def run
154
154
  return unless allowed?
155
155
  model.update(model_params)
@@ -219,7 +219,7 @@ The following options are available:
219
219
  |`accessible_to_method`|`is_accessible_to?`| method to call an the object that the user might be able to access |
220
220
  |`accessible_to_scope`| `accessible_to`| scope / class method on an object that the user might be able to access |
221
221
  |`action_map`| see [skinny_controllers.rb](./lib/skinny_controllers.rb#L61)| |
222
-
222
+ |`params_format`|`:json`| or `:json_api`|
223
223
 
224
224
  ## TODO
225
225
 
@@ -0,0 +1,29 @@
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 = data['id']
14
+ self.attributes = format_keys(data['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
@@ -30,7 +30,7 @@ module SkinnyControllers
30
30
 
31
31
  def id_from_params
32
32
  unless @id_from_params
33
- @id_from_params = params[:id]
33
+ @id_from_params = params[:data] ? params[:data][:id] : params[:id]
34
34
  if filter = params[:filter]
35
35
  @id_from_params = filter[:id].split(',')
36
36
  end
@@ -28,7 +28,20 @@ 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 ||= params[model_param_name].symbolize_keys
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
32
45
  end
33
46
 
34
47
  def model_param_name
@@ -0,0 +1,20 @@
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
@@ -0,0 +1,21 @@
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
@@ -20,7 +20,7 @@ module SkinnyControllers
20
20
  def method_missing(method_name, *args, &block)
21
21
  # if the method ends in a question mark, re-route to default
22
22
  if method_name.to_s =~ /(.+)\?/
23
- action = $1
23
+ action = Regexp.last_match(1)
24
24
  # alias destroy to delete
25
25
  # TODO: this means that a destroy method, if defined,
26
26
  # will never be called.... good or bad?
@@ -1,3 +1,3 @@
1
1
  module SkinnyControllers
2
- VERSION = '0.5.3'
2
+ VERSION = '0.5.4'
3
3
  end
@@ -7,6 +7,9 @@ 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'
10
13
  require 'skinny_controllers/lookup/controller'
11
14
  require 'skinny_controllers/lookup/model'
12
15
  require 'skinny_controllers/lookup/operation'
@@ -61,6 +64,10 @@ module SkinnyControllers
61
64
  :accessible_to
62
65
  end
63
66
 
67
+ cattr_accessor :params_format do
68
+ :json # or :json_api
69
+ end
70
+
64
71
  # the diet uses ActionController::Base's
65
72
  # `action_name` method to get the current action.
66
73
  # From that action, we map what verb we want to use for our operation
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skinny_controllers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - L. Preston Sego III
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-27 00:00:00.000000000 Z
11
+ date: 2015-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -189,6 +189,7 @@ 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
192
193
  - lib/skinny_controllers/lookup/controller.rb
193
194
  - lib/skinny_controllers/lookup/model.rb
194
195
  - lib/skinny_controllers/lookup/operation.rb
@@ -196,6 +197,8 @@ files:
196
197
  - lib/skinny_controllers/operation/base.rb
197
198
  - lib/skinny_controllers/operation/default.rb
198
199
  - lib/skinny_controllers/operation/model_helpers.rb
200
+ - lib/skinny_controllers/params/json.rb
201
+ - lib/skinny_controllers/params/json_api.rb
199
202
  - lib/skinny_controllers/policy/allow_all.rb
200
203
  - lib/skinny_controllers/policy/base.rb
201
204
  - lib/skinny_controllers/policy/default.rb
@@ -224,5 +227,5 @@ rubyforge_project:
224
227
  rubygems_version: 2.4.8
225
228
  signing_key:
226
229
  specification_version: 4
227
- summary: SkinnyControllers-0.5.3
230
+ summary: SkinnyControllers-0.5.4
228
231
  test_files: []