strong_resources 0.1.6 → 0.6.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: bb1daeb75ed867b843e6bcd4e4dc2a5f6adfccf5
4
- data.tar.gz: f08f4275ad58016de9de43e69c15b2d195de23e6
3
+ metadata.gz: 06127d5fd4f5c9f0913a1f71ffb939e931e267b6
4
+ data.tar.gz: 04ff9b25c85ede98987536ee36df0cbb541b713b
5
5
  SHA512:
6
- metadata.gz: c3c5d873e46cf32a0298e8d0f8b5a94be90a64551d50d59d05c6acfe5c6c38b5b28dd00cb05c6eee256d038e06c064a46a62c9fc375d04a2df09175eb9b7f363
7
- data.tar.gz: 4a0d314a8e02ec93776e1ccc249f41e3764c6453ef2e6095b99e8f57e7b36aead5029d605816d9f2fbe2bcb51c2ea4f7eb8821e9d1231a72283cd5161ee520f9
6
+ metadata.gz: f488ee7c0225d22cc92a7e573d947dc714bc24b44c9c44fc3ede2177d5be8d3f6be4263744c73ea7e30dd9dbfcb63b42dffe0eabc0c96401037fa0a9c85d7f22
7
+ data.tar.gz: 2a844362aec52e073ff1e26af09a44faf5dd16377627b94f28c0dbba76c9602ebd7c2170782441eb3152c9039ab95fda0f0c35cc25764772dd4232972f69c225
@@ -10,20 +10,52 @@ module StrongResources
10
10
  end
11
11
  end
12
12
 
13
+ # TODO: refactor
14
+ def apply_strong_params
15
+ deserializer = deserialized_params
16
+
17
+ unless deserializer.attributes.respond_to?(:permit)
18
+ deserializer.attributes = ActionController::Parameters.new(deserializer.attributes)
19
+ end
20
+ deserializer.attributes = deserializer.attributes.permit(strong_resource)
21
+
22
+ deserializer.relationships.each_pair do |name, relationship_payload|
23
+ [relationship_payload].flatten.each do |rp|
24
+ apply_strong_param(rp, strong_resource[:relationships][name])
25
+ end
26
+ end
27
+ end
28
+
29
+ # TODO: refactor
30
+ def apply_strong_param(relationship_payload, permitted)
31
+ if relationship_payload[:meta][:method] == :disassociate
32
+ raise 'not allowed disass' unless permitted[:_disassociate]
33
+ end
34
+
35
+ if relationship_payload[:meta][:method] == :destroy
36
+ raise 'not allowed destroy' unless permitted[:_destroy]
37
+ end
38
+
39
+ unless relationship_payload[:attributes].respond_to?(:permit)
40
+ relationship_payload[:attributes] = ActionController::Parameters.new(relationship_payload[:attributes])
41
+ end
42
+
43
+ relationship_payload[:attributes] = relationship_payload[:attributes].permit(permitted)
44
+ relationship_payload[:relationships].each_pair do |name, rp|
45
+ [rp].flatten.each do |_rp|
46
+ apply_strong_param(_rp, permitted[:relationships][name])
47
+ end
48
+ end
49
+ end
50
+
13
51
  def strong_resource
14
52
  resource = self.class._strong_resources[action_name.to_sym]
15
53
  _params = params
16
- _params = _params.require(resource.require) if resource.require
17
- _params.permit(resource.permits(self))
18
- end
19
-
20
- def update_action?
21
- action_name == 'update'
54
+ resource.permits(self)
22
55
  end
23
56
 
24
57
  module ClassMethods
25
58
  def strong_resource(name, opts = {}, &blk)
26
- opts[:require] ||= name unless opts[:require] == false
27
59
  resource = StrongResource.from(name, opts, &blk)
28
60
 
29
61
  resources = { create: resource, update: resource }
@@ -1,16 +1,17 @@
1
1
  module StrongResources
2
2
  class StrongResource
3
- attr_accessor :attributes, :relations, :require, :relation_type,
3
+ attr_accessor :attributes,
4
+ :relations,
5
+ :relation_type,
4
6
  :only,
5
7
  :except,
6
- :delete,
8
+ :disassociate,
7
9
  :destroy
8
10
  attr_reader :name, :customized_actions, :jsonapi_type
9
11
 
10
12
  def self.from(name, opts = {}, &blk)
11
13
  config = StrongResources.config.strong_resources[name]
12
14
  resource = new(name)
13
- resource.require = opts[:require]
14
15
  resource.instance_eval(&config[:base])
15
16
  resource.instance_eval(&blk) if blk
16
17
  resource
@@ -32,8 +33,8 @@ module StrongResources
32
33
  end
33
34
  end
34
35
 
35
- def delete?
36
- !!@delete
36
+ def disassociate?
37
+ !!@disassociate
37
38
  end
38
39
 
39
40
  def destroy?
@@ -60,26 +61,26 @@ module StrongResources
60
61
  resource: nil,
61
62
  only: nil,
62
63
  except: nil,
63
- delete: false,
64
+ disassociate: false,
64
65
  destroy: false,
65
66
  &blk)
66
67
  resource_name = resource || name.to_s.singularize.to_sym
67
68
  related_resource = self.class.from(resource_name)
68
69
  related_resource.instance_eval(&blk) if block_given?
69
70
  related_resource.relation_type = :has_many
70
- add_relation(name, related_resource, only, except, delete, destroy)
71
+ add_relation(name, related_resource, only, except, disassociate, destroy)
71
72
  end
72
73
 
73
74
  def belongs_to(name,
74
75
  resource: nil,
75
76
  only: nil,
76
77
  except: nil,
77
- delete: false,
78
+ disassociate: false,
78
79
  destroy: false,
79
80
  &blk)
80
81
  resource_name = resource || name
81
82
  related_resource = self.class.from(resource_name, &blk)
82
- add_relation(name, related_resource, only, except, delete, destroy)
83
+ add_relation(name, related_resource, only, except, disassociate, destroy)
83
84
  end
84
85
 
85
86
  def has_one(*args, &blk)
@@ -92,7 +93,9 @@ module StrongResources
92
93
  related_resource = opts[:resource]
93
94
  related = related_permits(related_resource, controller)
94
95
 
95
- permits.merge!(:"#{relation_name}_attributes" => related)
96
+ permits[:relationships] ||= {}
97
+ permits[:relationships][relation_name] = related
98
+ permits
96
99
  end
97
100
  end
98
101
  end
@@ -101,6 +104,7 @@ module StrongResources
101
104
 
102
105
  def base_permits(resource, controller)
103
106
  permits = {}
107
+ permits.merge!(id: StrongResources.type_for_param(:id))
104
108
  resource.attributes.each_pair do |name, opts|
105
109
  next if (opts[:if] and opts[:if].call(controller) == false)
106
110
  permits.merge!(name => StrongResources.type_for_param(opts[:type]))
@@ -114,27 +118,24 @@ module StrongResources
114
118
  def related_permits(related_resource, controller)
115
119
  related_resource.permits(controller).tap do |permits|
116
120
  permits.merge!(id: StrongResources.type_for_param(:id))
117
-
118
- if controller.update_action?
119
- merge_delete_destroy(related_resource, permits)
120
- end
121
+ merge_disassociate_destroy(related_resource, permits)
121
122
  end
122
123
  end
123
124
 
124
- def merge_delete_destroy(related_resource, permits)
125
- if related_resource.delete?
126
- permits.merge!(_delete: StrongResources.type_for_param(:boolean))
125
+ def merge_disassociate_destroy(related_resource, permits)
126
+ if related_resource.disassociate?
127
+ permits.merge!(_disassociate: true)
127
128
  end
128
129
 
129
130
  if related_resource.destroy?
130
- permits.merge!(_destroy: StrongResources.type_for_param(:boolean))
131
+ permits.merge!(_destroy: true)
131
132
  end
132
133
  end
133
134
 
134
- def add_relation(name, resource, only, except, delete, destroy)
135
+ def add_relation(name, resource, only, except, disassociate, destroy)
135
136
  resource.only = only
136
137
  resource.except = except
137
- resource.delete = delete
138
+ resource.disassociate = disassociate
138
139
  resource.destroy = destroy
139
140
 
140
141
  self.relations[name] = {
@@ -1,3 +1,3 @@
1
1
  module StrongResources
2
- VERSION = "0.1.6"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strong_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-28 00:00:00.000000000 Z
11
+ date: 2017-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: stronger_parameters
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
147
  version: '0'
148
148
  requirements: []
149
149
  rubyforge_project:
150
- rubygems_version: 2.4.5.1
150
+ rubygems_version: 2.6.7
151
151
  signing_key:
152
152
  specification_version: 4
153
153
  summary: Auto-generate swagger docs and strong params