Sha256: 07802e00fc7a7c3ee8f898d8f5267bf8abc81f72bdda20d5f7e484316f48c855

Contents?: true

Size: 1.74 KB

Versions: 10

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require_relative 'mixins/file_content'

# 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"
#
class Occams::Content::Tag::FileLink < Occams::Content::Tag
  include Occams::Content::Tag::Mixins::FileContent

  # @return [String] A {Occams::Cms::Site#files} ID.
  attr_reader :identifier

  # @type ["url", "link", "image"]
  attr_reader :as

  # @type [{String => String}]
  attr_reader :variant_attrs

  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')

    return if @identifier.present?

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

  # @return [Occams::Cms::File]
  def file_record
    @file_record ||= context.site.files.detect { |f| f.id == identifier.to_i }
  end

  # @return [ActiveStorage::Blob]
  def file
    file_record&.attachment
  end

  # @return [String]
  def label
    return '' if file_record.nil?

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

Occams::Content::Renderer.register_tag(
  :file_link, Occams::Content::Tag::FileLink
)

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
occams-1.0.7.3 lib/occams/content/tags/file_link.rb
occams-1.0.7.2 lib/occams/content/tags/file_link.rb
occams-1.0.7.1 lib/occams/content/tags/file_link.rb
occams-1.0.7 lib/occams/content/tags/file_link.rb
occams-1.0.6.1 lib/occams/content/tags/file_link.rb
occams-1.0.6 lib/occams/content/tags/file_link.rb
occams-1.0.5 lib/occams/content/tags/file_link.rb
occams-1.0.4 lib/occams/content/tags/file_link.rb
occams-1.0.3 lib/occams/content/tags/file_link.rb
occams-1.0.2 lib/occams/content/tags/file_link.rb