Module: RESTFramework::Mixins::BulkCreateModelMixin

Included in:
BulkModelControllerMixin
Defined in:
lib/rest_framework/mixins/bulk_model_controller_mixin.rb

Overview

Mixin for creating records in bulk. This is unique compared to update/destroy because we overload the existing ‘create` action to support bulk creation.

Instance Method Summary collapse

Instance Method Details

#createObject



13
14
15
16
17
18
19
20
21
# File 'lib/rest_framework/mixins/bulk_model_controller_mixin.rb', line 13

def create
  if params[:_json].is_a?(Array)
    records = self.create_all!
    serialized_records = self.bulk_serialize(records)
    return api_response(serialized_records)
  end

  return super
end

#create_all!Object

Perform the ‘create` call, and return the collection of (possibly) created records.



24
25
26
27
28
29
30
31
# File 'lib/rest_framework/mixins/bulk_model_controller_mixin.rb', line 24

def create_all!
  create_data = self.get_create_params(bulk_mode: true)[:_json]

  # Perform bulk create in a transaction.
  return ActiveRecord::Base.transaction do
    next self.get_create_from.create(create_data)
  end
end

#get_options_metadataObject

While bulk update/destroy are obvious because they create new router endpoints, bulk create overloads the existing collection ‘POST` endpoint, so we add a special key to the options metadata to indicate bulk create is supported.



9
10
11
# File 'lib/rest_framework/mixins/bulk_model_controller_mixin.rb', line 9

def 
  return super.merge({bulk_create: true})
end