Class: Practical::Views::TiptapDocumentComponent::Attachment

Inherits:
Node
  • Object
show all
Defined in:
app/components/practical/views/tiptap_document_component.rb

Instance Attribute Summary

Attributes inherited from Node

#node_content

Instance Method Summary collapse

Methods inherited from Node

#initialize, #render_node_contents

Methods included from NodeRendering

#render_node

Methods included from ElementHelper

#grab, #mix

Constructor Details

This class inherits a constructor from Practical::Views::TiptapDocumentComponent::Node

Instance Method Details

#attachmentObject



249
250
251
# File 'app/components/practical/views/tiptap_document_component.rb', line 249

def attachment
  @attachment ||= GlobalID::Locator.locate_signed(sgid.to_s, for: :document)&.attachment
end

#attachment_details_and_downloadObject



239
240
241
242
243
244
245
246
247
# File 'app/components/practical/views/tiptap_document_component.rb', line 239

def attachment_details_and_download
  tag.section(class: 'attachment-details') {
    tag.p {
      tag.a(href: url, target: "_blank") {
        "#{filename}#{human_file_size}"
      }
    }
  }
end

#attachment_figureObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'app/components/practical/views/tiptap_document_component.rb', line 215

def attachment_figure
  if previewable?
    image = tag.div {
      tag.img(src: url, width: width, height: height)
    }
  else
    image = tag.div {
      render Practical::Views::IconForFileExtensionComponent.new(extension: extension)
    }
  end

  safe_join([
    image,
    attachment_details_and_download,
    figure_caption
  ])
end

#attrsObject



257
258
259
# File 'app/components/practical/views/tiptap_document_component.rb', line 257

def attrs
  node_content[:attrs]
end

#callObject



191
192
193
194
195
196
197
198
199
# File 'app/components/practical/views/tiptap_document_component.rb', line 191

def call
  tag.figure(class: 'wa-stack wa-gap-s') {
    if missing_attachment?
      missing_attachment_figure
    else
      attachment_figure
    end
  }
end

#default_figure_sizeObject



307
308
309
# File 'app/components/practical/views/tiptap_document_component.rb', line 307

def default_figure_size
  100
end

#extensionObject



281
282
283
# File 'app/components/practical/views/tiptap_document_component.rb', line 281

def extension
  attachment.extension
end

#figure_captionObject



233
234
235
236
237
# File 'app/components/practical/views/tiptap_document_component.rb', line 233

def figure_caption
  if node_content[:content].present?
    tag.figcaption { render_node_contents }
  end
end

#filenameObject



269
270
271
# File 'app/components/practical/views/tiptap_document_component.rb', line 269

def filename
  attachment.original_filename
end

#has_dimensions?Boolean

Returns:

  • (Boolean)


293
294
295
# File 'app/components/practical/views/tiptap_document_component.rb', line 293

def has_dimensions?
  !stored_width.blank? && !stored_height.blank?
end

#heightObject



302
303
304
305
# File 'app/components/practical/views/tiptap_document_component.rb', line 302

def height
  return stored_height if has_dimensions?
  default_figure_size
end

#human_file_sizeObject



273
274
275
# File 'app/components/practical/views/tiptap_document_component.rb', line 273

def human_file_size
  helpers.number_to_human_size(attachment.size)
end

#missing_attachment?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'app/components/practical/views/tiptap_document_component.rb', line 253

def missing_attachment?
  attachment.nil?
end

#missing_attachment_figureObject



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/components/practical/views/tiptap_document_component.rb', line 201

def missing_attachment_figure
  safe_join([
    tag.div {
      render Practical::Views::IconForFileExtensionComponent.new(extension: "missing")
    },

    tag.section(class: 'attachment-details') {
      tag.p(t("tiptap_document.attachment_missing.text"))
    },

    figure_caption
  ])
end

#previewable?Boolean

Returns:

  • (Boolean)


261
262
263
# File 'app/components/practical/views/tiptap_document_component.rb', line 261

def previewable?
  attrs.dig(:previewable)
end

#sgidObject



265
266
267
# File 'app/components/practical/views/tiptap_document_component.rb', line 265

def sgid
  attrs.dig(:sgid)
end

#stored_heightObject



289
290
291
# File 'app/components/practical/views/tiptap_document_component.rb', line 289

def stored_height
  attrs.dig(:height)
end

#stored_widthObject



285
286
287
# File 'app/components/practical/views/tiptap_document_component.rb', line 285

def stored_width
  attrs.dig(:width)
end

#urlObject



277
278
279
# File 'app/components/practical/views/tiptap_document_component.rb', line 277

def url
  attachment.url
end

#widthObject



297
298
299
300
# File 'app/components/practical/views/tiptap_document_component.rb', line 297

def width
  return stored_width if has_dimensions?
  default_figure_size
end