Class: Avo::AttachmentsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/avo/attachments_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#_current_user, #check_avo_license, #context, #exception_logger, #init_app, #render, #turbo_frame_request?

Methods included from Concerns::Breadcrumbs

#add_breadcrumb, #avo_breadcrumbs

Methods included from UrlHelpers

#edit_resource_path, #new_resource_path, #related_resources_path, #resource_attach_path, #resource_detach_path, #resource_path, #resource_view_path, #resources_path

Methods included from ApplicationHelper

#a_button, #a_link, #button_classes, #empty_state, #get_model_class, #input_classes, #mount_path, #render_license_warning, #render_license_warnings, #root_path_without_url, #svg, #white_panel_classes

Instance Method Details

#createObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/avo/attachments_controller.rb', line 9

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

#destroyObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/avo/attachments_controller.rb', line 25

def destroy
  raise Avo::NotAuthorizedError.new unless authorized_to :delete

  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