skinny_controllers 0.3 → 0.3.1

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: f549f005c93de6eb099efb11b80a429c99077cf0
4
- data.tar.gz: 44dd3003008e227b826fff4c51b3c1e9d28cf9d7
3
+ metadata.gz: ab06e95b7202701fab9189c65efc971ca5422b71
4
+ data.tar.gz: 8696ce2bc5873152f14f1743b7d5ec349c7f31ad
5
5
  SHA512:
6
- metadata.gz: 975ae25a383bee2c3b88d79b126c95be07c379cf69382ce24666ef0963cd89a6e312fa8cd530944f784c0ec63599b2afcdc294678cfe9f13f58bd4c649ee5c6d
7
- data.tar.gz: c0a2877d7e19e90deaa5c5e43f54222ffc256fd2a7b1487ad79fd149011504288ffea052e8510bd9dd164bff20dd319f9e940727277a1d791a01ee683af92e15
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
- # object_type_of_interest.demodulize
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
- "#{namespace}::#{name}" +
11
- SkinnyControllers.policy_suffix
12
- ).constantize
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 object_class
43
- @object_class ||= Lookup::Model.class_from_operation(self.class.name)
42
+ def model_class
43
+ @model_class ||= Lookup::Model.class_from_operation(self.class.name)
44
44
  end
45
45
 
46
- def object_type_of_interest
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
- object_type_of_interest.tableize
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(object_type_of_interest)
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 id_from_params
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
- model_from_named_id(key.first)
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 = (object_class.column_names & params.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 = object_class.where(sanitized_params)
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, id = key.split('_')
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
- object_class.find(id_from_params)
70
+ model_class.find(id_from_params)
70
71
  end
71
72
  end
72
73
  end
@@ -0,0 +1,9 @@
1
+ module SkinnyControllers
2
+ module Policy
3
+ class AllowAll < Base
4
+ def default?; true; end
5
+ def read?; true; end
6
+ def read_all?; true; end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module SkinnyControllers
2
+ module Policy
3
+ class Default < Base
4
+ # See <#Base>.read?
5
+ # See <#Base>.read_all?
6
+ # See <#Base>.default?
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module SkinnyControllers
2
+ module Policy
3
+ class DenyAll < Base
4
+ def default?; false; end
5
+ def read?; false; end
6
+ def read_all?; false; end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module SkinnyControllers
2
- VERSION = 0.3
2
+ VERSION = '0.3.1'
3
3
  end
@@ -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: '0.3'
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: []