Class: FieldDefinitions::File

Inherits:
FieldDefinition
  • Object
show all
Defined in:
app/models/iron/field_definitions/file.rb

Constant Summary collapse

FILE_TYPE_PRESETS =
{
  "images" => {
    extensions: %w[jpg jpeg png gif webp svg heic],
    mime_types: %w[image/jpeg image/png image/gif image/webp image/svg+xml image/heic]
  },
  "videos" => {
    extensions: %w[mp4 mov avi webm],
    mime_types: %w[video/mp4 video/quicktime video/x-msvideo video/webm]
  },
  "documents" => {
    extensions: %w[pdf doc docx xls xlsx ppt pptx txt],
    mime_types: %w[
      application/pdf
      application/msword
      application/vnd.openxmlformats-officedocument.wordprocessingml.document
      application/vnd.ms-excel
      application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
      application/vnd.ms-powerpoint
      application/vnd.openxmlformats-officedocument.presentationml.presentation
      text/plain
    ]
  },
  "audio" => {
    extensions: %w[mp3 wav ogg m4a],
    mime_types: %w[audio/mpeg audio/wav audio/ogg audio/mp4]
  }
}.freeze

Instance Method Summary collapse

Instance Method Details

#accepted_extensionsObject



41
42
43
44
45
# File 'app/models/iron/field_definitions/file.rb', line 41

def accepted_extensions
  return nil if file_scope == "all"

  selected_presets.flat_map { |preset| FILE_TYPE_PRESETS.dig(preset, :extensions) || [] }.uniq
end

#accepted_mime_typesObject



47
48
49
50
51
# File 'app/models/iron/field_definitions/file.rb', line 47

def accepted_mime_types
  return nil if file_scope == "all"

  selected_presets.flat_map { |preset| FILE_TYPE_PRESETS.dig(preset, :mime_types) || [] }.uniq
end

#file_scopeObject



33
34
35
# File 'app/models/iron/field_definitions/file.rb', line 33

def file_scope
  super || "all"
end

#selected_presetsObject



37
38
39
# File 'app/models/iron/field_definitions/file.rb', line 37

def selected_presets
  super || []
end

#validation_descriptionObject



53
54
55
56
57
# File 'app/models/iron/field_definitions/file.rb', line 53

def validation_description
  return "All file types accepted" if file_scope == "all"

  "Accepted formats: #{accepted_extensions.join(', ')}" if accepted_extensions.present?
end