Sha256: 831c6f35a85383f4561f5c530f1fb10488c1248f31019d4bb1fce195a7164fa8

Contents?: true

Size: 1.55 KB

Versions: 20

Compression:

Stored size: 1.55 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_model, 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(@model, params[:attachment_key])

      if association_name.blank?
        raise ActionController::BadRequest.new("Could not find the attachment association for #{params[:attachment_key]} (check the `attachment_key` for this Trix field)")
      end

      @model.send(association_name).attach blob

      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: @model, raise_exception: false)
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
avo-2.53.0 app/controllers/avo/attachments_controller.rb
avo-2.52.0 app/controllers/avo/attachments_controller.rb
avo-2.49.0 app/controllers/avo/attachments_controller.rb
avo-2.48.0 app/controllers/avo/attachments_controller.rb
avo-2.47.0 app/controllers/avo/attachments_controller.rb
avo-2.46.0 app/controllers/avo/attachments_controller.rb
avo-2.45.0 app/controllers/avo/attachments_controller.rb
avo-2.44.0 app/controllers/avo/attachments_controller.rb
avo-2.43.0 app/controllers/avo/attachments_controller.rb
avo-2.42.2 app/controllers/avo/attachments_controller.rb
avo-2.42.1 app/controllers/avo/attachments_controller.rb
avo-2.42.0 app/controllers/avo/attachments_controller.rb
avo-2.41.0 app/controllers/avo/attachments_controller.rb
avo-2.40.0 app/controllers/avo/attachments_controller.rb
avo-2.39.0 app/controllers/avo/attachments_controller.rb
avo-2.38.0 app/controllers/avo/attachments_controller.rb
avo-2.37.2 app/controllers/avo/attachments_controller.rb
avo-2.37.1 app/controllers/avo/attachments_controller.rb
avo-2.37.0 app/controllers/avo/attachments_controller.rb
avo-2.36.3 app/controllers/avo/attachments_controller.rb