rest_framework 0.6.0 → 0.6.2

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: 365696e92f267b3c3547bc6fed9ad2cfcbc1a3cc769ee7128332cc469a534eec
4
- data.tar.gz: bde678de3e2c11847f3777b057b5d267f75185c23a997fe704acaab6a36ad23d
3
+ metadata.gz: 207b074408e0f95dd2d289abdd7d048f66f44d38f795b45b51245af28376a1c4
4
+ data.tar.gz: 67729758c21dd7627419248801d85f929188c64f2b20dba9095938a9650259da
5
5
  SHA512:
6
- metadata.gz: 527ffee7ac29d26d0405c6ff6dba94c2d5d7e8ba505b2c6f42860fb67d782fc2b180ddaeb462947b918b84818fd30633a8290bde5d2558ced238b5a28bd9eab8
7
- data.tar.gz: 6d56d27874e79beea8ce627fc85b0841e3a2d1cc864a7664a07ef54279216e8da80f13061ed07f3761c1da9ae570906387331c21f946c9bef102adab2bb56b6c
6
+ metadata.gz: 8dd2c8c6209b04ffa77fe1506906332c9beb3e87ee60bad2f348ab5331258b46fe66fc3ced5048b66472c424b84132746fbca650b82faa87415c1a36b41c16c2
7
+ data.tar.gz: d04ceada0800c54161cfaeccb3d43251c3ae3bd5c629a937da058c0be640c0287453ac3338744d36e0290aea536b4987f56e76b0ace999580bd90adbd14d85c1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.6.2
@@ -68,16 +68,17 @@ module RESTFramework::BaseModelControllerMixin
68
68
  return (action_config[action] if action) || self.class.send(generic_config_key)
69
69
  end
70
70
 
71
- # Get a list of fields for the current action.
72
- def get_fields(fallback: true)
73
- action_fields = _get_specific_action_config(:action_fields, :fields)
71
+ # Get a list of fields for the current action. Returning `nil` indicates that anything should be
72
+ # accepted unless `fallback` is true, in which case we should fallback to this controller's model
73
+ # columns, or en empty array.
74
+ def get_fields(fallback: false)
75
+ fields = _get_specific_action_config(:action_fields, :fields)
74
76
 
75
- # Typically we want to fallback to either the DB dolumns or an empty array.
76
77
  if fallback
77
- action_fields ||= self.get_model&.column_names || []
78
+ fields ||= self.get_model&.column_names || []
78
79
  end
79
80
 
80
- return action_fields
81
+ return fields
81
82
  end
82
83
 
83
84
  # Get a list of find_by fields for the current action.
@@ -85,9 +86,9 @@ module RESTFramework::BaseModelControllerMixin
85
86
  return self.class.find_by_fields || self.get_fields
86
87
  end
87
88
 
88
- # Get a list of find_by fields for the current action.
89
+ # Get a list of find_by fields for the current action. Default to the model column names.
89
90
  def get_filterset_fields
90
- return self.class.filterset_fields || self.get_fields
91
+ return self.class.filterset_fields || self.get_fields(fallback: true)
91
92
  end
92
93
 
93
94
  # Get a list of ordering fields for the current action.
@@ -95,14 +96,17 @@ module RESTFramework::BaseModelControllerMixin
95
96
  return self.class.ordering_fields || self.get_fields
96
97
  end
97
98
 
98
- # Get a list of search fields for the current action.
99
+ # Get a list of search fields for the current action. Default to the model column names.
99
100
  def get_search_fields
100
- return self.class.search_fields || self.get_fields
101
+ return self.class.search_fields || self.get_fields(fallback: true)
101
102
  end
102
103
 
103
104
  # Get a list of parameters allowed for the current action.
104
105
  def get_allowed_parameters
105
- return _get_specific_action_config(:allowed_action_parameters, :allowed_parameters)
106
+ return _get_specific_action_config(
107
+ :allowed_action_parameters,
108
+ :allowed_parameters,
109
+ ) || self.fields
106
110
  end
107
111
 
108
112
  # Helper to get the configured serializer class, or `NativeSerializer` as a default.
@@ -120,15 +124,18 @@ module RESTFramework::BaseModelControllerMixin
120
124
  # Filter the request body for keys in current action's allowed_parameters/fields config.
121
125
  def get_body_params
122
126
  return @_get_body_params ||= begin
123
- # Map fields to strings because body keys will be string.
124
- fields = (self.get_allowed_parameters || self.get_fields).map(&:to_s)
125
-
126
- # Filter the request body.
127
- body_params = request.request_parameters.select { |p| fields.include?(p) }
127
+ # Filter the request body and map to strings. Return all params if we cannot resolve a list of
128
+ # allowed parameters or fields.
129
+ body_params = if allowed_params = self.get_allowed_parameters&.map(&:to_s)
130
+ request.request_parameters.select { |p| allowed_params.include?(p) }
131
+ else
132
+ request.request_parameters
133
+ end
128
134
 
129
- # Add query params in place of missing body params, if configured.
135
+ # Add query params in place of missing body params, if configured. If fields are not defined,
136
+ # fallback to using columns for this particular feature.
130
137
  if self.class.accept_generic_params_as_body_params
131
- (fields - body_params.keys).each do |k|
138
+ (self.get_fields(fallback: true) - body_params.keys).each do |k|
132
139
  if (value = params[k])
133
140
  body_params[k] = value
134
141
  end
@@ -189,14 +196,17 @@ module RESTFramework::BaseModelControllerMixin
189
196
  return @_get_record if @_get_record
190
197
 
191
198
  recordset = self.get_recordset
192
-
193
- # Map find_by fields to strings because query param value is a string.
194
- find_by_fields = self.get_find_by_fields.map(&:to_s)
195
199
  find_by_key = self.get_model.primary_key
196
200
 
197
201
  # Find by another column if it's permitted.
198
- if find_by_query_param && params[find_by_query_param].in?(find_by_fields)
199
- find_by_key = params[find_by_query_param]
202
+ if find_by_param = self.class.find_by_query_param.presence
203
+ if find_by = params[find_by_param].presence
204
+ find_by_fields = self.get_find_by_fields&.map(&:to_s)
205
+
206
+ if !find_by_fields || find_by.in?(find_by_fields)
207
+ find_by_key = find_by
208
+ end
209
+ end
200
210
  end
201
211
 
202
212
  # Filter recordset, if configured.
@@ -250,8 +260,10 @@ module RESTFramework::CreateModelMixin
250
260
 
251
261
  def create!
252
262
  if self.get_recordset.respond_to?(:create!) && self.create_from_recordset
253
- # Create with any properties inherited from the recordset.
254
- @record ||= self.get_recordset.create!(self.get_create_params)
263
+ # Create with any properties inherited from the recordset. We exclude any `select` clauses in
264
+ # case model callbacks need to call `count` on this collection, which typically raises a SQL
265
+ # `SyntaxError`.
266
+ @record ||= self.get_recordset.except(:select).create!(self.get_create_params)
255
267
  else
256
268
  # Otherwise, perform a "bare" create.
257
269
  @record ||= self.get_model.create!(self.get_create_params)
@@ -14,9 +14,11 @@ 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
16
  # Map filterset fields to strings because query parameter keys are strings.
17
- fields = @controller.get_filterset_fields.map(&:to_s)
17
+ if fields = @controller.get_filterset_fields&.map(&:to_s)
18
+ return @controller.request.query_parameters.select { |p, _| fields.include?(p) }
19
+ end
18
20
 
19
- return @controller.request.query_parameters.select { |p, _| fields.include?(p) }
21
+ return @controller.request.query_parameters.to_h
20
22
  end
21
23
 
22
24
  # Filter data according to the request query parameters.
@@ -37,7 +39,7 @@ class RESTFramework::ModelOrderingFilter < RESTFramework::BaseFilter
37
39
  return nil if @controller.class.ordering_query_param.blank?
38
40
 
39
41
  # Ensure ordering_fields are strings since the split param will be strings.
40
- ordering_fields = @controller.get_ordering_fields.map(&:to_s)
42
+ ordering_fields = @controller.get_ordering_fields&.map(&:to_s)
41
43
  order_string = @controller.params[@controller.class.ordering_query_param]
42
44
 
43
45
  unless order_string.blank?
@@ -50,7 +52,7 @@ class RESTFramework::ModelOrderingFilter < RESTFramework::BaseFilter
50
52
  column = field
51
53
  direction = :asc
52
54
  end
53
- if column.in?(ordering_fields)
55
+ if !ordering_fields || column.in?(ordering_fields)
54
56
  ordering[column] = direction
55
57
  end
56
58
  end
@@ -21,7 +21,8 @@ end
21
21
  class RESTFramework::PageNumberPaginator < RESTFramework::BasePaginator
22
22
  def initialize(**kwargs)
23
23
  super
24
- @count = @data.count
24
+ # Exclude any `select` clauses since that would cause `count` to fail with a SQL `SyntaxError`.
25
+ @count = @data.except(:select).count
25
26
  @page_size = self._page_size
26
27
 
27
28
  @total_pages = @count / @page_size
@@ -213,8 +213,7 @@ class RESTFramework::NativeSerializer < RESTFramework::BaseSerializer
213
213
  end
214
214
 
215
215
  # If the config wasn't determined, build a serializer config from controller fields.
216
- fields = @controller.get_fields(fallback: false) if @controller
217
- if fields
216
+ if fields = @controller&.get_fields
218
217
  fields = fields.deep_dup
219
218
 
220
219
  if @model
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.6.0
4
+ version: 0.6.2
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-08-30 00:00:00.000000000 Z
11
+ date: 2022-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails