Sha256: 611df41acc150b75ad1c409d711af4b28649059f79434dabf7eb4a506291ddd5

Contents?: true

Size: 1.02 KB

Versions: 54

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

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

  def initialize(file: nil, field:, resource:)
    @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

54 entries across 54 versions & 1 rubygems

Version Path
avo-2.27.2.pre.pr1606 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.27.1 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.27.0 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.26.3.pre.pr1601 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.26.2.pre.pr1579 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.26.1.pr1584.pre.1 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.25.1.pre.1.pr1579 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.26.0 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.25.0 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.24.1 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.24.0 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.23.3.pre.1.pr1529 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.23.2 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.23.1 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.23.0 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.22.0 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.21.3.pre.pr1489 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.21.2.pre.pr1486 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.21.1.pre.pr1484 app/components/avo/fields/common/single_file_viewer_component.rb
avo-2.21.0 app/components/avo/fields/common/single_file_viewer_component.rb