Module: ComfortableMexicanSofa::Content::Tag::Mixins::FileContent

Included in:
File, FileLink, PageFileLink
Defined in:
lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb

Instance Method Summary collapse

Instance Method Details

#content(file: self.file, as: self.as, variant_attrs: self.variant_attrs, label: self.label) ⇒ String

Parameters:

  • file (ActiveStorage::Blob) (defaults to: self.file)
  • as ("link", "image", "url") (defaults to: self.as)
  • variant_attrs ({String => String}) (defaults to: self.variant_attrs)

    ImageMagick variant attributes

  • label (String) (defaults to: self.label)

    alt text for `as: “image”`, link text for `as: “link”`

Returns:

  • (String)

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb', line 12

def content(file: self.file, as: self.as, variant_attrs: self.variant_attrs, label: self.label)
  return "" unless file

  url_helpers = Rails.application.routes.url_helpers

  attachment_url =
    if variant_attrs.present? && file.image?
      variant = file.variant(combine_options: variant_attrs)
      url_helpers.rails_representation_path(variant, only_path: true)
    else
      url_helpers.rails_blob_path(file, only_path: true)
    end

  case as
  when "link"
    "<a href='#{attachment_url}'#{html_class_attribute} target='_blank'>#{label}</a>"
  when "image"
    "<img src='#{attachment_url}'#{html_class_attribute} alt='#{label}'/>"
  else
    attachment_url
  end
end