jsonapi_compliable 0.6.2 → 0.6.3

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: 25562eb5beee62f18eacf4e9cbeced3d5a245b3f
4
- data.tar.gz: f559d365a3647591fd0d221902303a3e84ff0e06
3
+ metadata.gz: 9138437a5f6cf6613987e8f84a8822d1cf2c84e4
4
+ data.tar.gz: 16ac6bf944f5f04e3573fd8e033a90d83658a97d
5
5
  SHA512:
6
- metadata.gz: efc0088447905a6120acfad7a4fb1d8790e49e3e7732f98b76c3063b3da64e18f4a49dbdb60dee8bb95b9833658ab5a99738e6a9ca2b4d4c1122b081d24602ea
7
- data.tar.gz: f87a8b7b3dc56b0449afbfc67682ae901ab4ebe97db6c4cfdff1caad303a5b98128aca66ba60f1f2a1b3fe6459ff6477171ea3f545a4fcb6ea1cdc021a0472c7
6
+ metadata.gz: e5eb95f420f9811808b528d5e73e40e8f0279e829d307aaaeda6d1127fe3e99d897ce63d56a47b8e3a6ee3b3b25a902d51af68c7bd4a7c7e57a3744a3b721423
7
+ data.tar.gz: d2cb8c48650ca2c1be14683e0bef55f354153efe7d7189d3803d521dd7f22befc34cda32f027810330358e6c81c219d7d401e4382343e2d3b06886601ba00c86
@@ -32,6 +32,7 @@ end
32
32
 
33
33
  require "jsonapi_compliable/extensions/extra_attribute"
34
34
  require "jsonapi_compliable/extensions/boolean_attribute"
35
+ require "jsonapi_compliable/extensions/temp_id"
35
36
 
36
37
  module JsonapiCompliable
37
38
  autoload :Base, 'jsonapi_compliable/base'
@@ -16,7 +16,7 @@ module JsonapiCompliable
16
16
  end
17
17
 
18
18
  def count(scope, attr)
19
- scope.count
19
+ scope.uniq.count
20
20
  end
21
21
 
22
22
  def average(scope, attr)
@@ -48,23 +48,32 @@ module JsonapiCompliable
48
48
  end
49
49
 
50
50
  def jsonapi_create
51
- created = resource.transaction do
51
+ _persist do
52
52
  resource.persist_with_relationships \
53
53
  deserialized_params.meta,
54
54
  deserialized_params.attributes,
55
55
  deserialized_params.relationships
56
56
  end
57
- Util::ValidationResponse.new(created, deserialized_params)
58
57
  end
59
58
 
60
59
  def jsonapi_update
61
- updated = resource.transaction do
60
+ _persist do
62
61
  resource.persist_with_relationships \
63
62
  deserialized_params.meta,
64
63
  deserialized_params.attributes,
65
64
  deserialized_params.relationships
66
65
  end
67
- Util::ValidationResponse.new(updated, deserialized_params)
66
+ end
67
+
68
+ def _persist
69
+ validation_response = nil
70
+ resource.transaction do
71
+ object = yield
72
+ validation_response = Util::ValidationResponse.new \
73
+ object, deserialized_params
74
+ raise Errors::ValidationError unless validation_response.to_a[1]
75
+ end
76
+ validation_response
68
77
  end
69
78
 
70
79
  def perform_render_jsonapi(opts)
@@ -1,6 +1,7 @@
1
1
  module JsonapiCompliable
2
2
  module Errors
3
3
  class BadFilter < StandardError; end
4
+ class ValidationError < StandardError; end
4
5
 
5
6
  class UnsupportedPageSize < StandardError
6
7
  def initialize(size, max)
@@ -0,0 +1,15 @@
1
+ module JsonapiCompliable
2
+ module SerializableTempId
3
+ def as_jsonapi(*)
4
+ super.tap do |hash|
5
+ if temp_id = @object.instance_variable_get(:'@_jsonapi_temp_id')
6
+ hash[:'temp-id'] = temp_id
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ JSONAPI::Serializable::Resource.class_eval do
14
+ prepend JsonapiCompliable::SerializableTempId
15
+ end
@@ -226,9 +226,14 @@ module JsonapiCompliable
226
226
  end
227
227
 
228
228
  def transaction
229
- adapter.transaction(model) do
230
- yield
229
+ response = nil
230
+ begin
231
+ adapter.transaction(model) do
232
+ response = yield
233
+ end
234
+ rescue Errors::ValidationError
231
235
  end
236
+ response
232
237
  end
233
238
  end
234
239
  end
@@ -7,14 +7,40 @@ class JsonapiCompliable::Util::ValidationResponse
7
7
  end
8
8
 
9
9
  def success?
10
- if object.respond_to?(:errors)
11
- object.errors.blank?
12
- else
13
- true
14
- end
10
+ all_valid?(object, @deserialized_params.relationships)
15
11
  end
16
12
 
17
13
  def to_a
18
14
  [object, success?]
19
15
  end
16
+
17
+ private
18
+
19
+ def valid_object?(object)
20
+ object.respond_to?(:errors) && object.errors.blank?
21
+ end
22
+
23
+ def all_valid?(model, deserialized_params)
24
+ valid = true
25
+ return false unless valid_object?(model)
26
+ deserialized_params.each_pair do |name, payload|
27
+ if payload.is_a?(Array)
28
+ related_objects = model.send(name)
29
+ related_objects.each do |r|
30
+ valid = valid_object?(r)
31
+
32
+ if valid
33
+ valid = all_valid?(r, deserialized_params[:relationships] || {})
34
+ end
35
+ end
36
+ else
37
+ related_object = model.send(name)
38
+ valid = valid_object?(related_object)
39
+ if valid
40
+ valid = all_valid?(related_object, deserialized_params[:relationships] || {})
41
+ end
42
+ end
43
+ end
44
+ valid
45
+ end
20
46
  end
@@ -1,3 +1,3 @@
1
1
  module JsonapiCompliable
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_compliable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-04-12 00:00:00.000000000 Z
12
+ date: 2017-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jsonapi-serializable
@@ -153,6 +153,7 @@ files:
153
153
  - lib/jsonapi_compliable/errors.rb
154
154
  - lib/jsonapi_compliable/extensions/boolean_attribute.rb
155
155
  - lib/jsonapi_compliable/extensions/extra_attribute.rb
156
+ - lib/jsonapi_compliable/extensions/temp_id.rb
156
157
  - lib/jsonapi_compliable/query.rb
157
158
  - lib/jsonapi_compliable/rails.rb
158
159
  - lib/jsonapi_compliable/resource.rb
@@ -196,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
197
  version: '0'
197
198
  requirements: []
198
199
  rubyforge_project:
199
- rubygems_version: 2.6.7
200
+ rubygems_version: 2.6.11
200
201
  signing_key:
201
202
  specification_version: 4
202
203
  summary: Easily build jsonapi.org-compatible APIs