Sha256: 46fb1d45cb2ca031a32b1aa09a89dc695ae0f042cd5b9c895a59a88b1ed67d89

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

class Headmin::MediaController < HeadminController
  layout false

  def index
    @blobs =
      ActiveStorage::Blob
        .left_outer_joins(:attachments)
        .where.not(active_storage_attachments: {record_type: "ActiveStorage::VariantRecord"}) # Not a variant
        .or(ActiveStorage::Blob.where(active_storage_attachments: {id: nil})) # Or an orphan
        .order(created_at: :desc)
        .group(:id)
        .all
  end

  def create
    blobs = []
    media_params[:files].reject { |c| c.blank? }.each do |file|
      blobs << ActiveStorage::Blob.create_and_upload!(io: file, filename: file.original_filename)
    end

    respond_to do |format|
      format.turbo_stream {
        @blobs = blobs
      }
      format.html { redirect_to root_path }
    end
  end

  def show
    @blob = ActiveStorage::Blob.find(params[:id])
  end

  def update
    @blob = ActiveStorage::Blob.find(params[:id])
    media_item_params[:filename] = media_item_params[:filename] + "." + @blob.filename.to_s.rpartition(".").last
    if @blob.update(media_item_params)
      flash.now[:notice] = t("admin.flash.updated", name: @blob.filename)
    end
  end

  private

  def media_params
    params.permit(
      files: []
    )
  end

  def media_item_params
    params.require(:blob).permit!
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
headmin-0.5.5 app/controllers/headmin/media_controller.rb
headmin-0.5.4 app/controllers/headmin/media_controller.rb
headmin-0.5.3 app/controllers/headmin/media_controller.rb
headmin-0.5.2 app/controllers/headmin/media_controller.rb