Class: Avo::Fields::Common::SingleFileViewerComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
ApplicationHelper, Avo::Fields::Concerns::FileAuthorization
Defined in:
app/components/avo/fields/common/single_file_viewer_component.rb

Instance Method Summary collapse

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

#can_delete_file?, #can_download_file?, #can_upload_file?

Methods included from ApplicationHelper

#a_button, #a_link, #button_classes, #empty_state, #get_model_class, #input_classes, #mount_path, #render_license_warning, #render_license_warnings, #root_path_without_url, #svg, #white_panel_classes

Constructor Details

#initialize(field:, resource:, file: nil) ⇒ SingleFileViewerComponent

Returns a new instance of SingleFileViewerComponent.



7
8
9
10
11
# File 'app/components/avo/fields/common/single_file_viewer_component.rb', line 7

def initialize(field:, resource:, file: nil)
  @file = file
  @field = field
  @resource = resource
end

Instance Method Details

#destroy_pathObject



13
14
15
# File 'app/components/avo/fields/common/single_file_viewer_component.rb', line 13

def destroy_path
  Avo::Services::URIService.parse(@resource.record_path).append_paths("active_storage_attachments", id, file.id).to_s
end

#fileObject



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

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

#idObject



17
18
19
# File 'app/components/avo/fields/common/single_file_viewer_component.rb', line 17

def id
  @field.id
end

#is_audio?Boolean

Returns:

  • (Boolean)


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

def is_audio?
  file.audio? || @field.is_audio
rescue
  false
end

#is_image?Boolean

Returns:

  • (Boolean)


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

def is_image?
  file.image? || @field.is_image
rescue
  false
end

#is_video?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'app/components/avo/fields/common/single_file_viewer_component.rb', line 39

def is_video?
  file.video? || @field.is_video
rescue
  false
end

#record_persisted?Boolean

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

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'app/components/avo/fields/common/single_file_viewer_component.rb', line 50

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

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

#render?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/components/avo/fields/common/single_file_viewer_component.rb', line 45

def render?
  record_persisted?
end