jsonapi_for_rails 0.2.0 → 0.2.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: 69b79b4230ffd98fd6046ddcc70ef3469b39fafc
4
- data.tar.gz: 66864910b74b564e07f9c18e25c346c665656d5a
3
+ metadata.gz: 6afda53ef8dd36273d0b5365f89058551f62a7ef
4
+ data.tar.gz: fe345dd87616f5026a78093225a39097dff12c40
5
5
  SHA512:
6
- metadata.gz: 0b7a960d5e6778940a75a9be27c5af70424ea71264b0032c8fa5deded47db55670642b7c88fbf951abd451c003ec12cac29c65889fcf0fdf84b4753db14a946c
7
- data.tar.gz: d9e63036c68538bdf502a677f7ca6452a3293abb5b060f29e165a5976d38d26e849f07dbf143a543e067a9a90953f53b76f7952e4a01ebffe8849a0cd7269887
6
+ metadata.gz: 5b5f3aa2d2afa1dca312b61350aa9f962cc82f01cb34383b416f982ad078ca2793a8050782aa5646aeaade5a2a934222b1af2368530c894255595d45e81d580b
7
+ data.tar.gz: 4675588d3f5ac6b88528da09b31de0056f761c404477a7bb9ab6d22099a307fdf8ab0a25fc5774ebf25e365a71b348595c171a6533bf6a9a3aa6ca2676f0f667
@@ -1 +1 @@
1
- ��j�����qW)T_6C{���ڠ���r?�dWhL��0�ڒ�_�^&��wLX��eB�M�C&��k'�%���x܋$4J+5�ka�����Z��3W�:a9rR(� �!��5,o*��"�f[��
1
+ x�=�V�����9R5�����S��9���`�w��d9\Ģ�L�%lv5�;k!j鳝����8"]��8���j{8�DВ ��U^6O���q��Fp;�$�V"�МDb�‡D���{������Y ��6~���ܷ��ɴ�Q+�c�p���K��Q;��A��uoRu����zԥ>%ё�/l�^�*��~�n��oM����ҍ���w��緦xC ��|��8��M�c�����mVb{FEU
data.tar.gz.sig CHANGED
Binary file
@@ -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
- render_json @json
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 = received_attributes
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
- render_errors 500, "Model class not found."
50
+ jsonapi_render_errors 500, "Model class not found."
51
51
  return
52
52
  rescue
53
- render_errors 500, @jsonapi_record.to_jsonapi_errors_hash
53
+ jsonapi_render_errors 500, @jsonapi_record.to_jsonapi_errors_hash
54
54
  return
55
55
  end
56
56
 
57
57
  # relationships
58
- received_relationships.each do |relationship|
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
- render_errors 500, "Relationship could not be created."
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
- render_json @json
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
- render_errors 500, "Not implemented."
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 received_attributes
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
- def relationships
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: association.klass.to_s.underscore.pluralize.to_sym,
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 received_relationships
224
- rels = relationships
225
- if params[:relationship] # only one relationship received for relationship action
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 :received_attributes
335
- private :relationships
336
- private :received_relationships
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
- render_json @json
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
- render_errors 403, 'Replacing all members of a to-many relationship is forbidden.'
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
- render_errors 403, 'Record not found.'
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
- render_errors 403, 'Operation allowed for to-many relationships only.'
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
- render_errors 403, "Non-existing record #{record.inspect}."
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
- render_errors 403, 'Operation allowed for to-many relationships only.'
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
- render_errors 403, "Non-existing record #{record.inspect}."
108
+ jsonapi_render_errors 403, "Non-existing record #{record.inspect}."
109
109
  return
110
110
  end
111
111
  record
@@ -38,7 +38,7 @@ module JsonapiForRails::Controller
38
38
  end
39
39
  #$stderr.puts "@jsonapi_record: #{@jsonapi_record.inspect}"
40
40
  return if @jsonapi_record
41
- render_errors 401, "Bad request."
41
+ jsonapi_render_errors 401, "Bad request."
42
42
  end
43
43
 
44
44
  end
@@ -22,11 +22,14 @@ module JsonapiForRails::Controller
22
22
 
23
23
  module InstanceMethods
24
24
  def jsonapi_require_relationship
25
- #$stderr.puts "JsonapiForRails::Controller::RelationshipFromRequest#jsonapi_require_relationship called"
26
- @jsonapi_relationship = received_relationships.first
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
- render_errors 401, "Bad request."
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 :render_json, :render_errors
24
+ private :jsonapi_render, :jsonapi_render_errors
25
25
  end
26
26
  end
27
27
 
28
28
  module InstanceMethods
29
- def render_json object
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 render_errors status, argument
47
+ def jsonapi_render_errors status, argument
48
48
  # Status code
49
49
  @jsonapi_status = status
50
50
 
@@ -1,3 +1,3 @@
1
1
  module JsonapiForRails
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_for_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doga Armangil
metadata.gz.sig CHANGED
Binary file