Sha256: 7885de2bc18efee067adb8f9d2c3a19c614c01ab4d8e24296091101c53c18c5a

Contents?: true

Size: 1.71 KB

Versions: 48

Compression:

Stored size: 1.71 KB

Contents

require_dependency "avo/application_controller"

module Avo
  class AttachmentsController < ApplicationController
    before_action :set_resource_name, only: [:destroy, :create]
    before_action :set_resource, only: [:destroy, :create]
    before_action :set_record, only: [:destroy, :create]

    def create
      blob = ActiveStorage::Blob.create_and_upload! io: params[:file].to_io, filename: params[:filename]
      association_name = BaseResource.valid_attachment_name(@record, params[:attachment_key])

      # If association name is present attach the blob to it
      if association_name
        @record.send(association_name).attach blob
      # If key is present use the blob from the key else raise error
      elsif params[:key].blank?
        raise ActionController::BadRequest.new("Could not find the attachment association for #{params[:attachment_key]} (check the `attachment_key` for this Trix field)")
      end

      render json: {
        url: main_app.url_for(blob),
        href: main_app.url_for(blob)
      }
    end

    def destroy
      if authorized_to :delete
        attachment = ActiveStorage::Attachment.find(params[:attachment_id])

        flash[:notice] = if attachment.present?
          @destroyed = attachment.destroy

          t("avo.attachment_destroyed")
        else
          t("avo.failed_to_find_attachment")
        end
      else
        flash[:notice] = t("avo.not_authorized")
      end

      respond_to do |format|
        format.turbo_stream do
          render "destroy"
        end
      end
    end

    private

    def authorized_to(action)
      @resource.authorization.authorize_action("#{action}_#{params[:attachment_name]}?", record: @record, raise_exception: false)
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
avo-3.15.1 app/controllers/avo/attachments_controller.rb
avo-3.15.0 app/controllers/avo/attachments_controller.rb
avo-3.14.5 app/controllers/avo/attachments_controller.rb
avo-3.14.4 app/controllers/avo/attachments_controller.rb
avo-3.14.3 app/controllers/avo/attachments_controller.rb
avo-3.14.2 app/controllers/avo/attachments_controller.rb
avo-3.14.1 app/controllers/avo/attachments_controller.rb
avo-3.14.0 app/controllers/avo/attachments_controller.rb
avo-3.13.7 app/controllers/avo/attachments_controller.rb
avo-3.13.6 app/controllers/avo/attachments_controller.rb
avo-3.13.5 app/controllers/avo/attachments_controller.rb
avo-3.13.4 app/controllers/avo/attachments_controller.rb
avo-3.13.3 app/controllers/avo/attachments_controller.rb
avo-3.13.2 app/controllers/avo/attachments_controller.rb
avo-3.13.1 app/controllers/avo/attachments_controller.rb
avo-3.13.0 app/controllers/avo/attachments_controller.rb
avo-3.12.0 app/controllers/avo/attachments_controller.rb
avo-3.11.10 app/controllers/avo/attachments_controller.rb
avo-3.11.9 app/controllers/avo/attachments_controller.rb
avo-3.11.8 app/controllers/avo/attachments_controller.rb