restful_json 4.3.1 → 4.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +65 -40
- data/lib/restful_json/config.rb +3 -0
- data/lib/restful_json/controller.rb +10 -4
- data/lib/restful_json/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 939e484c768821d49878165026195240c627d440
|
4
|
+
data.tar.gz: 43e95c66ab6f95d36d9872639018d1f61a34cd2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0197b80bb543238b5c7c702ea467b9098cd1a3345041d755f02697213e125e13caf061e2c0e57b99039caa7319a52dba367754d7c62936d3f95a6513877ba381
|
7
|
+
data.tar.gz: 3081b689fd3ae24b4e0b9a27ca46cee918d766ec59a29f1161b32594b4499919e279570f8080f656bb2833bfaf61380f24cd88dea5515d6e153ba90de993eb69
|
data/README.md
CHANGED
@@ -108,38 +108,44 @@ Be sure to read the [Strong Parameters][strong_parameters] docs, because you nee
|
|
108
108
|
As noted in [Strong Parameters][strong_parameters], it is suggested to encapsulate the permitting into a private method in the controller, so we allow:
|
109
109
|
|
110
110
|
```ruby
|
111
|
-
|
112
|
-
|
113
|
-
|
111
|
+
private
|
112
|
+
|
113
|
+
def foobar_params
|
114
|
+
params.require(:foobar).permit(:name, :age)
|
115
|
+
end
|
114
116
|
```
|
115
117
|
|
116
118
|
or if `self.allow_action_specific_params_methods = true` is set in restful_json configuration, as it is by default:
|
117
119
|
|
118
120
|
```ruby
|
119
|
-
|
120
|
-
params.require(:foobar).permit(:name, :age)
|
121
|
-
end
|
121
|
+
private
|
122
122
|
|
123
|
-
def
|
124
|
-
|
125
|
-
end
|
123
|
+
def create_foobar_params
|
124
|
+
params.require(:foobar).permit(:name, :age)
|
125
|
+
end
|
126
|
+
|
127
|
+
def update_foobar_params
|
128
|
+
params.require(:foobar).permit(:age)
|
129
|
+
end
|
126
130
|
```
|
127
131
|
|
128
|
-
and even other actions if
|
132
|
+
and even other actions, if needed:
|
129
133
|
|
130
134
|
```ruby
|
131
|
-
|
132
|
-
params.require(:foobars).permit(:foo_id)
|
133
|
-
end
|
135
|
+
private
|
134
136
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
end
|
137
|
+
def index_foobars_params
|
138
|
+
params.require(:foobars).permit(:foo_id)
|
139
|
+
end
|
139
140
|
|
140
|
-
|
141
|
-
|
142
|
-
|
141
|
+
# where 'some_action' is a custom action created by query_for
|
142
|
+
def some_action_foobars_params
|
143
|
+
params.require(:foobars).permit(:foo_id)
|
144
|
+
end
|
145
|
+
|
146
|
+
def show_foobar_params
|
147
|
+
params.require(:foobar).permit(:id)
|
148
|
+
end
|
143
149
|
```
|
144
150
|
|
145
151
|
##### Permitters
|
@@ -714,12 +720,9 @@ By convention, a restful_json controller can call the `(singular model name)_par
|
|
714
720
|
```ruby
|
715
721
|
self.actions_supporting_params_methods = [:create, :update]
|
716
722
|
```
|
723
|
+
e.g., starting in restful_json v4.4, in FoobarsController after any other specifics, it would look for a foobar_params method in your controller, and if it is there, it will call it, expecting it to permit params. But, if you change the value of `self.actions_supporting_params_methods` in restful_json config or controller config, it will use whatever you specified.
|
717
724
|
|
718
|
-
|
719
|
-
|
720
|
-
```ruby
|
721
|
-
self.allow_action_specific_params_methods = true
|
722
|
-
```
|
725
|
+
Also, if you have a method like `create_foobar_params`, and it will try to call that on create (and similar for `update_foobar_params` and update).
|
723
726
|
|
724
727
|
##### Avoid n+1 Queries
|
725
728
|
|
@@ -912,13 +915,37 @@ For more realistic use that takes advantage of existing configuration in the con
|
|
912
915
|
|
913
916
|
### Error Handling
|
914
917
|
|
915
|
-
####
|
918
|
+
#### Validation Errors
|
916
919
|
|
917
|
-
|
920
|
+
Validation errors (for validations you put in your models) are handled in the controller and returned as an error response in JSON.
|
921
|
+
|
922
|
+
#### RecordNotFound and Other Non-validation Errors
|
923
|
+
|
924
|
+
##### Rails 4 Default Rack Error Handling
|
918
925
|
|
919
926
|
Rails 4 has basic error handling for non-HTML formats defined in the [public_exceptions][public_exceptions] and [show_exceptions][show_exceptions] Rack middleware.
|
920
927
|
|
921
|
-
Rails
|
928
|
+
To use the Rails 4 default Rack error handling, you need to remove all of the default configuration for rescue handlers in restful_json via:
|
929
|
+
|
930
|
+
```ruby
|
931
|
+
RestfulJson.configure do
|
932
|
+
self.rescue_handlers = []
|
933
|
+
end
|
934
|
+
```
|
935
|
+
|
936
|
+
If you don't want to handle it the way Rack does by default, read on...
|
937
|
+
|
938
|
+
#### Rails 3.2.x+ Rack Exceptions App Customization
|
939
|
+
|
940
|
+
First, ensure that you've unset restful_json's rescue_handlers to use the Rails 3.2+ Rack error handling:
|
941
|
+
|
942
|
+
```ruby
|
943
|
+
RestfulJson.configure do
|
944
|
+
self.rescue_handlers = []
|
945
|
+
end
|
946
|
+
```
|
947
|
+
|
948
|
+
Rails 3.2.x has support for `config.exceptions_app` which can be defined as the following in your Rails app configuration to simulate Rails 4 exception handling, or if you want to customize Rails 4's Rack exception handling:
|
922
949
|
|
923
950
|
```ruby
|
924
951
|
config.exceptions_app = lambda do |env|
|
@@ -953,17 +980,11 @@ Unfortunately, this doesn't work for Rails 3.1.x. However, in many scenarios the
|
|
953
980
|
|
954
981
|
But, if you can make Rack respond a little better for some errors, that's great.
|
955
982
|
|
956
|
-
|
983
|
+
#### The Default Way Non-validation Errors Are Handled
|
957
984
|
|
958
|
-
|
959
|
-
RestfulJson.configure do
|
960
|
-
self.rescue_handlers = []
|
961
|
-
end
|
962
|
-
```
|
963
|
-
|
964
|
-
#### Controller Error-handling Configuration
|
985
|
+
The default configuration will use a configuration to use rescue_handlers which is just a way to configure how the controller's action methods rescue non-validation errors to render a response.
|
965
986
|
|
966
|
-
The
|
987
|
+
The standard configuration will rescue StandardError in each action method and will render as 404 for ActiveRecord::RecordNotFound or 500 for all other StandardError (and ancestors, like a normal rescue).
|
967
988
|
|
968
989
|
There are a few options to customize the rescue and error rendering behavior.
|
969
990
|
|
@@ -993,13 +1014,17 @@ RestfulJson.configure do
|
|
993
1014
|
end
|
994
1015
|
```
|
995
1016
|
|
996
|
-
The `return_error_data` config option will not only return a response with `status` and `error` but also an `error_data` containing the `e.class.name`, `e.message`, and cleaned `e.backtrace`.
|
1017
|
+
The `return_error_data` restful_json config option (true by default) will not only return a response with `status` and `error` but also an `error_data` containing the `e.class.name`, `e.message`, and cleaned `e.backtrace`.
|
997
1018
|
|
998
|
-
|
1019
|
+
You can turn off backtrace cleaning in `error_data` by setting `clean_backtrace` per handler to false, e.g.:
|
999
1020
|
|
1000
1021
|
```ruby
|
1001
1022
|
RestfulJson.configure do
|
1002
|
-
self.
|
1023
|
+
self.rescue_class = Exception
|
1024
|
+
self.rescue_handlers = [
|
1025
|
+
{exception_ancestor_classes: [ActiveRecord::RecordNotFound], status: :not_found, i18n_key: 'api.not_found'.freeze},
|
1026
|
+
{i18n_key: 'api.internal_server_error'.freeze, clean_backtrace: false}
|
1027
|
+
]
|
1003
1028
|
end
|
1004
1029
|
```
|
1005
1030
|
|
data/lib/restful_json/config.rb
CHANGED
@@ -83,6 +83,9 @@ RestfulJson.configure do
|
|
83
83
|
|
84
84
|
# will define order of errors handled and what status and/or i18n message key to use
|
85
85
|
self.rescue_handlers = []
|
86
|
+
|
87
|
+
# default to checking for the StrongParameters default method (singular model name)_params and using it if haven't tried
|
88
|
+
self.actions_supporting_params_methods = [:create, :update]
|
86
89
|
|
87
90
|
# rescue_handlers are an ordered array of handlers to handle rescue of self.rescue_class or sub types.
|
88
91
|
# can use optional i18n_key for message, but will default to e.message if i18n_key not found.
|
@@ -52,6 +52,7 @@ module RestfulJson
|
|
52
52
|
self.param_to_through ||= {}
|
53
53
|
self.action_to_render_options ||= {}
|
54
54
|
self.action_to_query_includes ||= {}
|
55
|
+
self.actions_supporting_params_methods ||= []
|
55
56
|
end
|
56
57
|
|
57
58
|
module ClassMethods
|
@@ -308,7 +309,7 @@ module RestfulJson
|
|
308
309
|
|
309
310
|
def apply_includes(value)
|
310
311
|
this_includes = current_action_includes
|
311
|
-
if this_includes
|
312
|
+
if this_includes && this_includes.size > 0
|
312
313
|
value = value.includes(*this_includes)
|
313
314
|
end
|
314
315
|
value
|
@@ -327,10 +328,14 @@ module RestfulJson
|
|
327
328
|
if self.actions_that_permit.include?(action_sym)
|
328
329
|
if self.use_permitters
|
329
330
|
return permitted_params_using(self.action_to_permitter[action_sym] || permitter_class)
|
330
|
-
elsif self.allow_action_specific_params_methods && respond_to?(action_specific_params_method)
|
331
|
+
elsif self.allow_action_specific_params_methods && self.respond_to?(action_specific_params_method, true)
|
331
332
|
return __send__(action_specific_params_method)
|
332
|
-
elsif self.actions_supporting_params_methods.include?(action_sym)
|
333
|
-
|
333
|
+
elsif self.actions_supporting_params_methods.include?(action_sym)
|
334
|
+
if self.respond_to?(model_name_params_method, true)
|
335
|
+
return __send__(model_name_params_method)
|
336
|
+
elsif defined?(::ActionController::StrongParameters)
|
337
|
+
raise "#{self.class.name} needs a method (can be private): #{model_name_params_method}#{self.allow_action_specific_params_methods ? " or #{action_specific_params_method}" : ''}"
|
338
|
+
end
|
334
339
|
end
|
335
340
|
end
|
336
341
|
|
@@ -350,6 +355,7 @@ module RestfulJson
|
|
350
355
|
#
|
351
356
|
# It handles any format in theory that is supported by respond_to and has a `to_(some format)` method.
|
352
357
|
def render_error(e, handling_data)
|
358
|
+
use_backtrace_cleaner = handling_data[:clean_backtrace] || true
|
353
359
|
i18n_key = handling_data[:i18n_key]
|
354
360
|
msg = t(i18n_key, default: e.message)
|
355
361
|
status = handling_data[:status] || :internal_server_error
|
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: 4.
|
4
|
+
version: 4.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gary S. Weaver
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|