skinny_controllers 0.6.2 → 0.7.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: f16b93f4e0724245262a2ad6a08184e64856c686
4
- data.tar.gz: ef7a73a27ee75c3bbf6b93da279676b441956277
3
+ metadata.gz: bfd1fac9652be4c71127faeea92909e2587462b5
4
+ data.tar.gz: 8ef6996a05fd1dfc06682003c69654b130a4ae9d
5
5
  SHA512:
6
- metadata.gz: f434627729f255be2934939bceb324bb64af99193b4299eb7a512af6b588f35dd0ccbfacfa0133f8d57c955c155aa4c819460e79715c22437d377b2e3d62e14d
7
- data.tar.gz: 4a53721fd7bec0bc449cc2004c491ac4a8949916c080195f792caad2fc1180575e26c1ddea5a80d336e1db48ede60bc267b5920a2f73eb9e85a39fb0e9fe62b2
6
+ metadata.gz: 9c516408f33f026117fc3f2e4a432ed1d6e8ace278417fc69a878e691f80f37b7a075745ae4331d6960c4d77f4780f81f4740be35f0a9bc3316e73d34ab95137
7
+ data.tar.gz: 5c2bf53aa6ae2de2fed209c3bf9caa62c901cebbe5bf5c2fd9b989beb67b9067cb96956ee9326bcc66e24e0b9af850dca0372dab3b466caebce0deaee7b9a7f9
data/README.md CHANGED
@@ -74,6 +74,21 @@ end
74
74
  Note that `each_serializer` and `serializer` is not part of `SkinnyControllers`, and is part of [ActiveModel::Serializers](https://github.com/rails-api/active_model_serializers).
75
75
 
76
76
 
77
+ ### What if your model is namespaced?
78
+
79
+ All you have to do is set the `model_class`, and `model_key`.
80
+
81
+ ```ruby
82
+ class ItemsController < ApiController # or whatever your superclass is
83
+ include SkinnyControllers::Diet
84
+ self.model_class = NameSpace::Item
85
+ self.model_key = 'item'
86
+ end
87
+ ```
88
+ `model_key` specifies the key to look for params when creating / updating the model.
89
+
90
+ Note that while `model_key` doesn't *have* to be specified, it would default to name_space/item. So, just keep that in mind.
91
+
77
92
  ### What if you want to call your own operations?
78
93
 
79
94
  Sometimes, magic is scary. You can call anything you want manually (operations and policies).
@@ -4,13 +4,17 @@ module SkinnyControllers
4
4
 
5
5
  included do
6
6
  cattr_accessor :model_class
7
+ cattr_accessor :model_key
7
8
  end
8
9
 
9
10
  # TODO: what if we want multiple operations per action?
10
11
  #
11
12
  # @return an instance of the operation with default parameters
12
13
  def operation
13
- @operation ||= operation_class.new(current_user, params, params_for_action, action_name)
14
+ @operation ||= operation_class.new(
15
+ current_user,
16
+ params, params_for_action,
17
+ action_name, model_key)
14
18
  end
15
19
 
16
20
  # Assumes the operation name from the controller name
@@ -46,7 +50,9 @@ module SkinnyControllers
46
50
  return {} if action_name == 'destroy'
47
51
 
48
52
  model_key =
49
- if model_class
53
+ if self.model_key.present?
54
+ self.model_key
55
+ elsif model_class
50
56
  # model_class should be a class
51
57
  model_class.name.underscore
52
58
  else
@@ -24,7 +24,8 @@ module SkinnyControllers
24
24
  # Namespace::ModelOperations::Verb => Namespace::ModelOperations
25
25
  namespace = operation_name.deconstantize
26
26
  # Namespace::ModelOperations => ModelOperations
27
- nested_namespace = namespace.demodulize
27
+ # nested_namespace = namespace.demodulize
28
+ nested_namespace = namespace.gsub(SkinnyControllers.operations_namespace, '')
28
29
  # ModelOperations => Model
29
30
  nested_namespace.gsub(SkinnyControllers.operations_suffix, '')
30
31
  end
@@ -0,0 +1,32 @@
1
+ module SkinnyControllers
2
+ module Lookup
3
+ module Namespace
4
+ module_function
5
+
6
+ # iterate through the namespaces until one doesn't exist, and create the rest
7
+ def create_namespace(desired_namespace)
8
+ if klass = desired_namespace.safe_constantize
9
+ return klass
10
+ end
11
+
12
+ namespaces = desired_namespace.split('::')
13
+ existing = []
14
+ previous = Object
15
+
16
+ namespaces.each do |namespace|
17
+ current = (existing + [namespace]).join('::')
18
+ begin
19
+ Object.const_get(current)
20
+ rescue NameError => e
21
+ previous.const_set(namespace, Module.new)
22
+ end
23
+
24
+ existing << namespace
25
+ previous = current.constantize
26
+ end
27
+
28
+ previous
29
+ end
30
+ end
31
+ end
32
+ end
@@ -36,12 +36,15 @@ module SkinnyControllers
36
36
 
37
37
  # @return [Class] namespace for the default operation class
38
38
  def default_operation_namespace_for(model_name)
39
+ # binding.pry
39
40
  desired_namespace = namespace_from_model(model_name)
40
41
  parent_namespace = SkinnyControllers.operations_namespace
41
42
  namespace = "#{parent_namespace}::#{desired_namespace}".safe_constantize
42
- namespace || Object.const_set(desired_namespace, Module.new)
43
+ namespace || Namespace.create_namespace(desired_namespace)
43
44
  end
44
45
 
46
+
47
+
45
48
  # @example 'Object' => 'ObjectOperations'
46
49
  # @return [String] the operation namespace based on the model name
47
50
  def namespace_from_model(model_name)
@@ -18,7 +18,22 @@ module SkinnyControllers
18
18
 
19
19
  def define_policy_class(name)
20
20
  default_policy = SkinnyControllers::Policy::Default
21
- Object.const_set(name, default_policy.dup)
21
+ namespace_klass = Object
22
+ # if we are namespaced, we need to get / create the namespace if it doesn't exist already
23
+ if (name.include?('::'))
24
+ namespaces = name.split('::')
25
+ namespace = namespaces[0..-2].join('::').presence
26
+ namespace = namespace == name ? 'Object' : namespace
27
+ if (namespace.presence && namespace != name )
28
+ namespace_klass = Namespace.create_namespace(namespace)
29
+ end
30
+ end
31
+
32
+ # naw remove the namespace from the name
33
+ name = name.gsub(namespace_klass.name + '::', '')
34
+
35
+ # finally, define the new policy class
36
+ namespace_klass.const_set(name, default_policy.dup)
22
37
  end
23
38
 
24
39
  # @param [String] class_name name of the operation class
@@ -15,7 +15,7 @@ module SkinnyControllers
15
15
  class Base
16
16
  include ModelHelpers
17
17
 
18
- attr_accessor :params, :current_user, :authorized_via_parent, :action, :params_for_action
18
+ attr_accessor :params, :current_user, :authorized_via_parent, :action, :params_for_action, :model_key
19
19
 
20
20
  def self.run(current_user, params)
21
21
  object = new(current_user, params)
@@ -26,12 +26,13 @@ module SkinnyControllers
26
26
  # @param [Hash] controller_params the params hash raw from the controller
27
27
  # @param [Hash] params_for_action optional params hash, generally the result of strong parameters
28
28
  # @param [string] action the current action on the controller
29
- def initialize(current_user, controller_params, params_for_action = nil, action = nil)
29
+ def initialize(current_user, controller_params, params_for_action = nil, action = nil, model_key = nil)
30
30
  self.authorized_via_parent = false
31
31
  self.current_user = current_user
32
32
  self.action = action || controller_params[:action]
33
33
  self.params = controller_params
34
34
  self.params_for_action = params_for_action || controller_params
35
+ self.model_key = model_key
35
36
  end
36
37
 
37
38
  def id_from_params
@@ -49,7 +49,8 @@ module SkinnyControllers
49
49
  end
50
50
 
51
51
  def model_param_name
52
- model_name.underscore
52
+ # model_key comes from Operation::Base
53
+ self.model_key || model_name.underscore
53
54
  end
54
55
 
55
56
  # @param [Hash] scoped_params
@@ -1,3 +1,3 @@
1
1
  module SkinnyControllers
2
- VERSION = '0.6.2'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -7,6 +7,7 @@ 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/lookup/namespace'
10
11
  require 'skinny_controllers/lookup/controller'
11
12
  require 'skinny_controllers/lookup/model'
12
13
  require 'skinny_controllers/lookup/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.6.2
4
+ version: 0.7.0
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: 2016-01-26 00:00:00.000000000 Z
11
+ date: 2016-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -191,6 +191,7 @@ files:
191
191
  - lib/skinny_controllers/diet.rb
192
192
  - lib/skinny_controllers/lookup/controller.rb
193
193
  - lib/skinny_controllers/lookup/model.rb
194
+ - lib/skinny_controllers/lookup/namespace.rb
194
195
  - lib/skinny_controllers/lookup/operation.rb
195
196
  - lib/skinny_controllers/lookup/policy.rb
196
197
  - lib/skinny_controllers/operation/base.rb
@@ -221,8 +222,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
221
222
  version: '0'
222
223
  requirements: []
223
224
  rubyforge_project:
224
- rubygems_version: 2.4.8
225
+ rubygems_version: 2.5.1
225
226
  signing_key:
226
227
  specification_version: 4
227
- summary: SkinnyControllers-0.6.2
228
+ summary: SkinnyControllers-0.7.0
228
229
  test_files: []