Sha256: 081ee63b5dabbc0e9188f7bc1fc73be15d8e0ed609d25cff74f165a04cc0dced

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

class Avo::Fields::Common::SingleFileViewerComponent < ViewComponent::Base
  include Avo::ApplicationHelper
  include Avo::Fields::Concerns::FileAuthorization

  attr_reader :field

  def initialize(field:, resource:, file: nil)
    @file = file
    @field = field
    @resource = resource
  end

  def destroy_path
    Avo::Services::URIService.parse(@resource.record_path).append_paths("active_storage_attachments", id, file.id).to_s
  end

  def id
    field.id
  end

  def file
    @file || field.value.attachment
  rescue
    nil
  end

  def is_image?
    file.image? || field.is_image
  rescue
    false
  end

  def is_audio?
    file.audio? || field.is_audio
  rescue
    false
  end

  def is_video?
    file.video? || field.is_video
  rescue
    false
  end

  def render?
    record_persisted?
  end

  # If model is not persistent blob is automatically destroyed otherwise it can be "lost" on storage
  def record_persisted?
    return true if @resource.model.persisted?

    ActiveStorage::Blob.destroy(file.blob_id) if file.blob_id.present?
    false
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
avo-2.30.1 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.30.0 app/components/avo/fields/common/single_file_viewer_component.rb