Class: Headmin::Form::FileView

Inherits:
ViewModel show all
Includes:
Hintable, InputGroupable, Labelable, Placeholderable, Validatable, Wrappable
Defined in:
app/models/headmin/form/file_view.rb

Instance Method Summary collapse

Methods inherited from ViewModel

#attributes, #initialize, #to_hash

Constructor Details

This class inherits a constructor from ViewModel

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ViewModel

Instance Method Details

#attachedObject



42
43
44
# File 'app/models/headmin/form/file_view.rb', line 42

def attached
  form.object&.send(attribute)
end

#attachmentsObject



46
47
48
# File 'app/models/headmin/form/file_view.rb', line 46

def attachments
  attached&.attachments || []
end

#destroyObject



38
39
40
# File 'app/models/headmin/form/file_view.rb', line 38

def destroy
  @destroy || false
end

#dropzone_optionsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/headmin/form/file_view.rb', line 58

def dropzone_options
  if dropzone
    {
      class: ["h-dropzone", validation_class],
      data: {
        controller: "dropzone"
      }
    }
  else
    {
      class: validation_class
    }
  end
end

#input_group_optionsObject



19
20
21
22
23
# File 'app/models/headmin/form/file_view.rb', line 19

def input_group_options
  default_input_group_options
    .deep_merge(label_input_group_options)
    .deep_merge(@input_group || {})
end

#input_optionsObject



11
12
13
14
15
16
17
# File 'app/models/headmin/form/file_view.rb', line 11

def input_options
  keys = attributes - %i[append attribute dropzone destroy form input_group label prepend preview validate wrapper]
  options = to_h.slice(*keys)
  options = default_input_options.deep_merge(options)
  options = options.deep_merge(required: false) if attachments.any?
  options
end

#nested_attributeObject



54
55
56
# File 'app/models/headmin/form/file_view.rb', line 54

def nested_attribute
  attached.is_a?(ActiveStorage::Attached::Many) ? :"#{attribute}_attachments" : :"#{attribute}_attachment"
end

#number_of_filesObject



50
51
52
# File 'app/models/headmin/form/file_view.rb', line 50

def number_of_files
  multiple ? 2 : 1
end

#previewObject



34
35
36
# File 'app/models/headmin/form/file_view.rb', line 34

def preview
  @preview || dropzone
end

#wrapper_optionsObject



25
26
27
28
29
30
31
32
# File 'app/models/headmin/form/file_view.rb', line 25

def wrapper_options
  default_wrapper_options.deep_merge({
    class: ["mb-3 h-form-file", ("form-floating" if float)],
    data: {
      controller: ("file-preview" if preview)
    }
  }).deep_merge(@wrapper || {})
end