skinny_controllers 0.3 → 0.3.1
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 +4 -4
- data/README.md +7 -1
- data/lib/skinny_controllers/lookup/model.rb +1 -1
- data/lib/skinny_controllers/lookup/policy.rb +13 -4
- data/lib/skinny_controllers/operation/base.rb +5 -5
- data/lib/skinny_controllers/operation/model_helpers.rb +10 -9
- data/lib/skinny_controllers/policy/allow_all.rb +9 -0
- data/lib/skinny_controllers/policy/default.rb +9 -0
- data/lib/skinny_controllers/policy/deny_all.rb +9 -0
- data/lib/skinny_controllers/version.rb +1 -1
- data/lib/skinny_controllers.rb +3 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab06e95b7202701fab9189c65efc971ca5422b71
|
4
|
+
data.tar.gz: 8696ce2bc5873152f14f1743b7d5ec349c7f31ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 022423564d78053df74fa1ea751a121d02eb45f8349c347ed906daacdf3d5e89751bc9cd462817aedcfd093d400cb415da0994e437f1502fdcd037100401d231
|
7
|
+
data.tar.gz: 830c2b021accec0eca75ee25dadf9e7d423a9d29ff282c7b6da6c1a6f336005e832bc734c437f9fa48840e927943e004cc38748c918e18c0f4626a75f5cd90ae
|
data/README.md
CHANGED
@@ -56,7 +56,7 @@ In `EventSummariesController`, you would make the following additions:
|
|
56
56
|
```ruby
|
57
57
|
class EventSummariesController < ApiController # or whatever your superclass is
|
58
58
|
include SkinnyControllers::Diet
|
59
|
-
model_class = Event
|
59
|
+
self.model_class = Event
|
60
60
|
|
61
61
|
def index
|
62
62
|
render json: model, each_serializer: EventSummariesSerializer
|
@@ -139,3 +139,9 @@ The following options are available:
|
|
139
139
|
|`accessible_to_method`|`is_accessible_to?`| method to call an the object that the user might be able to access |
|
140
140
|
|`accessible_to_scope`| `accessible_to`| scope / class method on an object that the user might be able to access |
|
141
141
|
|`action_map`| see [skinny_controllers.rb](./lib/skinny_controllers.rb#L61)| |
|
142
|
+
|
143
|
+
|
144
|
+
## TODO
|
145
|
+
|
146
|
+
- Configurable Error Renderer
|
147
|
+
- Default to JSON API format errors?
|
@@ -9,7 +9,7 @@ module SkinnyControllers
|
|
9
9
|
def class_from_operation(operation_name)
|
10
10
|
# "Namespace::Model" => "Model"
|
11
11
|
model_name = Model.name_from_operation(operation_name)
|
12
|
-
#
|
12
|
+
# model_name.demodulize
|
13
13
|
|
14
14
|
# "Model" => Model
|
15
15
|
model_name.constantize
|
@@ -6,10 +6,19 @@ module SkinnyControllers
|
|
6
6
|
# @param [String] name the name of the model
|
7
7
|
# @return [Class] the policy class
|
8
8
|
def class_from_model(name)
|
9
|
-
(
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
policy_class_name = class_name_from_model(name)
|
10
|
+
klass = policy_class_name.safe_constantize
|
11
|
+
klass || define_policy_class(policy_class_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def class_name_from_model(name)
|
15
|
+
parent_namespace = namespace.present? ? "#{namespace}::" : ''
|
16
|
+
"#{parent_namespace}#{name}" + SkinnyControllers.policy_suffix
|
17
|
+
end
|
18
|
+
|
19
|
+
def define_policy_class(name)
|
20
|
+
default_policy = SkinnyControllers::Policy::Default
|
21
|
+
Object.const_set(name, default_policy.dup)
|
13
22
|
end
|
14
23
|
|
15
24
|
# @param [String] class_name name of the operation class
|
@@ -39,23 +39,23 @@ module SkinnyControllers
|
|
39
39
|
@id_from_params
|
40
40
|
end
|
41
41
|
|
42
|
-
def
|
43
|
-
@
|
42
|
+
def model_class
|
43
|
+
@model_class ||= Lookup::Model.class_from_operation(self.class.name)
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
46
|
+
def model_name
|
47
47
|
@object_type_name ||= Lookup::Model.name_from_operation(self.class.name)
|
48
48
|
end
|
49
49
|
|
50
50
|
def association_name_from_object
|
51
|
-
|
51
|
+
model_name.tableize
|
52
52
|
end
|
53
53
|
|
54
54
|
# Takes the class name of self and converts it to a Policy class name
|
55
55
|
#
|
56
56
|
# @example In Operation::Event::Read, Policy::EventPolicy is returned
|
57
57
|
def policy_class
|
58
|
-
@policy_class ||= Lookup::Policy.class_from_model(
|
58
|
+
@policy_class ||= Lookup::Policy.class_from_model(model_name)
|
59
59
|
end
|
60
60
|
|
61
61
|
# Converts the class name to the method name to call on the policy
|
@@ -5,20 +5,21 @@ module SkinnyControllers
|
|
5
5
|
# TODO: not sure if multiple ids is a good idea here
|
6
6
|
# if we don't have a(ny) id(s), get all of them
|
7
7
|
@model ||=
|
8
|
-
if
|
9
|
-
model_from_id
|
10
|
-
elsif params[:scope]
|
8
|
+
if params[:scope]
|
11
9
|
model_from_scope
|
12
10
|
elsif (key = params.keys.grep(/\_id$/)).present?
|
13
11
|
# hopefully there is only ever one of these passed
|
14
|
-
|
12
|
+
id = params[key.first]
|
13
|
+
model_from_named_id(key.first, id)
|
14
|
+
elsif id_from_params
|
15
|
+
model_from_id
|
15
16
|
else
|
16
17
|
model_from_params
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
20
21
|
def sanitized_params
|
21
|
-
keys = (
|
22
|
+
keys = (model_class.column_names & params.keys)
|
22
23
|
params.slice(*keys).symbolize_keys
|
23
24
|
end
|
24
25
|
|
@@ -35,7 +36,7 @@ module SkinnyControllers
|
|
35
36
|
end
|
36
37
|
|
37
38
|
def model_from_params
|
38
|
-
ar_proxy =
|
39
|
+
ar_proxy = model_class.where(sanitized_params)
|
39
40
|
|
40
41
|
if ar_proxy.respond_to? SkinnyControllers.accessible_to_scope
|
41
42
|
# It's better to filter in sql, than in the app, so if there is
|
@@ -47,8 +48,8 @@ module SkinnyControllers
|
|
47
48
|
ar_proxy
|
48
49
|
end
|
49
50
|
|
50
|
-
def model_from_named_id(key)
|
51
|
-
name,
|
51
|
+
def model_from_named_id(key, id)
|
52
|
+
name, _id = key.split('_')
|
52
53
|
name = name.camelize
|
53
54
|
model_from_scope(
|
54
55
|
id: id,
|
@@ -66,7 +67,7 @@ module SkinnyControllers
|
|
66
67
|
end
|
67
68
|
|
68
69
|
def model_from_id
|
69
|
-
|
70
|
+
model_class.find(id_from_params)
|
70
71
|
end
|
71
72
|
end
|
72
73
|
end
|
data/lib/skinny_controllers.rb
CHANGED
@@ -12,6 +12,9 @@ require 'skinny_controllers/lookup/model'
|
|
12
12
|
require 'skinny_controllers/lookup/operation'
|
13
13
|
require 'skinny_controllers/lookup/policy'
|
14
14
|
require 'skinny_controllers/policy/base'
|
15
|
+
require 'skinny_controllers/policy/default'
|
16
|
+
require 'skinny_controllers/policy/deny_all'
|
17
|
+
require 'skinny_controllers/policy/allow_all'
|
15
18
|
require 'skinny_controllers/operation/model_helpers'
|
16
19
|
require 'skinny_controllers/operation/base'
|
17
20
|
require 'skinny_controllers/operation/default'
|
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:
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- L. Preston Sego III
|
@@ -196,7 +196,10 @@ files:
|
|
196
196
|
- lib/skinny_controllers/operation/base.rb
|
197
197
|
- lib/skinny_controllers/operation/default.rb
|
198
198
|
- lib/skinny_controllers/operation/model_helpers.rb
|
199
|
+
- lib/skinny_controllers/policy/allow_all.rb
|
199
200
|
- lib/skinny_controllers/policy/base.rb
|
201
|
+
- lib/skinny_controllers/policy/default.rb
|
202
|
+
- lib/skinny_controllers/policy/deny_all.rb
|
200
203
|
- lib/skinny_controllers/version.rb
|
201
204
|
homepage: https://github.com/NullVoxPopuli/skinny-controllers
|
202
205
|
licenses:
|
@@ -221,5 +224,5 @@ rubyforge_project:
|
|
221
224
|
rubygems_version: 2.4.8
|
222
225
|
signing_key:
|
223
226
|
specification_version: 4
|
224
|
-
summary: SkinnyControllers-0.3
|
227
|
+
summary: SkinnyControllers-0.3.1
|
225
228
|
test_files: []
|