Class: Spina::Admin::AttachmentsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/spina/admin/attachments_controller.rb

Instance Method Summary collapse

Methods inherited from AdminController

#current_admin_path

Instance Method Details

#createObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/spina/admin/attachments_controller.rb', line 18

def create
  @attachments = params[:attachment][:files].map do |file|
    next if file.blank? # Skip the blank string posted by the hidden files[] field

    attachment = Attachment.create(attachment_params)
    attachment.file.attach(file)
    attachment
  end.compact

  respond_to do |format|
    format.turbo_stream { render turbo_stream: turbo_stream.prepend("attachments", partial: "attachment", collection: @attachments) }
    format.html { redirect_to spina.admin_attachments_url }
  end
end

#destroyObject



57
58
59
60
61
# File 'app/controllers/spina/admin/attachments_controller.rb', line 57

def destroy
  @attachment = Attachment.find(params[:id])
  @attachment.destroy
  render turbo_stream: turbo_stream.remove(@attachment)
end

#editObject



14
15
16
# File 'app/controllers/spina/admin/attachments_controller.rb', line 14

def edit
  @attachment = Attachment.find(params[:id])
end

#indexObject



6
7
8
# File 'app/controllers/spina/admin/attachments_controller.rb', line 6

def index
  @attachments = Attachment.sorted.with_attached_file.with_filename(params[:query].to_s).page(params[:page]).per(25)
end

#inline_uploadObject



33
34
35
36
# File 'app/controllers/spina/admin/attachments_controller.rb', line 33

def inline_upload
  @attachment = Attachment.create(attachment_params)
  render turbo_stream: turbo_stream.append(params[:select_id], partial: "parts/attachments/attachment", object: @attachment)
end

#showObject



10
11
12
# File 'app/controllers/spina/admin/attachments_controller.rb', line 10

def show
  @attachment = Attachment.find(params[:id])
end

#updateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/spina/admin/attachments_controller.rb', line 38

def update
  @attachment = Attachment.find(params[:id])
  old_signed_id = @attachment.file&.blob&.signed_id
  @attachment.update(attachment_params) if params[:attachment].present?
  if params[:filename].present?
    extension = @attachment.file.filename.extension
    filename = "#{params[:filename]}.#{extension}"
    @attachment.file.blob.update(filename: filename)
  end

  # Replace all occurrences of the old signed blob ID
  # with the new ID in a background job
  if @attachment.reload.file&.blob&.signed_id != old_signed_id
    Spina::ReplaceSignedIdJob.perform_later(old_signed_id, @attachment.file&.blob&.signed_id)
  end

  render @attachment
end