Class: Spina::Admin::ImagesController

Inherits:
AdminController 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

#add_to_media_folderObject



37
38
39
40
41
# File 'app/controllers/spina/admin/images_controller.rb', line 37

def add_to_media_folder
  @media_folder = MediaFolder.find(params[:id])
  @media_folder.images << Image.find(params[:image_id])
  render json: @media_folder
end

#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



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

def create
  @images = params[:image][:files].map do |file|
    # Create the image and attach the file
    image = Image.create(media_folder_id: params[:media_library])
    image.file.attach(file)

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

    image
  end.compact
end

#destroyObject



31
32
33
34
35
# File 'app/controllers/spina/admin/images_controller.rb', line 31

def destroy
  @image = Image.find(params[:id])
  @image.destroy
  redirect_back fallback_location: spina.admin_images_url
end

#indexObject



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

def index
  add_breadcrumb I18n.t('spina.website.images'), admin_images_path
  @media_folders = MediaFolder.order(:name)
  @images = Image.sorted.where(media_folder_id: nil).with_attached_file.page(params[:page])
end