Module: RESTFramework::BaseModelControllerMixin

Includes:
BaseControllerMixin
Included in:
ModelControllerMixin, ReadOnlyModelControllerMixin
Defined in:
lib/rest_framework/controller_mixins/models.rb

Overview

This module provides the core functionality for controllers based on models.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseControllerMixin

#api_response, #get_filtered_data, #options, #record_invalid, #record_not_destroyed, #record_not_found, #record_not_saved, #root, #serialize, #unknown_attribute_error

Class Method Details

.included(base) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rest_framework/controller_mixins/models.rb', line 121

def self.included(base)
  return unless base.is_a?(Class)

  RESTFramework::BaseControllerMixin.included(base)
  base.extend(ClassMethods)

  # Add class attributes (with defaults) unless they already exist.
  {
    # Core attributes related to models.
    model: nil,
    recordset: nil,

    # Attributes for configuring record fields.
    fields: nil,
    action_fields: nil,
    metadata_fields: nil,

    # Attributes for finding records.
    find_by_fields: nil,
    find_by_query_param: "find_by",

    # Attributes for create/update parameters.
    allowed_parameters: nil,
    allowed_action_parameters: nil,

    # Attributes for the default native serializer.
    native_serializer_config: nil,
    native_serializer_singular_config: nil,
    native_serializer_plural_config: nil,
    native_serializer_only_query_param: "only",
    native_serializer_except_query_param: "except",

    # Attributes for default model filtering, ordering, and searching.
    filterset_fields: nil,
    ordering_fields: nil,
    ordering_query_param: "ordering",
    ordering_no_reorder: false,
    search_fields: nil,
    search_query_param: "search",
    search_ilike: false,

    # Other misc attributes.
    create_from_recordset: true,  # Option for `recordset.create` vs `Model.create` behavior.
    filter_recordset_before_find: true,  # Control if filtering is done before find.
  }.each do |a, default|
    next if base.respond_to?(a)

    base.class_attribute(a)

    # Set default manually so we can still support Rails 4. Maybe later we can use the default
    # parameter on `class_attribute`.
    base.send(:"#{a}=", default)
  end

  # Actions to run at the end of the class definition.
  TracePoint.trace(:end) do |t|
    next if base != t.self

    # Delegate extra actions.
    base.extra_actions&.each do |action, config|
      next unless config.is_a?(Hash) && config[:delegate]

      base.define_method(action) do
        model = self.class.get_model
        next unless model.respond_to?(action)

        if model.method(action).parameters.last&.first == :keyrest
          return api_response(model.send(action, **params))
        else
          return api_response(model.send(action))
        end
      end
    end

    # Delegate extra member actions.
    base.extra_member_actions&.each do |action, config|
      next unless config.is_a?(Hash) && config[:delegate]

      base.define_method(action) do
        record = self.get_record
        next unless record.respond_to?(action)

        if record.method(action).parameters.last&.first == :keyrest
          return api_response(record.send(action, **params))
        else
          return api_response(record.send(action))
        end
      end
    end

    # It's important to disable the trace once we've found the end of the base class definition,
    # for performance.
    t.disable
  end
end

Instance Method Details

#_get_specific_action_config(action_config_key, generic_config_key) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/rest_framework/controller_mixins/models.rb', line 217

def _get_specific_action_config(action_config_key, generic_config_key)
  action_config = self.class.send(action_config_key)&.with_indifferent_access || {}
  action = self.action_name&.to_sym

  # Index action should use :list serializer if :index is not provided.
  action = :list if action == :index && !action_config.key?(:index)

  return (action_config[action] if action) || self.class.send(generic_config_key)
end

#get_allowed_parametersObject

Get a list of parameters allowed for the current action. By default we do not fallback to columns so arbitrary fields can be submitted if no fields are defined.



253
254
255
256
257
258
# File 'lib/rest_framework/controller_mixins/models.rb', line 253

def get_allowed_parameters
  return _get_specific_action_config(
    :allowed_action_parameters,
    :allowed_parameters,
  ) || self.fields
end

#get_body_paramsObject Also known as: get_create_params, get_update_params

Filter the request body for keys in current action’s allowed_parameters/fields config.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/rest_framework/controller_mixins/models.rb', line 273

def get_body_params
  # Filter the request body and map to strings. Return all params if we cannot resolve a list of
  # allowed parameters or fields.
  allowed_params = self.get_allowed_parameters&.map(&:to_s)
  body_params = if allowed_params
    request.request_parameters.select { |p| allowed_params.include?(p) }
  else
    request.request_parameters
  end

  # Add query params in place of missing body params, if configured.
  if self.class.accept_generic_params_as_body_params && allowed_params
    (allowed_params - body_params.keys).each do |k|
      if value = params[k].presence
        body_params[k] = value
      end
    end
  end

  # Filter primary key if configured.
  if self.class.filter_pk_from_request_body
    body_params.delete(self.class.get_model&.primary_key)
  end

  # Filter fields in exclude_body_fields.
  (self.class.exclude_body_fields || []).each { |f| body_params.delete(f) }

  return body_params
end

#get_fields(fallback: false) ⇒ Object

Get a list of fields for the current action. Returning ‘nil` indicates that anything should be accepted unless `fallback` is true, in which case we should fallback to this controller’s model columns, or en empty array.



230
231
232
233
234
235
236
237
238
# File 'lib/rest_framework/controller_mixins/models.rb', line 230

def get_fields(fallback: false)
  fields = _get_specific_action_config(:action_fields, :fields)

  if fallback
    fields ||= self.class.get_model&.column_names || []
  end

  return fields
end

#get_filter_backendsObject

Helper to get filtering backends, defaulting to using ‘ModelFilter` and `ModelOrderingFilter`.



266
267
268
269
270
# File 'lib/rest_framework/controller_mixins/models.rb', line 266

def get_filter_backends
  return self.class.filter_backends || [
    RESTFramework::ModelFilter, RESTFramework::ModelOrderingFilter
  ]
end

#get_find_by_fieldsObject

Get a list of find_by fields for the current action. Do not fallback to columns in case the user wants to find by virtual columns.



247
248
249
# File 'lib/rest_framework/controller_mixins/models.rb', line 247

def get_find_by_fields
  return self.class.find_by_fields || self.get_fields
end

#get_options_metadataObject

Pass fields to get dynamic metadata based on which fields are available.



241
242
243
# File 'lib/rest_framework/controller_mixins/models.rb', line 241

def 
  return self.class.(fields: self.get_fields(fallback: true))
end

#get_recordObject

Get a single record by primary key or another column, if allowed. The return value is cached and exposed to the view as the ‘@record` instance variable.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/rest_framework/controller_mixins/models.rb', line 328

def get_record
  # Cache the result.
  return @record if instance_variable_defined?(:@record)

  recordset = self.get_recordset
  find_by_key = self.class.get_model.primary_key

  # Find by another column if it's permitted.
  if find_by_param = self.class.find_by_query_param.presence
    if find_by = params[find_by_param].presence
      find_by_fields = self.get_find_by_fields&.map(&:to_s)

      if !find_by_fields || find_by.in?(find_by_fields)
        find_by_key = find_by
      end
    end
  end

  # Filter recordset, if configured.
  if self.filter_recordset_before_find
    recordset = self.get_records
  end

  # Return the record. Route key is always `:id` by Rails convention.
  return @record = recordset.find_by!(find_by_key => params[:id])
end

#get_recordsObject

Helper to get the records this controller has access to after any filtering is applied.



320
321
322
323
324
# File 'lib/rest_framework/controller_mixins/models.rb', line 320

def get_records
  return @records if instance_variable_defined?(:@records)

  return @records = self.get_filtered_data(self.get_recordset)
end

#get_recordsetObject

Get the set of records this controller has access to. The return value is cached and exposed to the view as the ‘@recordset` instance variable.



307
308
309
310
311
312
313
314
315
316
317
# File 'lib/rest_framework/controller_mixins/models.rb', line 307

def get_recordset
  return @recordset if instance_variable_defined?(:@recordset)
  return (@recordset = self.class.recordset) if self.class.recordset

  # If there is a model, return that model's default scope (all records by default).
  if (model = self.class.get_model(from_get_recordset: true))
    return @recordset = model.all
  end

  return @recordset = nil
end

#get_serializer_classObject

Helper to get the configured serializer class, or ‘NativeSerializer` as a default.



261
262
263
# File 'lib/rest_framework/controller_mixins/models.rb', line 261

def get_serializer_class
  return super || RESTFramework::NativeSerializer
end