Class: Fields::File

Inherits:
Field
  • Object
show all
Defined in:
app/models/iron/fields/file.rb

Instance Method Summary collapse

Instance Method Details

#content_value=(value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/iron/fields/file.rb', line 7

def content_value=(value)
  @content_errors = nil
  if value.nil?
    self.file = nil
  elsif !value.is_a?(String)
    add_content_error("must be an upload token (signed id)")
  elsif value.present?
    self.file = ActiveStorage::Blob.find_signed!(value)
  end
rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveRecord::RecordNotFound
  add_content_error("has an invalid upload token")
end

#export_attachmentsObject



32
33
34
35
36
# File 'app/models/iron/fields/file.rb', line 32

def export_attachments
  return [] unless file.attached?

  [ file.blob ]
end

#export_valueObject



26
27
28
29
30
# File 'app/models/iron/fields/file.rb', line 26

def export_value
  return { type: "file", value: nil } unless file.attached?

  { type: "file", value: export_attachment_path }
end

#filled?Boolean

Returns:



20
# File 'app/models/iron/fields/file.rb', line 20

def filled? = file.attached?

#image?Boolean

Returns:



38
39
40
# File 'app/models/iron/fields/file.rb', line 38

def image?
  file.attached? && file.blob.content_type&.start_with?("image/")
end

#valueObject



22
23
24
# File 'app/models/iron/fields/file.rb', line 22

def value
  file.attached? ? file : nil
end