restful_json 3.2.0 → 3.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +25 -7
- data/lib/restful_json/controller.rb +22 -3
- data/lib/restful_json/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -271,15 +271,11 @@ You can have something as simple as:
|
|
271
271
|
|
272
272
|
which would use the restful_json configuration and the controller's classname for the service definition.
|
273
273
|
|
274
|
-
Or you could
|
274
|
+
Or, you could define more bells and whistles (read on to see what these do...):
|
275
275
|
|
276
|
-
class
|
277
|
-
acts_as_restful_json
|
278
|
-
end
|
279
|
-
|
280
|
-
And define more bells and whistles (read on to see what these do...):
|
276
|
+
class FoobarsController < ApplicationController
|
281
277
|
|
282
|
-
|
278
|
+
acts_as_restful_json
|
283
279
|
|
284
280
|
query_for :index, is: ->(t,q) {q.joins(:apples, :pears).where(apples: {color: 'green'}).where(pears: {color: 'green'})}
|
285
281
|
|
@@ -308,6 +304,28 @@ And define more bells and whistles (read on to see what these do...):
|
|
308
304
|
|
309
305
|
end
|
310
306
|
|
307
|
+
##### Parent/Ancestor Class Definition Not Supported
|
308
|
+
|
309
|
+
Have had issues with putting `acts_as_restful_json` in parent/ancestor class, so even though it seems like it would be a good idea at first to move it into a shared parent controller, please keep it in each individual controller, e.g.:
|
310
|
+
|
311
|
+
Don't do this:
|
312
|
+
|
313
|
+
class ServiceController < ApplicationController
|
314
|
+
acts_as_restful_json
|
315
|
+
end
|
316
|
+
|
317
|
+
class FoobarsController < ServiceController
|
318
|
+
acts_as_restful_json
|
319
|
+
end
|
320
|
+
|
321
|
+
It may appear to work when using the same controller, but when you make requests to more than one controller sharing the same parent/ancestor that define `acts_as_restful_json`, it may fail in very strange ways that are hard to diagnose from the error message.
|
322
|
+
|
323
|
+
Do this instead:
|
324
|
+
|
325
|
+
class FoobarsController < ApplicationController
|
326
|
+
acts_as_restful_json
|
327
|
+
end
|
328
|
+
|
311
329
|
#### Default Filtering by Attribute(s)
|
312
330
|
|
313
331
|
First, declare in the controller:
|
@@ -4,6 +4,20 @@ require 'active_model_serializers'
|
|
4
4
|
require 'strong_parameters'
|
5
5
|
require 'cancan'
|
6
6
|
|
7
|
+
# The restful_json controller module. This module (RestfulJson::Controller) is included on ActionController
|
8
|
+
# and then each individual controller should call acts_as_restful_json.
|
9
|
+
#
|
10
|
+
# Only use acts_as_restful_json in each individual service controller rather than a parent or
|
11
|
+
# ancestor class of the service controller. class_attribute's are supposed to work when you subclass,
|
12
|
+
# if you use setters (=, =+ to add to array instead of <<, etc.) but we have seen strange errors
|
13
|
+
# about missing columns, etc. related to the model_class, etc. being wrong if you share a
|
14
|
+
# parent/ancestor class that acts_as_restful_json and then switch back and forth between controllers.
|
15
|
+
# Why? The controller class overrides the shared class_attribute's when the controller class loads,
|
16
|
+
# which other than re-loading via Rails autoload, only happens once; so you hit one controller, then the
|
17
|
+
# the other, it starts overwriting/adding to attributes, and then when you hit the first one again, no
|
18
|
+
# class method calling on class instantiation is being called, so it is using the wrong model. That is
|
19
|
+
# bad, so don't do that.
|
20
|
+
#
|
7
21
|
module RestfulJson
|
8
22
|
module Controller
|
9
23
|
extend ActiveSupport::Concern
|
@@ -358,11 +372,12 @@ module RestfulJson
|
|
358
372
|
@value.save
|
359
373
|
instance_variable_set(@model_at_singular_name_sym, @value)
|
360
374
|
|
361
|
-
if self.render_enabled
|
362
|
-
custom_action_serializer = self.action_to_serializer[params[:action].to_s]
|
375
|
+
if self.render_enabled
|
363
376
|
if !@value.nil? && RestfulJson.return_resource
|
364
377
|
respond_with(@value) do |format|
|
365
378
|
format.json do
|
379
|
+
# define local variables in blocks, not outside of them, to be safe, even though would work in this case
|
380
|
+
custom_action_serializer = self.action_to_serializer[params[:action].to_s]
|
366
381
|
if @value.errors.empty?
|
367
382
|
render custom_action_serializer ? {json: @value, status: :created, serializer: custom_action_serializer} : {json: @value, status: :created}
|
368
383
|
else
|
@@ -371,6 +386,7 @@ module RestfulJson
|
|
371
386
|
end
|
372
387
|
end
|
373
388
|
else
|
389
|
+
custom_action_serializer = self.action_to_serializer[params[:action].to_s]
|
374
390
|
respond_with @value, custom_action_serializer ? {serializer: custom_action_serializer} : {}
|
375
391
|
end
|
376
392
|
else
|
@@ -396,10 +412,11 @@ module RestfulJson
|
|
396
412
|
instance_variable_set(@model_at_singular_name_sym, @value)
|
397
413
|
|
398
414
|
if self.render_enabled
|
399
|
-
custom_action_serializer = self.action_to_serializer[params[:action].to_s]
|
400
415
|
if !@value.nil? && RestfulJson.return_resource
|
401
416
|
respond_with(@value) do |format|
|
402
417
|
format.json do
|
418
|
+
# define local variables in blocks, not outside of them, to be safe, even though would work in this case
|
419
|
+
custom_action_serializer = self.action_to_serializer[params[:action].to_s]
|
403
420
|
if @value.errors.empty?
|
404
421
|
render custom_action_serializer ? {json: @value, status: :ok, serializer: custom_action_serializer} : {json: @value, status: :ok}
|
405
422
|
else
|
@@ -408,6 +425,7 @@ module RestfulJson
|
|
408
425
|
end
|
409
426
|
end
|
410
427
|
else
|
428
|
+
custom_action_serializer = self.action_to_serializer[params[:action].to_s]
|
411
429
|
respond_with @value, custom_action_serializer ? {serializer: custom_action_serializer} : {}
|
412
430
|
end
|
413
431
|
else
|
@@ -423,6 +441,7 @@ module RestfulJson
|
|
423
441
|
instance_variable_set(@model_at_singular_name_sym, @value)
|
424
442
|
|
425
443
|
if self.render_enabled
|
444
|
+
custom_action_serializer = self.action_to_serializer[params[:action].to_s]
|
426
445
|
respond_with @value, custom_action_serializer ? {serializer: custom_action_serializer} : {}
|
427
446
|
else
|
428
447
|
@value
|
data/lib/restful_json/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.2.
|
4
|
+
version: 3.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-03-
|
13
|
+
date: 2013-03-26 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: actionpack
|