rest_framework 0.5.6 → 0.5.7
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
- data/VERSION +1 -1
- data/lib/rest_framework/controller_mixins/base.rb +47 -33
- data/lib/rest_framework/controller_mixins/models.rb +33 -29
- data/lib/rest_framework/filters.rb +9 -8
- data/lib/rest_framework/routers.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9e50f091ca2fab796992252f39a700ab1c22ad89b50953654240ac5273cd7ac
|
4
|
+
data.tar.gz: ed46ccce3fb8986f207f5b8e996f1a9577e58834f70fcc7de0c36a3e54b8f65f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed76863c305eb92f712affbb0d43b1470c518f933226a86c60983690edf5846faadc7dca0278d1075ed903d6637b45c4eeff1788e00ef130ec49457f94dba4f4
|
7
|
+
data.tar.gz: 16b12ff971d7201669d0114804e8664dc641065c771c4cb34ba1ac21899f1fd14fd396d363f76619a58dba67688b88e35ae69b34be80e4975b6f94cfe6760c67
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.7
|
@@ -45,6 +45,7 @@ module RESTFramework::BaseControllerMixin
|
|
45
45
|
page_query_param: "page",
|
46
46
|
page_size_query_param: "page_size",
|
47
47
|
max_page_size: nil,
|
48
|
+
rescue_unknown_format_with: :json,
|
48
49
|
serializer_class: nil,
|
49
50
|
serialize_to_json: true,
|
50
51
|
serialize_to_xml: true,
|
@@ -145,42 +146,55 @@ module RESTFramework::BaseControllerMixin
|
|
145
146
|
raise RESTFramework::NilPassedToAPIResponseError
|
146
147
|
end
|
147
148
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
format.json {
|
154
|
-
jkwargs = kwargs.merge(json_kwargs)
|
155
|
-
render(json: payload, layout: false, **jkwargs)
|
156
|
-
} if self.class.serialize_to_json
|
157
|
-
format.xml {
|
158
|
-
xkwargs = kwargs.merge(xml_kwargs)
|
159
|
-
render(xml: payload, layout: false, **xkwargs)
|
160
|
-
} if self.class.serialize_to_xml
|
161
|
-
# TODO: possibly support more formats here if supported?
|
162
|
-
end
|
163
|
-
format.html {
|
164
|
-
@payload = payload
|
149
|
+
# Flag to track if we had to rescue unknown format.
|
150
|
+
already_rescued_unknown_format = false
|
151
|
+
|
152
|
+
begin
|
153
|
+
respond_to do |format|
|
165
154
|
if payload == ""
|
166
|
-
|
167
|
-
|
155
|
+
format.json { head(:no_content) } if self.class.serialize_to_json
|
156
|
+
format.xml { head(:no_content) } if self.class.serialize_to_xml
|
168
157
|
else
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
rescue ActionView::MissingTemplate # fallback to rest_framework layout
|
179
|
-
hkwargs[:layout] = "rest_framework"
|
180
|
-
hkwargs[:html] = ""
|
181
|
-
render(**hkwargs)
|
158
|
+
format.json {
|
159
|
+
jkwargs = kwargs.merge(json_kwargs)
|
160
|
+
render(json: payload, layout: false, **jkwargs)
|
161
|
+
} if self.class.serialize_to_json
|
162
|
+
format.xml {
|
163
|
+
xkwargs = kwargs.merge(xml_kwargs)
|
164
|
+
render(xml: payload, layout: false, **xkwargs)
|
165
|
+
} if self.class.serialize_to_xml
|
166
|
+
# TODO: possibly support more formats here if supported?
|
182
167
|
end
|
183
|
-
|
168
|
+
format.html {
|
169
|
+
@payload = payload
|
170
|
+
if payload == ""
|
171
|
+
@json_payload = "" if self.class.serialize_to_json
|
172
|
+
@xml_payload = "" if self.class.serialize_to_xml
|
173
|
+
else
|
174
|
+
@json_payload = payload.to_json if self.class.serialize_to_json
|
175
|
+
@xml_payload = payload.to_xml if self.class.serialize_to_xml
|
176
|
+
end
|
177
|
+
@template_logo_text ||= "Rails REST Framework"
|
178
|
+
@title ||= self.controller_name.camelize
|
179
|
+
@route_groups ||= RESTFramework::Utils.get_routes(Rails.application.routes, request)
|
180
|
+
hkwargs = kwargs.merge(html_kwargs)
|
181
|
+
begin
|
182
|
+
render(**hkwargs)
|
183
|
+
rescue ActionView::MissingTemplate # fallback to rest_framework layout
|
184
|
+
hkwargs[:layout] = "rest_framework"
|
185
|
+
hkwargs[:html] = ""
|
186
|
+
render(**hkwargs)
|
187
|
+
end
|
188
|
+
}
|
189
|
+
end
|
190
|
+
rescue ActionController::UnknownFormat
|
191
|
+
if !already_rescued_unknown_format && rescue_format = self.class.rescue_unknown_format_with
|
192
|
+
request.format = rescue_format
|
193
|
+
already_rescued_unknown_format = true
|
194
|
+
retry
|
195
|
+
else
|
196
|
+
raise
|
197
|
+
end
|
184
198
|
end
|
185
199
|
end
|
186
200
|
end
|
@@ -59,7 +59,7 @@ module RESTFramework::BaseModelControllerMixin
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def _get_specific_action_config(action_config_key, generic_config_key)
|
62
|
-
action_config = self.class.send(action_config_key) || {}
|
62
|
+
action_config = self.class.send(action_config_key)&.with_indifferent_access || {}
|
63
63
|
action = self.action_name&.to_sym
|
64
64
|
|
65
65
|
# Index action should use :list serializer if :index is not provided.
|
@@ -71,35 +71,33 @@ module RESTFramework::BaseModelControllerMixin
|
|
71
71
|
# Get a list of fields for the current action.
|
72
72
|
def get_fields
|
73
73
|
return (
|
74
|
-
_get_specific_action_config(:action_fields, :fields)&.
|
75
|
-
self.get_model&.column_names ||
|
76
|
-
[]
|
74
|
+
_get_specific_action_config(:action_fields, :fields) || self.get_model&.column_names || []
|
77
75
|
)
|
78
76
|
end
|
79
77
|
|
80
78
|
# Get a list of find_by fields for the current action.
|
81
79
|
def get_find_by_fields
|
82
|
-
return self.class.find_by_fields
|
80
|
+
return self.class.find_by_fields || self.get_fields
|
83
81
|
end
|
84
82
|
|
85
83
|
# Get a list of find_by fields for the current action.
|
86
84
|
def get_filterset_fields
|
87
|
-
return self.class.filterset_fields
|
85
|
+
return self.class.filterset_fields || self.get_fields
|
88
86
|
end
|
89
87
|
|
90
88
|
# Get a list of ordering fields for the current action.
|
91
89
|
def get_ordering_fields
|
92
|
-
return self.class.ordering_fields
|
90
|
+
return self.class.ordering_fields || self.get_fields
|
93
91
|
end
|
94
92
|
|
95
93
|
# Get a list of search fields for the current action.
|
96
94
|
def get_search_fields
|
97
|
-
return self.class.search_fields
|
95
|
+
return self.class.search_fields || self.get_fields
|
98
96
|
end
|
99
97
|
|
100
98
|
# Get a list of parameters allowed for the current action.
|
101
99
|
def get_allowed_parameters
|
102
|
-
return _get_specific_action_config(:allowed_action_parameters, :allowed_parameters)
|
100
|
+
return _get_specific_action_config(:allowed_action_parameters, :allowed_parameters)
|
103
101
|
end
|
104
102
|
|
105
103
|
# Helper to get the configured serializer class, or `NativeSerializer` as a default.
|
@@ -117,7 +115,8 @@ module RESTFramework::BaseModelControllerMixin
|
|
117
115
|
# Filter the request body for keys in current action's allowed_parameters/fields config.
|
118
116
|
def get_body_params
|
119
117
|
return @_get_body_params ||= begin
|
120
|
-
fields
|
118
|
+
# Map fields to strings because body keys will be string.
|
119
|
+
fields = (self.get_allowed_parameters || self.get_fields).map(&:to_s)
|
121
120
|
|
122
121
|
# Filter the request body.
|
123
122
|
body_params = request.request_parameters.select { |p| fields.include?(p) }
|
@@ -137,7 +136,7 @@ module RESTFramework::BaseModelControllerMixin
|
|
137
136
|
end
|
138
137
|
|
139
138
|
# Filter fields in exclude_body_fields.
|
140
|
-
(self.class.exclude_body_fields || []).each { |f| body_params.delete(f
|
139
|
+
(self.class.exclude_body_fields || []).each { |f| body_params.delete(f) }
|
141
140
|
|
142
141
|
body_params
|
143
142
|
end
|
@@ -181,8 +180,13 @@ module RESTFramework::BaseModelControllerMixin
|
|
181
180
|
|
182
181
|
# Get a single record by primary key or another column, if allowed.
|
183
182
|
def get_record
|
183
|
+
# Cache the result.
|
184
|
+
return @_get_record if @_get_record
|
185
|
+
|
184
186
|
recordset = self.get_recordset
|
185
|
-
|
187
|
+
|
188
|
+
# Map find_by fields to strings because query param value is a string.
|
189
|
+
find_by_fields = self.get_find_by_fields.map(&:to_s)
|
186
190
|
find_by_key = self.get_model.primary_key
|
187
191
|
|
188
192
|
# Find by another column if it's permitted.
|
@@ -196,18 +200,18 @@ module RESTFramework::BaseModelControllerMixin
|
|
196
200
|
end
|
197
201
|
|
198
202
|
# Return the record. Route key is always :id by Rails convention.
|
199
|
-
return recordset.find_by!(find_by_key => params[:id])
|
203
|
+
return @_get_record = recordset.find_by!(find_by_key => params[:id])
|
200
204
|
end
|
201
205
|
end
|
202
206
|
|
203
207
|
# Mixin for listing records.
|
204
208
|
module RESTFramework::ListModelMixin
|
205
209
|
def index
|
206
|
-
api_response(self.
|
210
|
+
api_response(self.index!)
|
207
211
|
end
|
208
212
|
|
209
|
-
def
|
210
|
-
@records
|
213
|
+
def index!
|
214
|
+
@records ||= self.get_filtered_data(self.get_recordset)
|
211
215
|
|
212
216
|
# Handle pagination, if enabled.
|
213
217
|
if self.class.paginator_class
|
@@ -224,11 +228,11 @@ end
|
|
224
228
|
# Mixin for showing records.
|
225
229
|
module RESTFramework::ShowModelMixin
|
226
230
|
def show
|
227
|
-
api_response(self.
|
231
|
+
api_response(self.show!)
|
228
232
|
end
|
229
233
|
|
230
|
-
def
|
231
|
-
@record
|
234
|
+
def show!
|
235
|
+
@record ||= self.get_record
|
232
236
|
return self.get_serializer_class.new(@record, controller: self).serialize
|
233
237
|
end
|
234
238
|
end
|
@@ -236,16 +240,16 @@ end
|
|
236
240
|
# Mixin for creating records.
|
237
241
|
module RESTFramework::CreateModelMixin
|
238
242
|
def create
|
239
|
-
api_response(self.
|
243
|
+
api_response(self.create!)
|
240
244
|
end
|
241
245
|
|
242
|
-
def
|
246
|
+
def create!
|
243
247
|
if self.get_recordset.respond_to?(:create!) && self.create_from_recordset
|
244
248
|
# Create with any properties inherited from the recordset.
|
245
|
-
@record
|
249
|
+
@record ||= self.get_recordset.create!(self.get_create_params)
|
246
250
|
else
|
247
251
|
# Otherwise, perform a "bare" create.
|
248
|
-
@record
|
252
|
+
@record ||= self.get_model.create!(self.get_create_params)
|
249
253
|
end
|
250
254
|
|
251
255
|
return self.get_serializer_class.new(@record, controller: self).serialize
|
@@ -255,11 +259,11 @@ end
|
|
255
259
|
# Mixin for updating records.
|
256
260
|
module RESTFramework::UpdateModelMixin
|
257
261
|
def update
|
258
|
-
api_response(self.
|
262
|
+
api_response(self.update!)
|
259
263
|
end
|
260
264
|
|
261
|
-
def
|
262
|
-
@record
|
265
|
+
def update!
|
266
|
+
@record ||= self.get_record
|
263
267
|
@record.update!(self.get_update_params)
|
264
268
|
return self.get_serializer_class.new(@record, controller: self).serialize
|
265
269
|
end
|
@@ -268,12 +272,12 @@ end
|
|
268
272
|
# Mixin for destroying records.
|
269
273
|
module RESTFramework::DestroyModelMixin
|
270
274
|
def destroy
|
271
|
-
self.
|
275
|
+
self.destroy!
|
272
276
|
api_response("")
|
273
277
|
end
|
274
278
|
|
275
|
-
def
|
276
|
-
@record
|
279
|
+
def destroy!
|
280
|
+
@record ||= self.get_record
|
277
281
|
@record.destroy!
|
278
282
|
end
|
279
283
|
end
|
@@ -13,15 +13,15 @@ end
|
|
13
13
|
class RESTFramework::ModelFilter < RESTFramework::BaseFilter
|
14
14
|
# Filter params for keys allowed by the current action's filterset_fields/fields config.
|
15
15
|
def _get_filter_params
|
16
|
-
fields
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
# Map filterset fields to strings because query parameter keys are strings.
|
17
|
+
fields = @controller.get_filterset_fields.map(&:to_s)
|
18
|
+
|
19
|
+
return @controller.request.query_parameters.select { |p, _| fields.include?(p) }
|
20
20
|
end
|
21
21
|
|
22
22
|
# Filter data according to the request query parameters.
|
23
23
|
def get_filtered_data(data)
|
24
|
-
filter_params = self._get_filter_params
|
24
|
+
filter_params = self._get_filter_params.symbolize_keys
|
25
25
|
unless filter_params.blank?
|
26
26
|
return data.where(**filter_params)
|
27
27
|
end
|
@@ -36,11 +36,12 @@ class RESTFramework::ModelOrderingFilter < RESTFramework::BaseFilter
|
|
36
36
|
def _get_ordering
|
37
37
|
return nil if @controller.class.ordering_query_param.blank?
|
38
38
|
|
39
|
-
ordering_fields
|
39
|
+
# Ensure ordering_fields are strings since the split param will be strings.
|
40
|
+
ordering_fields = @controller.get_ordering_fields.map(&:to_s)
|
40
41
|
order_string = @controller.params[@controller.class.ordering_query_param]
|
41
42
|
|
42
43
|
unless order_string.blank?
|
43
|
-
ordering = {}
|
44
|
+
ordering = {}.with_indifferent_access
|
44
45
|
order_string.split(",").each do |field|
|
45
46
|
if field[0] == "-"
|
46
47
|
column = field[1..-1]
|
@@ -50,7 +51,7 @@ class RESTFramework::ModelOrderingFilter < RESTFramework::BaseFilter
|
|
50
51
|
direction = :asc
|
51
52
|
end
|
52
53
|
if column.in?(ordering_fields)
|
53
|
-
ordering[column
|
54
|
+
ordering[column] = direction
|
54
55
|
end
|
55
56
|
end
|
56
57
|
return ordering
|
@@ -6,7 +6,7 @@ module ActionDispatch::Routing
|
|
6
6
|
# Internal interface to get the controller class from the name and current scope.
|
7
7
|
def _get_controller_class(name, pluralize: true, fallback_reverse_pluralization: true)
|
8
8
|
# Get class name.
|
9
|
-
name = name.to_s.camelize #
|
9
|
+
name = name.to_s.camelize # Camelize to leave plural names plural.
|
10
10
|
name = name.pluralize if pluralize
|
11
11
|
if name == name.pluralize
|
12
12
|
name_reverse = name.singularize
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_framework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregory N. Schmit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|