rest_framework 0.6.0 → 0.6.1

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: 365696e92f267b3c3547bc6fed9ad2cfcbc1a3cc769ee7128332cc469a534eec
4
- data.tar.gz: bde678de3e2c11847f3777b057b5d267f75185c23a997fe704acaab6a36ad23d
3
+ metadata.gz: 33c45fc367d9899fa2e47ea4c042a46ed381986e9210bf4574b61d1301c60fcc
4
+ data.tar.gz: 225adc8d9b5380e7e403765d7ec21331a61c9a8c562013781571dc84037560d0
5
5
  SHA512:
6
- metadata.gz: 527ffee7ac29d26d0405c6ff6dba94c2d5d7e8ba505b2c6f42860fb67d782fc2b180ddaeb462947b918b84818fd30633a8290bde5d2558ced238b5a28bd9eab8
7
- data.tar.gz: 6d56d27874e79beea8ce627fc85b0841e3a2d1cc864a7664a07ef54279216e8da80f13061ed07f3761c1da9ae570906387331c21f946c9bef102adab2bb56b6c
6
+ metadata.gz: 7178418a52918caf797f2a96bd1d39a5e8291d154ba335122b590e5534cc02124f5203feb24dade1f5dcb84ac45b281a7e4fb30274be943025e2f7850bd1cfca
7
+ data.tar.gz: e4195747856948df06aa0c396812d6ea3bbd3d374318b3c9f7a60c55747ae62db25ef76fc59e0705fd1531822304c62a416151772d29877aa4b723852015f2db
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.6.1
@@ -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.
@@ -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
@@ -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.1
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-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails