Sha256: 76e21a433358cd7c85a48657f9a86cb0354031f66b0ecbb4ae21326556c9e33a

Contents?: true

Size: 1.08 KB

Versions: 5

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 record is not persistent blob is automatically destroyed otherwise it can be "lost" on storage
  def record_persisted?
    return true if @resource.record.persisted?

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
avo-3.0.0.pre8 app/components/avo/fields/common/single_file_viewer_component.rb
avo-3.0.0.pre7 app/components/avo/fields/common/single_file_viewer_component.rb
avo-3.0.0.pre5 app/components/avo/fields/common/single_file_viewer_component.rb
avo-3.0.0.pre6 app/components/avo/fields/common/single_file_viewer_component.rb
avo-3.0.0.pre4 app/components/avo/fields/common/single_file_viewer_component.rb