Sha256: 95437b390c8ac578ecd46e1c09d8bc2a1cc7e880676e12b72d9c15c1c511fdf3

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

# This tag allows for linking of js and css content defined on the layout.
# Looks something like this:
#   {{cms:asset layout_identifier, type: css, as: tag}}
#
# `type` - css | js - what we're outputting here
# `as`   - url (default) | tag - output url or wrap it in the appropriate tag
#
class ComfortableMediaSurfer::Content::Tags::Asset < ComfortableMediaSurfer::Content::Tag
  attr_reader :identifier, :type, :as

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

    options = params.extract_options!
    @identifier = params[0]
    @type       = options['type']
    @as         = options['as'] || 'url'

    return if @identifier.present?

    raise Error, 'Missing layout identifier for asset tag'
  end

  def layout
    @layout ||= context.site.layouts.find_by(identifier: @identifier)
  end

  def content
    return '' unless layout

    base = ComfortableMediaSurfer.config.public_cms_path || ''
    unless base.ends_with?('/')
      base += '/'
    end

    case @type
    when 'css'
      out = "#{base}cms-css/#{context.site.id}/#{@identifier}/#{layout.cache_buster}.css"
      if @as == 'tag'
        out = "<link href='#{out}' media='screen' rel='stylesheet' type='text/css' />"
      end
      out
    when 'js'
      out = "#{base}cms-js/#{context.site.id}/#{@identifier}/#{layout.cache_buster}.js"
      if @as == 'tag'
        out = "<script src='#{out}' type='text/javascript'></script>"
      end
      out
    end
  end
end

ComfortableMediaSurfer::Content::Renderer.register_tag(
  :asset, ComfortableMediaSurfer::Content::Tags::Asset
)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comfortable_media_surfer-3.0.0 lib/comfortable_media_surfer/content/tags/asset.rb