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_upload_file?

Instance Method Details

#document_argumentsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 51

def document_arguments
  args = {
    class: class_names(
      "relative flex flex-col justify-evenly items-center px-2 rounded-lg border bg-white border-gray-500 min-h-24",
      {
        "hover:bg-gray-100 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:

  • (Boolean)


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? || @field.is_audio
rescue
  false
end

#is_image?Boolean

Returns:

  • (Boolean)


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? || @field.is_image
rescue
  false
end

#is_video?Boolean

Returns:

  • (Boolean)


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? || @field.is_video
rescue
  false
end

#record_persisted?Boolean

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

Returns:

  • (Boolean)


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

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:

  • (Boolean)


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

def render?
  record_persisted?
end