Class: Avo::AttachmentsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#exception_logger, #hello, #turbo_frame_request?

Methods included from Concerns::Breadcrumbs

#add_breadcrumb, #avo_breadcrumbs

Methods included from UrlHelpers

#edit_resource_path, #new_resource_path, #preview_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, #card_classes, #chart_color, #decode_filter_params, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #number_to_social, #render_license_warning, #root_path_without_url, #svg, #white_panel_classes

Methods included from ResourcesHelper

#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_select_all_input, #item_selector_init, #item_selector_input, #resource_grid, #resource_table

Methods included from InitializesAvo

#_current_user, #context, #init_app

Instance Method Details

#createObject



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

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

#destroyObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/avo/attachments_controller.rb', line 27

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