Sha256: b5dcec1b509783279c564488d4ae311b7e7da96705bb62a4cf24045bcc24c4a7

Contents?: true

Size: 1.27 KB

Versions: 21

Compression:

Stored size: 1.27 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], 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
      attachment = ActiveStorage::Attachment.find(params[:attachment_id])
      path = resource_path(model: @model, resource: @resource)

      if attachment.present?
        attachment.destroy

        redirect_to params[:referrer] || path, notice: t("avo.attachment_destroyed")
      else
        redirect_back fallback_location: path, notice: t("avo.failed_to_find_attachment")
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
avo-2.27.2.pre.pr1606 app/controllers/avo/attachments_controller.rb
avo-2.27.1 app/controllers/avo/attachments_controller.rb
avo-2.27.0 app/controllers/avo/attachments_controller.rb
avo-2.26.3.pre.pr1601 app/controllers/avo/attachments_controller.rb
avo-2.26.2.pre.pr1579 app/controllers/avo/attachments_controller.rb
avo-2.26.1.pr1584.pre.1 app/controllers/avo/attachments_controller.rb
avo-2.25.1.pre.1.pr1579 app/controllers/avo/attachments_controller.rb
avo-2.26.0 app/controllers/avo/attachments_controller.rb
avo-2.25.0 app/controllers/avo/attachments_controller.rb
avo-2.24.1 app/controllers/avo/attachments_controller.rb
avo-2.24.0 app/controllers/avo/attachments_controller.rb
avo-2.23.3.pre.1.pr1529 app/controllers/avo/attachments_controller.rb
avo-2.23.2 app/controllers/avo/attachments_controller.rb
avo-2.23.1 app/controllers/avo/attachments_controller.rb
avo-2.23.0 app/controllers/avo/attachments_controller.rb
avo-2.22.0 app/controllers/avo/attachments_controller.rb
avo-2.21.3.pre.pr1489 app/controllers/avo/attachments_controller.rb
avo-2.21.2.pre.pr1486 app/controllers/avo/attachments_controller.rb
avo-2.21.1.pre.pr1484 app/controllers/avo/attachments_controller.rb
avo-2.21.0 app/controllers/avo/attachments_controller.rb