Sha256: 3492edba3359baa41784dcea9d95986ed3bf4e4a44eb2d7342ece2ce8411840a

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

class Headmin::MediaController < HeadminController
  layout false

  def index
    @blobs =
      ActiveStorage::Blob
        .not_attached_to_variant
        .by_mimetypes_string(media_params[:mimetype])
        .order(created_at: :desc)
        .group(:id)
        .all
    @mimetypes = media_params[:mimetype]
  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(
      :min,
      :max,
      :name,
      :mimetype,
      ids: [],
      files: []
    )
  end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
headmin-0.6.1 app/controllers/headmin/media_controller.rb
headmin-0.6.0 app/controllers/headmin/media_controller.rb
headmin-0.5.9 app/controllers/headmin/media_controller.rb
headmin-0.5.8 app/controllers/headmin/media_controller.rb
headmin-0.5.7 app/controllers/headmin/media_controller.rb
headmin-0.5.6 app/controllers/headmin/media_controller.rb