Sha256: d7acd213eb62ec0664102d3a29722fe4be7b4690dd15848fc2141345f4b642a4

Contents?: true

Size: 952 Bytes

Versions: 12

Compression:

Stored size: 952 Bytes

Contents

# frozen_string_literal: true

class Avo::Fields::Common::Files::ViewType::GridItemComponent < ViewComponent::Base
  attr_reader :field, :resource

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

12 entries across 12 versions & 1 rubygems

Version Path
avo-2.53.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.52.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.49.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.48.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.47.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.46.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.45.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.44.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.43.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.42.2 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.42.1 app/components/avo/fields/common/files/view_type/grid_item_component.rb
avo-2.42.0 app/components/avo/fields/common/files/view_type/grid_item_component.rb