Class: Formstrap::MediaView

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



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

def accept
  @accept
end

Instance Method Details

#association_objectObject



63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/formstrap/media_view.rb', line 63

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



87
88
89
# File 'app/models/formstrap/media_view.rb', line 87

def attached
  form.object.send(attribute)
end

#attachmentsObject



75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/formstrap/media_view.rb', line 75

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



146
147
148
# File 'app/models/formstrap/media_view.rb', line 146

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

#build_nested_attributeObject



91
92
93
94
95
96
97
# File 'app/models/formstrap/media_view.rb', line 91

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



41
42
43
44
45
46
47
48
49
# File 'app/models/formstrap/media_view.rb', line 41

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

#edit_modal_url(attachment) ⇒ Object



134
135
136
137
138
# File 'app/models/formstrap/media_view.rb', line 134

def edit_modal_url(attachment)
  return nil unless edit_url.present?
  return edit_url unless attachment&.persisted?
  edit_url.gsub(":id", attachment.blob&.id.to_s)
end

#input_group_optionsObject



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

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

#item_optionsObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/formstrap/media_view.rb', line 28

def item_options
  options = {
    sort: sort,
    url: modal_url
  }

  # 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



115
116
117
118
119
120
121
# File 'app/models/formstrap/media_view.rb', line 115

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

#minObject



107
108
109
110
111
112
113
# File 'app/models/formstrap/media_view.rb', line 107

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


123
124
125
126
127
128
129
130
131
132
# File 'app/models/formstrap/media_view.rb', line 123

def modal_url
  formstrap_media_url(
    name: name,
    ids: blob_ids,
    min: min,
    max: max,
    mimetype: accept,
    exclude_models: exclude_models
  )
end

#nameObject



150
151
152
# File 'app/models/formstrap/media_view.rb', line 150

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

#nested_attributeObject



99
100
101
102
103
104
105
# File 'app/models/formstrap/media_view.rb', line 99

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

#requiredObject



142
143
144
# File 'app/models/formstrap/media_view.rb', line 142

def required
  @required ? 1 : nil
end

#sortObject



154
155
156
# File 'app/models/formstrap/media_view.rb', line 154

def sort
  @sort == true
end

#thumbnail_optionsObject



51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/formstrap/media_view.rb', line 51

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



16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/formstrap/media_view.rb', line 16

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