Module: RESTFramework::BulkCreateModelMixin
- Included in:
- BulkModelControllerMixin
- Defined in:
- lib/rest_framework/controller_mixins/bulk.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. :nocov:
Instance Method Summary collapse
- #create ⇒ Object
-
#create_all! ⇒ Object
Perform the ‘create` or `insert_all` call and return the created records with any errors.
Instance Method Details
#create ⇒ Object
7 8 9 10 |
# File 'lib/rest_framework/controller_mixins/bulk.rb', line 7 def create status, payload = self.create_all! return api_response(payload, status: status) end |
#create_all! ⇒ Object
Perform the ‘create` or `insert_all` call and return the created records with any errors. The result should be of the form: `(status, payload)`, and `payload` should be of the form: `[record: | errors:]`, unless batch mode is enabled, in which case `payload` is blank with a status of `202`.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rest_framework/controller_mixins/bulk.rb', line 16 def create_all! if self.class.bulk_batch_mode insert_from = if self.get_recordset.respond_to?(:insert_all) && self.create_from_recordset # Create with any properties inherited from the recordset. We exclude any `select` clauses # in case model callbacks need to call `count` on this collection, which typically raises a # SQL `SyntaxError`. self.get_recordset.except(:select) else # Otherwise, perform a "bare" insert_all. self.class.get_model end insert_from end # Perform bulk creation, possibly in a transaction. self.class._rrf_bulk_transaction do if self.get_recordset.respond_to?(:insert_all) && self.create_from_recordset # Create with any properties inherited from the recordset. We exclude any `select` clauses # in case model callbacks need to call `count` on this collection, which typically raises a # SQL `SyntaxError`. return self.get_recordset.except(:select).create!(self.get_create_params) else # Otherwise, perform a "bare" insert_all. return self.class.get_model.insert_all(self.get_create_params) end end end |