Sha256: c2cc09e3a34f7dc0a6d3cd55814bcd3d40ab38a1245275492477873ec7ec45d0

Contents?: true

Size: 883 Bytes

Versions: 1

Compression:

Stored size: 883 Bytes

Contents

# frozen_string_literal: true

module Motor
  class ActiveStorageAttachmentsController < ApiBaseController
    wrap_parameters :data, except: %i[include fields]

    load_and_authorize_resource :attachment, class: 'ActiveStorage::Attachment', parent: false

    def create
      if attachable?(@attachment.record)
        @attachment.record.public_send(@attachment.name).attach(
          io: StringIO.new(params.dig(:data, :file, :io).to_s.encode('ISO-8859-1')),
          filename: params.dig(:data, :file, :filename)
        )

        head :ok
      else
        head :unprocessable_entity
      end
    end

    private

    def attachable?(record)
      record.respond_to?("#{@attachment.name}_attachment=") ||
        record.respond_to?("#{@attachment.name}_attachments=")
    end

    def attachment_params
      params.require(:data).except(:file).permit!
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motor-admin-0.1.28 app/controllers/motor/active_storage_attachments_controller.rb