Class: Occams::Content::Tags::PageFileLink

Inherits:
Occams::Content::Tag show all
Includes:
Mixins::FileContent
Defined in:
lib/occams/content/tags/page_file_link.rb

Overview

This tag allows you to link page-level files from within the page content.

E.g. if your layout has:

{{ cms:file graphic, render: false }}
{{ cms:files attachments, redner: false }}

You can link to the files from an individual page (or snippet rendered in the context of the page) like so:

{{ cms:page_file_link graphic }}
{{ cms:page_file_link attachments, filename: "cat.jpg" }}

‘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”

  • the following params are deprecated / 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) ⇒ PageFileLink

Returns a new instance of PageFileLink.

Parameters:

  • context (Occams::Cms::WithFragments)
  • params (Array<String, {String => String}>) (defaults to: [])
  • source (String, nil) (defaults to: nil)

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/occams/content/tags/page_file_link.rb', line 44

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')
  @filename       = options['filename']

  return if @identifier.present?

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

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



35
36
37
# File 'lib/occams/content/tags/page_file_link.rb', line 35

def as
  @as
end

#filenameString (readonly)

Returns Used to refer to a file in a cms:files } tag.

Returns:

  • (String)

    Used to refer to a file in a cms:files } tag.



41
42
43
# File 'lib/occams/content/tags/page_file_link.rb', line 41

def filename
  @filename
end

#identifierString (readonly)

Returns A ‘cms:file(s)` identifier.

Returns:

  • (String)

    A ‘cms:file(s)` identifier.



32
33
34
# File 'lib/occams/content/tags/page_file_link.rb', line 32

def identifier
  @identifier
end

#variant_attrsObject (readonly)

Returns the value of attribute variant_attrs.



38
39
40
# File 'lib/occams/content/tags/page_file_link.rb', line 38

def variant_attrs
  @variant_attrs
end

Instance Method Details

#fileActiveStorage::Blob

Returns:

  • (ActiveStorage::Blob)


65
66
67
68
69
70
71
72
73
74
# File 'lib/occams/content/tags/page_file_link.rb', line 65

def file
  @file ||=
    if fragment.nil?
      nil
    elsif filename.nil?
      fragment.attachments.first
    else
      fragment.attachments.detect { |a| a.filename.to_s == filename }
    end
end

#fragmentOccams::Cms::Fragment



60
61
62
# File 'lib/occams/content/tags/page_file_link.rb', line 60

def fragment
  @fragment ||= context.fragments.detect { |f| f.identifier == identifier }
end

#labelString

Returns:

  • (String)


77
78
79
80
81
# File 'lib/occams/content/tags/page_file_link.rb', line 77

def label
  return if file.nil?

  file.filename.to_s
end