Class: Spina::Admin::ImagesController

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

Instance Method Summary collapse

Methods inherited from AdminController

#current_admin_path

Instance Method Details

#createObject

There’s no file validation yet in ActiveStorage We do two things to reduce errors right now:

  1. We add accept=“image/*” to the image form

  2. We destroy the entire record if the uploaded file is not an image



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/spina/admin/images_controller.rb', line 24

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

    # Create the image and attach the file
    image = Image.create(media_folder_id: image_params[:media_folder_id])
    image.file.attach(file)

    # Was it not an image after all? DESTROY IT
    image.destroy and next unless image.file.image?

    image
  end.compact

  if params[:origin] == 'media-picker'
    redirect_to spina.admin_media_picker_path(media_folder_id: image_params[:media_folder_id])
  elsif params[:origin] == 'auto-file-upload'
    render turbo_stream: turbo_stream.update(
      'auto-file-upload-images',
      Spina::Forms::EditorInsertImagesMetaComponent
        .new(images: @images, trix_target_id: params[:trix_target_id])
        .render_in(view_context)
    )
  else
    render turbo_stream: turbo_stream.prepend("images", partial: "image", collection: @images)
  end
end

#destroyObject



76
77
78
79
80
# File 'app/controllers/spina/admin/images_controller.rb', line 76

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

#editObject



16
17
18
# File 'app/controllers/spina/admin/images_controller.rb', line 16

def edit
  @image = Image.find(params[:id])
end

#indexObject



7
8
9
10
# File 'app/controllers/spina/admin/images_controller.rb', line 7

def index
  @media_folders = MediaFolder.order(:name).includes(:images)
  @images = Image.sorted.where(media_folder: @media_folder).with_attached_file.with_filename(params[:query].to_s).page(params[:page]).per(25)
end

#showObject



12
13
14
# File 'app/controllers/spina/admin/images_controller.rb', line 12

def show
  @image = Image.find(params[:id])
end

#updateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/spina/admin/images_controller.rb', line 52

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

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

    @media_folders = MediaFolder.order(:name)
    render @image
  end
end