Class: ComfortableMexicanSofa::Content::Tag::FileLink

Inherits:
ComfortableMexicanSofa::Content::Tag show all
Includes:
Mixins::FileContent
Defined in:
lib/comfortable_mexican_sofa/content/tags/file_link.rb

Overview

This is how you link previously uploaded file to anywhere. Good example may be a header image you want to use on the layout level.

{{cms:file_link id, as: image}}

‘as` - url (default) | link | image - how file gets rendered out `label` - attach label attribute to link or image tag `resize` - imagemagic option. For example: “100x50>” `gravity` - imagemagic option. For example: “center” `crop` - imagemagic option. For example: “100x50+0+0” `class` - any html classes that you want on the result link or image tag. For example “class1 class2”

Instance Attribute Summary collapse

Attributes inherited from ComfortableMexicanSofa::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods included from Mixins::FileContent

#content

Methods inherited from ComfortableMexicanSofa::Content::Tag

#allow_erb?, #content, #nodes, #render

Constructor Details

#initialize(context:, params: [], source: nil) ⇒ FileLink

Returns a new instance of FileLink.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/comfortable_mexican_sofa/content/tags/file_link.rb', line 29

def initialize(context:, params: [], source: nil)
  super

  options = params.extract_options!
  @identifier     = params[0]
  @as             = options["as"] || "url"
  @class          = options["class"]
  @variant_attrs  = options.slice("resize", "gravity", "crop")

  unless @identifier.present?
    raise Error, "Missing identifier for file link tag"
  end
end

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



24
25
26
# File 'lib/comfortable_mexican_sofa/content/tags/file_link.rb', line 24

def as
  @as
end

#identifierString (readonly)

Returns A Comfy::Cms::Site#files ID.

Returns:

  • (String)

    A Comfy::Cms::Site#files ID.



21
22
23
# File 'lib/comfortable_mexican_sofa/content/tags/file_link.rb', line 21

def identifier
  @identifier
end

#variant_attrsObject (readonly)

Returns the value of attribute variant_attrs.



27
28
29
# File 'lib/comfortable_mexican_sofa/content/tags/file_link.rb', line 27

def variant_attrs
  @variant_attrs
end

Instance Method Details

#fileActiveStorage::Blob

Returns:

  • (ActiveStorage::Blob)


49
50
51
# File 'lib/comfortable_mexican_sofa/content/tags/file_link.rb', line 49

def file
  file_record&.attachment
end

#file_recordComfy::Cms::File

Returns:



44
45
46
# File 'lib/comfortable_mexican_sofa/content/tags/file_link.rb', line 44

def file_record
  @file_record ||= context.site.files.detect { |f| f.id == identifier.to_i }
end

#labelString

Returns:

  • (String)


54
55
56
57
# File 'lib/comfortable_mexican_sofa/content/tags/file_link.rb', line 54

def label
  return "" if file_record.nil?
  file_record.label.presence || file.filename.to_s
end