rest_framework 0.5.6 → 0.5.7

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
  SHA256:
3
- metadata.gz: a4a0533297190fd34ec007578f495d67ef19e3e02b7b495b8be004dd2180f6b2
4
- data.tar.gz: 07f5478177e47bae6476c42587b10b698a862bbcfa762497a7bd11ebb5cf324e
3
+ metadata.gz: a9e50f091ca2fab796992252f39a700ab1c22ad89b50953654240ac5273cd7ac
4
+ data.tar.gz: ed46ccce3fb8986f207f5b8e996f1a9577e58834f70fcc7de0c36a3e54b8f65f
5
5
  SHA512:
6
- metadata.gz: d971192a3c797b69e9e7982f2f7e381b7fa2784db39e592c56f3640683985424e40a52e28a0ff503f0c22ad2b0cedbd828ba5b4461e354abe36d5d9e8c01c370
7
- data.tar.gz: 22f0d3779328a0c5dccb382674d44dfea4a5cffea92b96614366d9cbfa570ce436ea5774c13a16f524b995c54074d5ad5d979e82d081d2f012f89923f55339f6
6
+ metadata.gz: ed76863c305eb92f712affbb0d43b1470c518f933226a86c60983690edf5846faadc7dca0278d1075ed903d6637b45c4eeff1788e00ef130ec49457f94dba4f4
7
+ data.tar.gz: 16b12ff971d7201669d0114804e8664dc641065c771c4cb34ba1ac21899f1fd14fd396d363f76619a58dba67688b88e35ae69b34be80e4975b6f94cfe6760c67
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.6
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
- respond_to do |format|
149
- if payload == ""
150
- format.json { head(:no_content) } if self.class.serialize_to_json
151
- format.xml { head(:no_content) } if self.class.serialize_to_xml
152
- else
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
- @json_payload = "" if self.class.serialize_to_json
167
- @xml_payload = "" if self.class.serialize_to_xml
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
- @json_payload = payload.to_json if self.class.serialize_to_json
170
- @xml_payload = payload.to_xml if self.class.serialize_to_xml
171
- end
172
- @template_logo_text ||= "Rails REST Framework"
173
- @title ||= self.controller_name.camelize
174
- @route_groups ||= RESTFramework::Utils.get_routes(Rails.application.routes, request)
175
- hkwargs = kwargs.merge(html_kwargs)
176
- begin
177
- render(**hkwargs)
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)&.map(&:to_s) ||
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&.map(&:to_s) || self.get_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&.map(&:to_s) || self.get_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&.map(&:to_s) || self.get_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&.map(&:to_s) || self.get_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)&.map(&:to_s)
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 = self.get_allowed_parameters || self.get_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.to_s) }
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
- find_by_fields = self.get_find_by_fields
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._index)
210
+ api_response(self.index!)
207
211
  end
208
212
 
209
- def _index
210
- @records = self.get_filtered_data(self.get_recordset)
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._show)
231
+ api_response(self.show!)
228
232
  end
229
233
 
230
- def _show
231
- @record = self.get_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._create)
243
+ api_response(self.create!)
240
244
  end
241
245
 
242
- def _create
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 = self.get_recordset.create!(self.get_create_params)
249
+ @record ||= self.get_recordset.create!(self.get_create_params)
246
250
  else
247
251
  # Otherwise, perform a "bare" create.
248
- @record = self.get_model.create!(self.get_create_params)
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._update)
262
+ api_response(self.update!)
259
263
  end
260
264
 
261
- def _update
262
- @record = self.get_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._destroy
275
+ self.destroy!
272
276
  api_response("")
273
277
  end
274
278
 
275
- def _destroy
276
- @record = self.get_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 = @controller.get_filterset_fields
17
- return @controller.request.query_parameters.select { |p, _|
18
- fields.include?(p)
19
- }.to_h.symbolize_keys # convert from HashWithIndifferentAccess to Hash w/keys
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 = @controller.get_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.to_sym] = direction
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 # camelize to leave plural names plural
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.6
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-03-20 00:00:00.000000000 Z
11
+ date: 2022-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails