Class: Headmin::Form::MediaView

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

Instance Attribute Summary collapse

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 Attribute Details

#acceptObject (readonly)

Returns the value of attribute accept.



126
127
128
# File 'app/models/headmin/form/media_view.rb', line 126

def accept
  @accept
end

Instance Method Details

#association_objectObject



66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/headmin/form/media_view.rb', line 66

def association_object
  if attached.is_a?(ActiveStorage::Attached::Many)
    result = form.object.send(nested_attribute)

    # We sort the collection with ruby to prevent a new query to be made that would dispose of the objects in memory
    result = result.sort_by { |item| item.position } if sort
    result
  else
    form.object.send(nested_attribute)
  end
end

#attachedObject



90
91
92
# File 'app/models/headmin/form/media_view.rb', line 90

def attached
  form.object.send(attribute)
end

#attachmentsObject



78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/headmin/form/media_view.rb', line 78

def attachments
  if attached.is_a?(ActiveStorage::Attached::Many)
    result = form.object.send(nested_attribute)

    # We sort the collection with ruby to prevent a new query to be made that would dispose of the objects in memory
    result = result.sort_by { |item| item.position } if sort
    result.to_a.compact
  else
    [form.object.send(nested_attribute)].compact
  end
end

#blob_idsObject



132
133
134
# File 'app/models/headmin/form/media_view.rb', line 132

def blob_ids
  attachments.map { |attachment| attachment.blob_id }
end

#build_nested_attributeObject



94
95
96
97
98
99
100
# File 'app/models/headmin/form/media_view.rb', line 94

def build_nested_attribute
  if attached.is_a?(ActiveStorage::Attached::Many)
    form.object.send(nested_attribute).build
  else
    form.object.send("build_#{nested_attribute}")
  end
end

#custom_validation_optionsObject



44
45
46
47
48
49
50
51
52
# File 'app/models/headmin/form/media_view.rb', line 44

def custom_validation_options
  {
    form: form,
    attribute: attribute,
    min: min,
    max: max,
    accept: accept
  }
end

#input_group_optionsObject



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

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

#item_optionsObject



32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/headmin/form/media_view.rb', line 32

def item_options
  options = {
    sort: sort
  }

  # Don't pass width or height if it was not defined
  options = options.merge(width: width) if is_defined?(:width)
  options = options.merge(height: height) if is_defined?(:height)

  options
end

#maxObject



118
119
120
121
122
123
124
# File 'app/models/headmin/form/media_view.rb', line 118

def max
  if attached.is_a?(ActiveStorage::Attached::Many)
    @max
  else
    1
  end
end

#minObject



110
111
112
113
114
115
116
# File 'app/models/headmin/form/media_view.rb', line 110

def min
  if @required
    (@min.to_i < 1) ? 1 : @min.to_i
  else
    (@min.to_i < 1) ? 0 : @min.to_i
  end
end

#nameObject



136
137
138
# File 'app/models/headmin/form/media_view.rb', line 136

def name
  "#{attribute}_#{object_id}"
end

#nested_attributeObject



102
103
104
105
106
107
108
# File 'app/models/headmin/form/media_view.rb', line 102

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

#requiredObject



128
129
130
# File 'app/models/headmin/form/media_view.rb', line 128

def required
  @required ? 1 : nil
end

#sortObject



140
141
142
# File 'app/models/headmin/form/media_view.rb', line 140

def sort
  @sort == true
end

#thumbnail_optionsObject



54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/headmin/form/media_view.rb', line 54

def thumbnail_options
  options = {
    icon: "plus"
  }

  # Don't pass width or height if it was not defined
  options = options.merge(width: width) unless is_defined?(:width)
  options = options.merge(height: height) unless is_defined?(:height)

  options
end

#wrapper_optionsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/headmin/form/media_view.rb', line 17

def wrapper_options
  default_wrapper_options.deep_merge({
    class: ["mb-3", ("form-floating" if float)],
    data: {
      controller: "media",
      name: name,
      min: min,
      max: max,
      sort: sort,
      accept: accept,
      required: required.nil? ? 0 : required
    }
  }).deep_merge(@wrapper || {})
end