Sha256: 20c03e3e651936e0f1315a20e634381060e78a7cf6626761e79ece9970a26b72

Contents?: true

Size: 1.83 KB

Versions: 12

Compression:

Stored size: 1.83 KB

Contents

class Formstrap::MediaController < FormstrapController
  include Formstrap::Pagination
  layout false

  def index
    blobs = ActiveStorage::Blob
    blobs = filter(blobs)
    blobs = sort(blobs)
    blobs = blobs.group(:id)
    @blobs = paginate(blobs)
    @mimetypes = media_params[:mimetype]

    respond_to do |format|
      format.html
      format.turbo_stream
    end
  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 thumbnail
    @blob = ActiveStorage::Blob.find(params[:id])
  end

  private

  def filter(blobs)
    blobs = filter_unattached(blobs)
    blobs = filter_by_mimetype(blobs, media_params[:mimetype]) if media_params[:mimetype].present?
    blobs = filter_excluded_models(blobs, media_params[:exclude_models]) if media_params[:exclude_models].present?
    blobs = filter_search(blobs, media_params[:search]) if media_params[:search].present?
    blobs
  end

  def filter_unattached(blobs)
    blobs.not_attached_to_variant
  end

  def filter_by_mimetype(blobs, mimetype)
    blobs.by_mimetypes_string(mimetype)
  end

  def filter_excluded_models(blobs, model_names = [])
    blobs.not_attached_to(model_names)
  end

  def filter_search(blobs, string)
    blobs.search(string)
  end

  def sort(blobs)
    blobs.order(created_at: :desc)
  end

  def media_params
    params.permit(
      :max,
      :mimetype,
      :min,
      :name,
      :search,
      :page,
      :page_start,
      :per_page,
      ids: [],
      files: [],
      exclude_models: []
    )
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
formstrap-0.4.5 app/controllers/formstrap/media_controller.rb
formstrap-0.4.4 app/controllers/formstrap/media_controller.rb
formstrap-0.4.3 app/controllers/formstrap/media_controller.rb
formstrap-0.4.2 app/controllers/formstrap/media_controller.rb
formstrap-0.3.5 app/controllers/formstrap/media_controller.rb
formstrap-0.3.4 app/controllers/formstrap/media_controller.rb
formstrap-0.3.3 app/controllers/formstrap/media_controller.rb
formstrap-0.3.2 app/controllers/formstrap/media_controller.rb
formstrap-0.3.1 app/controllers/formstrap/media_controller.rb
formstrap-0.3.0 app/controllers/formstrap/media_controller.rb
formstrap-0.2.1 app/controllers/formstrap/media_controller.rb
formstrap-0.2.0 app/controllers/formstrap/media_controller.rb