app/controllers/motor/data_controller.rb in motor-admin-0.1.28 vs app/controllers/motor/data_controller.rb in motor-admin-0.1.29
- old
+ new
@@ -1,16 +1,17 @@
# frozen_string_literal: true
module Motor
class DataController < ApiBaseController
+ include Motor::WrapIoParams
+
INSTANCE_VARIABLE_NAME = 'resource'
wrap_parameters :data, except: %i[include fields]
before_action :load_and_authorize_resource
before_action :load_and_authorize_association
- before_action :wrap_io_params
def index
@resources = Motor::ApiQuery.call(@resources, params)
render json: {
@@ -25,16 +26,24 @@
def create
@resource.save!
render json: { data: Motor::ApiQuery::BuildJson.call(@resource, params) }
+ rescue ActiveRecord::RecordInvalid
+ render json: { errors: @resource.errors }, status: :unprocessable_entity
+ rescue StandardError => e
+ render json: { errors: [e.message] }, status: :unprocessable_entity
end
def update
@resource.update!(resource_params)
render json: { data: Motor::ApiQuery::BuildJson.call(@resource, params) }
+ rescue ActiveRecord::RecordInvalid
+ render json: { errors: @resource.errors }, status: :unprocessable_entity
+ rescue StandardError => e
+ render json: { errors: [e.message] }, status: :unprocessable_entity
end
def destroy
if @resource.respond_to?(:deleted_at)
@resource.update(deleted_at: Time.current)
@@ -75,10 +84,12 @@
CanCan::ControllerResource.new(
self,
options
).load_and_authorize_resource
+ rescue StandardError => e
+ render json: { errors: [e.message] }, status: :unprocessable_entity
end
def load_and_authorize_association
return if params[:association].blank?
@@ -94,28 +105,18 @@
instance_name: INSTANCE_VARIABLE_NAME
).load_and_authorize_resource
else
render json: { message: 'Unknown association' }, status: :not_found
end
+ rescue StandardError => e
+ render json: { errors: [e.message] }, status: :unprocessable_entity
end
def resource_params
if params[:data].present?
params.require(:data).except(resource_class.primary_key).permit!
else
{}
end
- end
-
- def wrap_io_params(hash = params)
- hash.each do |key, value|
- if key == 'io'
- hash[key] = StringIO.new(value.encode('ISO-8859-1'))
- elsif value.is_a?(ActionController::Parameters)
- wrap_io_params(value)
- end
- end
-
- hash
end
end
end