Class: Occams::Content::Tags::FileLink

Inherits:
Occams::Content::Tag show all
Includes:
Mixins::FileContent
Defined in:
lib/occams/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 `class` - any html classes that you want on the result link or image tag. For example “class1 class2”

  • variant_attrs are not functional, perhaps due to some change in ImageMagick

  • Simply use a class in your CSS / SASS to style your image display

‘label` - attach label attribute to link or image tag `resize` - imagemagick option. For example: “100x50>” `gravity` - imagemagick option. For example: “center” `crop` - imagemagick option. For example: “100x50+0+0”

Instance Attribute Summary collapse

Attributes inherited from Occams::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods included from Mixins::FileContent

#content

Methods inherited from Occams::Content::Tag

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

Constructor Details

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

Returns a new instance of FileLink.

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/occams/content/tags/file_link.rb', line 24

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') # broken for ImageMagick

  return if @identifier.present?

  raise Error, 'Missing identifier for file link tag'
end

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



22
23
24
# File 'lib/occams/content/tags/file_link.rb', line 22

def as
  @as
end

#identifierObject (readonly)

Returns the value of attribute identifier.



22
23
24
# File 'lib/occams/content/tags/file_link.rb', line 22

def identifier
  @identifier
end

#variant_attrsObject (readonly)

Returns the value of attribute variant_attrs.



22
23
24
# File 'lib/occams/content/tags/file_link.rb', line 22

def variant_attrs
  @variant_attrs
end

Instance Method Details

#fileActiveStorage::Blob

Returns:

  • (ActiveStorage::Blob)


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

def file
  file_record&.attachment
end

#file_recordOccams::Cms::File

Returns:



39
40
41
# File 'lib/occams/content/tags/file_link.rb', line 39

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

#labelString

Returns:

  • (String)


49
50
51
52
53
# File 'lib/occams/content/tags/file_link.rb', line 49

def label
  return '' if file_record.nil?

  file_record.label.presence || file.filename.to_s
end