rest_framework 0.0.8 → 0.0.9
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce317c05e27ab6421b18f37595dbb846a747c8616d19fc9a1927a9ecc30fefc7
|
4
|
+
data.tar.gz: b8f88631bfca835f395677af0e0c0079b40c079aa8d9a6cc8c8ee46dc5d589c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abd9fc1e83d61c60af77d834d3a3e03a4a904863f3ad4bb851d8cf35a5a563a322eef508a273764ce727303e3e25f526cca03a643127957a7d9952e4988de743
|
7
|
+
data.tar.gz: 8aadf4ba5467c645da2404df61880bd741a491eea0c14a09b20e7056726022f7fc7bd4b8a3f90e9ac068b578e63870c85cfaa813296245cf8c4fddfb7d002a66
|
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
@@ -36,6 +36,8 @@ module RESTFramework
|
|
36
36
|
])
|
37
37
|
base.rescue_from(ActiveRecord::RecordNotFound, with: :record_not_found)
|
38
38
|
base.rescue_from(ActiveRecord::RecordInvalid, with: :record_invalid)
|
39
|
+
base.rescue_from(ActiveRecord::RecordNotSaved, with: :record_not_saved)
|
40
|
+
base.rescue_from(ActiveRecord::RecordNotDestroyed, with: :record_not_destroyed)
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
@@ -51,6 +53,14 @@ module RESTFramework
|
|
51
53
|
return api_response({message: "Record not found.", exception: e}, status: 404)
|
52
54
|
end
|
53
55
|
|
56
|
+
def record_not_saved(e)
|
57
|
+
return api_response({message: "Record not saved.", exception: e}, status: 406)
|
58
|
+
end
|
59
|
+
|
60
|
+
def record_not_destroyed(e)
|
61
|
+
return api_response({message: "Record not destroyed.", exception: e}, status: 406)
|
62
|
+
end
|
63
|
+
|
54
64
|
def _get_routes
|
55
65
|
begin
|
56
66
|
formatter = ActionDispatch::Routing::ConsoleFormatter::Sheet
|
@@ -71,6 +81,11 @@ module RESTFramework
|
|
71
81
|
json_kwargs ||= {}
|
72
82
|
xml_kwargs ||= {}
|
73
83
|
|
84
|
+
# make empty responses status 204 unless a status is already explicitly defined
|
85
|
+
if (payload.nil? || payload == '') && !kwargs.key?(:status)
|
86
|
+
kwargs[:status] = 204
|
87
|
+
end
|
88
|
+
|
74
89
|
respond_to do |format|
|
75
90
|
if payload.respond_to?(:to_json)
|
76
91
|
format.json {
|
@@ -94,7 +109,8 @@ module RESTFramework
|
|
94
109
|
kwargs = kwargs.merge(html_kwargs)
|
95
110
|
begin
|
96
111
|
render(**kwargs)
|
97
|
-
rescue ActionView::MissingTemplate # fallback to rest_framework
|
112
|
+
rescue ActionView::MissingTemplate # fallback to rest_framework layout/view
|
113
|
+
kwargs[:layout] = "rest_framework"
|
98
114
|
kwargs[:template] = "rest_framework/default"
|
99
115
|
end
|
100
116
|
render(**kwargs)
|
@@ -13,6 +13,8 @@ module RESTFramework
|
|
13
13
|
:recordset,
|
14
14
|
:fields,
|
15
15
|
:action_fields,
|
16
|
+
:native_serializer_config,
|
17
|
+
:native_serializer_action_config,
|
16
18
|
:filterset_fields,
|
17
19
|
:allowed_parameters,
|
18
20
|
:allowed_action_parameters,
|
@@ -42,6 +44,19 @@ module RESTFramework
|
|
42
44
|
return action_fields[action] || self.class.fields || []
|
43
45
|
end
|
44
46
|
|
47
|
+
# Get a native serializer config for an action (or the current action if none given).
|
48
|
+
def get_native_serializer_config(action: nil)
|
49
|
+
native_serializer_action_config = self.class.native_serializer_action_config || {}
|
50
|
+
|
51
|
+
# action will, by default, be the current action name
|
52
|
+
action = action_name.to_sym unless action
|
53
|
+
|
54
|
+
# index action should use :list serializer config if :index is not provided
|
55
|
+
action = :list if action == :index && !native_serializer_action_config.key?(:index)
|
56
|
+
|
57
|
+
return native_serializer_action_config[action] || self.class.native_serializer_config
|
58
|
+
end
|
59
|
+
|
45
60
|
# Get a list of parameters allowed for an action (or the current action if none given).
|
46
61
|
def get_allowed_parameters(action: nil)
|
47
62
|
allowed_action_parameters = self.class.allowed_action_parameters || {}
|
@@ -178,12 +193,8 @@ module RESTFramework
|
|
178
193
|
module DestroyModelMixin
|
179
194
|
def destroy
|
180
195
|
@record = self.get_record
|
181
|
-
|
182
|
-
|
183
|
-
api_response('')
|
184
|
-
else
|
185
|
-
api_response({detail: "Method 'DELETE' not allowed."}, status: 405)
|
186
|
-
end
|
196
|
+
@record.destroy!
|
197
|
+
api_response(nil)
|
187
198
|
end
|
188
199
|
end
|
189
200
|
|
@@ -24,17 +24,26 @@ module RESTFramework
|
|
24
24
|
|
25
25
|
# Get a configuration passable to `as_json` for the model.
|
26
26
|
def get_native_serializer_config
|
27
|
+
# return a serializer config if one is defined
|
28
|
+
serializer_config = @controller.send(:get_native_serializer_config)
|
29
|
+
return serializer_config if serializer_config
|
30
|
+
|
31
|
+
# build serializer config from fields
|
27
32
|
fields = @controller.send(:get_fields)
|
28
33
|
unless fields.blank?
|
29
34
|
columns, methods = fields.partition { |f| f.to_s.in?(@model.column_names) }
|
30
35
|
return {only: columns, methods: methods}
|
31
36
|
end
|
37
|
+
|
32
38
|
return {}
|
33
39
|
end
|
34
40
|
|
35
41
|
# Convert the object(s) to Ruby primitives.
|
36
42
|
def serialize
|
37
|
-
|
43
|
+
if @object
|
44
|
+
return @object.as_json(self.get_native_serializer_config)
|
45
|
+
end
|
46
|
+
return nil
|
38
47
|
end
|
39
48
|
end
|
40
49
|
end
|