Module: RESTFramework::Mixins::BulkUpdateModelMixin

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

Overview

Mixin for updating records in bulk.

Instance Method Summary collapse

Instance Method Details

#update_allObject



34
35
36
37
38
# File 'lib/rest_framework/mixins/bulk_model_controller_mixin.rb', line 34

def update_all
  records = self.update_all!
  serialized_records = self.bulk_serialize(records)
  render(api: serialized_records)
end

#update_all!Object

Perform the ‘update` call and return the collection of (possibly) updated records.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rest_framework/mixins/bulk_model_controller_mixin.rb', line 41

def update_all!
  pk = self.class.get_model.primary_key
  data = if params[:_json].is_a?(Array)
    self.get_create_params(bulk_mode: :update)[:_json].index_by { |r| r[pk] }
  else
    create_params = self.get_create_params
    { create_params[pk] => create_params }
  end

  # Perform bulk update in a transaction.
  ActiveRecord::Base.transaction { self.get_recordset.update(data.keys, data.values) }
end