Class: Jav::AttachmentsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#_current_user, #check_jav_license, #context, #exception_logger, #init_app

Methods included from Concerns::Breadcrumbs

#add_breadcrumb, #jav_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, #decode_filter_params, #empty_state, #encode_filter_params, #frame_id, #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

Raises:

  • (ActionController::BadRequest)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/jav/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(@model, params[:attachment_key])

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

  @model.send(association_name).attach blob
  url = if blob.representable?
          ori_variant = @model.attachment_reflections[association_name]&.named_variants&.fetch(:ori, nil)
          if ori_variant
            main_app.rails_blob_path(blob.representation(ori_variant.transformations))
          else
            main_app.rails_blob_path(blob.representation(resize_to_limit: [1024, 1024], saver: { subsample_mode: "on", strip: true, interlace: true, quality: 85 }))
          end
        else
          main_app.rails_blob_path(blob)
        end

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

#destroyObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/jav/attachments_controller.rb', line 34

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

    flash.now[:notice] = if attachment.present?
                           @destroyed = attachment.destroy
                           t("jav.attachment_destroyed")
                         else
                           t("jav.failed_to_find_attachment")
                         end
  else
    flash.now[:notice] = t("jav.not_authorized")
  end

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