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



36
37
38
39
40
# File 'lib/rest_framework/mixins/bulk_model_controller_mixin.rb', line 36

def update_all
  records = self.update_all!
  serialized_records = self.bulk_serialize(records)
  return api_response(serialized_records)
end

#update_all!Object

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rest_framework/mixins/bulk_model_controller_mixin.rb', line 43

def update_all!
  pk = self.class.get_model.primary_key
  update_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.
  return ActiveRecord::Base.transaction do
    next self.get_recordset.update(update_data.keys, update_data.values)
  end
end