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

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/avo/fields/common/files/view_type/grid_item_component.rb

Direct Known Subclasses

ListItemComponent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field:, resource:, file: nil, extra_classes: nil) ⇒ GridItemComponent

Returns a new instance of GridItemComponent.



6
7
8
9
10
11
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 6

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

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



4
5
6
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 4

def field
  @field
end

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'app/components/avo/fields/common/files/view_type/grid_item_component.rb', line 4

def resource
  @resource
end

Instance Method Details

#fileObject



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

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

#idObject



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

def id
  field.id
end

#is_audio?Boolean

Returns:

  • (Boolean)


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

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

#is_image?Boolean

Returns:

  • (Boolean)


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

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

#is_video?Boolean

Returns:

  • (Boolean)


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

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)


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

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)


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

def render?
  record_persisted?
end