Class: Avo::Fields::Common::Files::ViewType::GridItemComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
Avo::Fields::Concerns::FileAuthorization
Defined in:
app/components/avo/fields/common/files/view_type/grid_item_component.rb

Direct Known Subclasses

ListItemComponent

Instance Method Summary collapse

Methods included from Avo::Fields::Concerns::FileAuthorization

#can_delete_file?, #can_download_file?, #can_reupload_file?, #can_upload_file?

Instance Method Details

#blob_persisted?Boolean

A blob is saved only when its record is. After a failed validation the attachment lives in memory alone, and every URL built from it -- image src, download link -- dies on signed_id. The file isn't attached, so skip it.

Returns:



46
47
48
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 46

def blob_persisted?
  file.nil? || file.blob&.persisted?
end

#document_argumentsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 58

def document_arguments
  args = {
    class: class_names(
      "relative flex flex-col justify-evenly items-center px-2 rounded-lg border bg-primary border-tertiary min-h-24",
      {
        "hover:bg-secondary transition": file.representable?
      }
    )
  }

  if file.representable? && can_download_file?
    args.merge!(
      {
        href: helpers.main_app.url_for(file),
        target: "_blank",
        rel: "noopener noreferrer"
      }
    )
  end

  args
end

#fileObject



15
16
17
18
19
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 15

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

#idObject



11
12
13
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 11

def id
  @field.id
end

#is_audio?Boolean

Returns:



27
28
29
30
31
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 27

def is_audio?
  file.audio?
rescue
  false
end

#is_image?Boolean

Returns:



21
22
23
24
25
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 21

def is_image?
  file.image?
rescue
  false
end

#is_video?Boolean

Returns:



33
34
35
36
37
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 33

def is_video?
  file.video?
rescue
  false
end

#record_persisted?Boolean

If record is not persistent blob is automatically destroyed otherwise it can be "lost" on storage

Returns:



51
52
53
54
55
56
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 51

def record_persisted?
  return true if @resource.record.persisted?

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

#render?Boolean

Returns:



39
40
41
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 39

def render?
  record_persisted? && blob_persisted?
end