Class: Formstrap::FileView

Inherits:
ViewModel show all
Includes:
Hintable, InputGroupable, Labelable, Placeholderable, Validatable, Wrappable
Defined in:
app/models/formstrap/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



41
42
43
# File 'app/models/formstrap/file_view.rb', line 41

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

#attachmentsObject



45
46
47
# File 'app/models/formstrap/file_view.rb', line 45

def attachments
  attached&.attachments || []
end

#destroyObject



37
38
39
# File 'app/models/formstrap/file_view.rb', line 37

def destroy
  @destroy || false
end

#dropzone_optionsObject



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

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

#input_group_optionsObject



18
19
20
21
22
# File 'app/models/formstrap/file_view.rb', line 18

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

#input_optionsObject



10
11
12
13
14
15
16
# File 'app/models/formstrap/file_view.rb', line 10

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



53
54
55
# File 'app/models/formstrap/file_view.rb', line 53

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

#number_of_filesObject



49
50
51
# File 'app/models/formstrap/file_view.rb', line 49

def number_of_files
  multiple ? 2 : 1
end

#previewObject



33
34
35
# File 'app/models/formstrap/file_view.rb', line 33

def preview
  @preview || dropzone
end

#wrapper_optionsObject



24
25
26
27
28
29
30
31
# File 'app/models/formstrap/file_view.rb', line 24

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