Sha256: 1352ab833c307d51500a197a3453bad21251411b9a5615db006714310e219ed7

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

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

  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

7 entries across 7 versions & 1 rubygems

Version Path
avo-2.29.1 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.29.1.pre.pr1652 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.29.0 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.28.3.pre.pr1646 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.28.2.pre.pr1642 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.28.1.pre.pr1642 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.28.0 app/components/avo/fields/common/single_file_viewer_component.rb