class CmAdmin::ResourceController

def resource_responder

def resource_responder
  respond_to do |format|
    if @ar_object.save
      redirect_url = if params['referrer']
                       params['referrer']
                     elsif @current_action.redirect_to.present?
                       @current_action.redirect_to.call(@ar_object)
                     else
                       cm_admin.send("cm_show_#{@model.name.underscore}_path", @ar_object)
                     end
      ActiveStorage::Attachment.where(id: params['attachment_destroy_ids']).destroy_all if params['attachment_destroy_ids'].present?
      notice = if @current_action&.name == 'new'
                 "#{@model&.formatted_name} was created"
               elsif @current_action&.name == 'edit'
                 "#{@model&.formatted_name} was updated"
               else
                 "#{@action&.formatted_name} #{@model&.formatted_name} was successful"
               end
      format.html { redirect_to redirect_url, allow_other_host: true, notice: }
      name = if @ar_object.respond_to?(:formatted_name)
               @ar_object.formatted_name
             elsif @ar_object.respond_to?(:name)
               @ar_object.name
             else
               @ar_object&.id
             end
      format.json { render json: { message: notice, data: { id: @ar_object&.id, name: } }, status: :ok }
    else
      format.html { render '/cm_admin/main/new', notice: "#{@action&.formatted_name} #{@model&.formatted_name} was unsuccessful" }
      format.json do
        formatted_error_response = @ar_object.errors.full_messages.map { |error_message| "<li>#{error_message}</li>" }.join
        render json: { message: formatted_error_response }, status: :unprocessable_entity
      end
    end
  end
end