Sha256: bb8144f5e909e5d28b2ebc90bad4dfd5a590e765a6d8c0e06b5dcf5c29054230

Contents?: true

Size: 1.19 KB

Versions: 35

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Motor
  class ActiveStorageAttachmentsController < ApiBaseController
    include Motor::WrapIoParams

    wrap_parameters :data, except: %i[include fields]

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

    def create
      if attachable?(record)
        record.public_send(@attachment.name).attach(file_params)

        head :ok
      else
        head :unprocessable_entity
      end
    end

    private

    def record
      record_pk = @attachment.record.class.primary_key

      Motor::Resources::FetchConfiguredModel.call(
        @attachment.record.class,
        cache_key: Motor::Resource.maximum(:updated_at)
      ).find_by(record_pk => @attachment.record[record_pk])
    end

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

    def file_params
      params.require(:data).require(:file).permit(:io, :filename).to_h.symbolize_keys
    end

    def attachment_params
      if params[:data].present?
        params.require(:data).except(:file).permit!
      else
        {}
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
motor-admin-0.2.68 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.67 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.66 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.65 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.64 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.63 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.62 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.61 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.60 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.59 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.58 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.57 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.56 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.55 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.54 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.53 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.51 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.50 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.49 app/controllers/motor/active_storage_attachments_controller.rb
motor-admin-0.2.48 app/controllers/motor/active_storage_attachments_controller.rb