jsonapi_for_rails 0.2.0 → 0.2.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
- checksums.yaml.gz.sig +1 -1
- data.tar.gz.sig +0 -0
- data/lib/jsonapi_for_rails.rb +1 -0
- data/lib/jsonapi_for_rails/controller/actions/object.rb +33 -20
- data/lib/jsonapi_for_rails/controller/actions/relationship.rb +7 -7
- data/lib/jsonapi_for_rails/controller/before_actions/record.rb +1 -1
- data/lib/jsonapi_for_rails/controller/before_actions/relationship.rb +6 -3
- data/lib/jsonapi_for_rails/controller/utils/render.rb +3 -3
- data/lib/jsonapi_for_rails/version.rb +1 -1
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6afda53ef8dd36273d0b5365f89058551f62a7ef
|
4
|
+
data.tar.gz: fe345dd87616f5026a78093225a39097dff12c40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b5f3aa2d2afa1dca312b61350aa9f962cc82f01cb34383b416f982ad078ca2793a8050782aa5646aeaade5a2a934222b1af2368530c894255595d45e81d580b
|
7
|
+
data.tar.gz: 4675588d3f5ac6b88528da09b31de0056f761c404477a7bb9ab6d22099a307fdf8ab0a25fc5774ebf25e365a71b348595c171a6533bf6a9a3aa6ca2676f0f667
|
checksums.yaml.gz.sig
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
�x�=�V�����9R�5�����S��9���`�w��d9\Ģ�L�%lv�5�;k!j鳝����8"]��8���j�{8�DВ��U^6O���q��F�p;�$�V"�МDb�D���{������Y��6~���ܷ��ɴ�Q+�c�p���K��Q;��A��uoRu����zԥ>%ё�/l�^�*��~�n��oM����ҍ���w��緦xC ��|��8��M�c�����mVb{F�EU�
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/jsonapi_for_rails.rb
CHANGED
@@ -22,5 +22,6 @@ require "jsonapi_for_rails/model"
|
|
22
22
|
ActionController::Metal.send :include, JsonapiForRails::Controller
|
23
23
|
|
24
24
|
# Add 'to_jsonapi_hash' instance method to models
|
25
|
+
# TODO: ApplicationRecord instead of ActiveRecord::Base
|
25
26
|
ActiveRecord::Base.send :include, JsonapiForRails::Model
|
26
27
|
|
@@ -27,7 +27,7 @@ module JsonapiForRails::Controller
|
|
27
27
|
}
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
jsonapi_render @json
|
31
31
|
end
|
32
32
|
|
33
33
|
# implements Create and Update operations
|
@@ -35,7 +35,7 @@ module JsonapiForRails::Controller
|
|
35
35
|
|
36
36
|
# attributes
|
37
37
|
begin
|
38
|
-
attrs =
|
38
|
+
attrs = jsonapi_received_attributes
|
39
39
|
if attrs
|
40
40
|
if @jsonapi_record
|
41
41
|
# update
|
@@ -47,15 +47,15 @@ module JsonapiForRails::Controller
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
rescue NameError => e
|
50
|
-
|
50
|
+
jsonapi_render_errors 500, "Model class not found."
|
51
51
|
return
|
52
52
|
rescue
|
53
|
-
|
53
|
+
jsonapi_render_errors 500, @jsonapi_record.to_jsonapi_errors_hash
|
54
54
|
return
|
55
55
|
end
|
56
56
|
|
57
57
|
# relationships
|
58
|
-
|
58
|
+
jsonapi_received_relationships.each do |relationship|
|
59
59
|
begin
|
60
60
|
# to-one
|
61
61
|
if relationship[:definition][:type] == :to_one
|
@@ -73,7 +73,7 @@ module JsonapiForRails::Controller
|
|
73
73
|
|
74
74
|
rescue
|
75
75
|
# Should not happen
|
76
|
-
|
76
|
+
jsonapi_render_errors 500, "Relationship could not be created."
|
77
77
|
return
|
78
78
|
end
|
79
79
|
end
|
@@ -149,7 +149,7 @@ module JsonapiForRails::Controller
|
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
152
|
-
|
152
|
+
jsonapi_render @json
|
153
153
|
end
|
154
154
|
|
155
155
|
def update
|
@@ -157,7 +157,7 @@ module JsonapiForRails::Controller
|
|
157
157
|
end
|
158
158
|
|
159
159
|
def destroy
|
160
|
-
|
160
|
+
jsonapi_render_errors 500, "Not implemented."
|
161
161
|
end
|
162
162
|
|
163
163
|
# private
|
@@ -166,7 +166,7 @@ module JsonapiForRails::Controller
|
|
166
166
|
# Use this for creating/updating a database record.
|
167
167
|
# Note that relationships (has_one associations etc) are filtered out
|
168
168
|
# but are still available in the original params.
|
169
|
-
def
|
169
|
+
def jsonapi_received_attributes
|
170
170
|
begin
|
171
171
|
params.require(
|
172
172
|
:data
|
@@ -192,9 +192,10 @@ module JsonapiForRails::Controller
|
|
192
192
|
end
|
193
193
|
end
|
194
194
|
|
195
|
-
|
195
|
+
# Definitions of all relationships for current model
|
196
|
+
def jsonapi_relationships
|
196
197
|
jsonapi_model_class.reflect_on_all_associations.collect do |association|
|
197
|
-
type = nil
|
198
|
+
#type = nil
|
198
199
|
|
199
200
|
type = :to_one if [
|
200
201
|
ActiveRecord::Reflection::HasOneReflection,
|
@@ -212,7 +213,7 @@ module JsonapiForRails::Controller
|
|
212
213
|
name: association.name,
|
213
214
|
type: type,
|
214
215
|
receiver: {
|
215
|
-
type:
|
216
|
+
type: association.klass.to_s.underscore.pluralize.to_sym,
|
216
217
|
class: association.klass.to_s.constantize
|
217
218
|
}
|
218
219
|
}
|
@@ -220,28 +221,40 @@ module JsonapiForRails::Controller
|
|
220
221
|
end
|
221
222
|
|
222
223
|
# TODO: define a separate method for relationship actions (i.e. when params[:relationship] != nil)
|
223
|
-
def
|
224
|
-
|
225
|
-
|
224
|
+
def jsonapi_received_relationships
|
225
|
+
# Relationship definitions for current model
|
226
|
+
rels = jsonapi_relationships
|
227
|
+
|
228
|
+
# Consider only current relationship for relationship actions
|
229
|
+
# (params[:relationship] contains the relationship name)
|
230
|
+
if params[:relationship]
|
231
|
+
|
226
232
|
rels.select! do |rel|
|
227
233
|
rel[:name].to_sym == params[:relationship].to_sym
|
228
234
|
end
|
235
|
+
|
236
|
+
# If no relationship is received, then return the definition only
|
229
237
|
if request.method == "GET"
|
230
|
-
# no relationship received, return definition only
|
231
238
|
return rels.collect do |rel|
|
232
239
|
{definition: rel}
|
233
240
|
end
|
234
241
|
end
|
242
|
+
|
235
243
|
end
|
244
|
+
|
236
245
|
rels.collect do |relationship|
|
237
246
|
begin
|
238
247
|
received_params = nil
|
248
|
+
|
249
|
+
# Relationship action
|
239
250
|
if params[:relationship]
|
240
251
|
received_params = params.permit({
|
241
252
|
data: [
|
242
253
|
:type, :id
|
243
254
|
]
|
244
255
|
})
|
256
|
+
|
257
|
+
# Object action
|
245
258
|
else
|
246
259
|
received_params = params.require(
|
247
260
|
:data
|
@@ -268,7 +281,7 @@ module JsonapiForRails::Controller
|
|
268
281
|
break
|
269
282
|
end
|
270
283
|
received_params[:data].each do |item|
|
271
|
-
next if item[:type] == relationship[:receiver][:type]
|
284
|
+
next if item[:type].to_sym == relationship[:receiver][:type]
|
272
285
|
conformant = false
|
273
286
|
break
|
274
287
|
end
|
@@ -331,9 +344,9 @@ module JsonapiForRails::Controller
|
|
331
344
|
|
332
345
|
def self.run_macros receiver
|
333
346
|
receiver.instance_exec do
|
334
|
-
private :
|
335
|
-
private :
|
336
|
-
private :
|
347
|
+
private :jsonapi_received_attributes
|
348
|
+
private :jsonapi_relationships
|
349
|
+
private :jsonapi_received_relationships
|
337
350
|
end
|
338
351
|
end
|
339
352
|
|
@@ -41,13 +41,13 @@ module JsonapiForRails::Controller
|
|
41
41
|
}
|
42
42
|
end
|
43
43
|
|
44
|
-
|
44
|
+
jsonapi_render @json
|
45
45
|
end
|
46
46
|
|
47
47
|
# PATCH
|
48
48
|
def relationship_update
|
49
49
|
if @jsonapi_relationship[:definition][:type] == :to_many
|
50
|
-
|
50
|
+
jsonapi_render_errors 403, 'Replacing all members of a to-many relationship is forbidden.'
|
51
51
|
return
|
52
52
|
end
|
53
53
|
|
@@ -57,7 +57,7 @@ module JsonapiForRails::Controller
|
|
57
57
|
@jsonapi_relationship[:params][:data][:id]
|
58
58
|
)
|
59
59
|
unless related
|
60
|
-
|
60
|
+
jsonapi_render_errors 403, 'Record not found.'
|
61
61
|
return
|
62
62
|
end
|
63
63
|
end
|
@@ -71,7 +71,7 @@ module JsonapiForRails::Controller
|
|
71
71
|
# POST for to-many relations only
|
72
72
|
def relationship_add
|
73
73
|
unless @jsonapi_relationship[:definition][:type] == :to_many
|
74
|
-
|
74
|
+
jsonapi_render_errors 403, 'Operation allowed for to-many relationships only.'
|
75
75
|
return
|
76
76
|
end
|
77
77
|
|
@@ -80,7 +80,7 @@ module JsonapiForRails::Controller
|
|
80
80
|
record[:id]
|
81
81
|
)
|
82
82
|
unless record
|
83
|
-
|
83
|
+
jsonapi_render_errors 403, "Non-existing record #{record.inspect}."
|
84
84
|
return
|
85
85
|
end
|
86
86
|
record
|
@@ -96,7 +96,7 @@ module JsonapiForRails::Controller
|
|
96
96
|
# DELETE for to-many relations only
|
97
97
|
def relationship_remove
|
98
98
|
unless @jsonapi_relationship[:definition][:type] == :to_many
|
99
|
-
|
99
|
+
jsonapi_render_errors 403, 'Operation allowed for to-many relationships only.'
|
100
100
|
return
|
101
101
|
end
|
102
102
|
|
@@ -105,7 +105,7 @@ module JsonapiForRails::Controller
|
|
105
105
|
record[:id]
|
106
106
|
)
|
107
107
|
unless record
|
108
|
-
|
108
|
+
jsonapi_render_errors 403, "Non-existing record #{record.inspect}."
|
109
109
|
return
|
110
110
|
end
|
111
111
|
record
|
@@ -22,11 +22,14 @@ module JsonapiForRails::Controller
|
|
22
22
|
|
23
23
|
module InstanceMethods
|
24
24
|
def jsonapi_require_relationship
|
25
|
-
#$stderr.puts "
|
26
|
-
|
25
|
+
#$stderr.puts "@@@@@@@@@@@@@@ jsonapi_require_relationship"
|
26
|
+
#log_request
|
27
|
+
#$stderr.puts "#{jsonapi_received_relationships.inspect}"
|
28
|
+
|
29
|
+
@jsonapi_relationship = jsonapi_received_relationships.first
|
27
30
|
return if @jsonapi_relationship
|
28
31
|
|
29
|
-
|
32
|
+
jsonapi_render_errors 401, "Bad request."
|
30
33
|
end
|
31
34
|
end
|
32
35
|
end
|
@@ -21,12 +21,12 @@ module JsonapiForRails::Controller
|
|
21
21
|
|
22
22
|
def self.run_macros receiver
|
23
23
|
receiver.instance_exec do
|
24
|
-
private :
|
24
|
+
private :jsonapi_render, :jsonapi_render_errors
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
module InstanceMethods
|
29
|
-
def
|
29
|
+
def jsonapi_render object
|
30
30
|
# Status code
|
31
31
|
@jsonapi_status = 200
|
32
32
|
|
@@ -44,7 +44,7 @@ module JsonapiForRails::Controller
|
|
44
44
|
response.headers['Content-Type'] = @jsonapi_content_type
|
45
45
|
end
|
46
46
|
|
47
|
-
def
|
47
|
+
def jsonapi_render_errors status, argument
|
48
48
|
# Status code
|
49
49
|
@jsonapi_status = status
|
50
50
|
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|