Sha256: fe2cc7a0237e2d55705fe7e48a49bf6c1bb7cecd00bf100732cb1b65b97d7248

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

# 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 ComfortableMexicanSofa::Content::Tag::Asset < ComfortableMexicanSofa::Content::Tag

  attr_reader :identifier, :type, :as

  def initialize(context, params_string)
    super

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

    unless @identifier.present?
      raise Error, "Missing layout identifier for asset tag"
    end
  end

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

  def content
    return "" unless layout

    base = ComfortableMexicanSofa.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

ComfortableMexicanSofa::Content::Renderer.register_tag(
  :asset, ComfortableMexicanSofa::Content::Tag::Asset
)

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-2.0.8 lib/comfortable_mexican_sofa/content/tags/asset.rb
comfortable_mexican_sofa-2.0.7 lib/comfortable_mexican_sofa/content/tags/asset.rb
comfortable_mexican_sofa-2.0.6 lib/comfortable_mexican_sofa/content/tags/asset.rb
comfortable_mexican_sofa-2.0.5 lib/comfortable_mexican_sofa/content/tags/asset.rb
comfortable_mexican_sofa-2.0.4 lib/comfortable_mexican_sofa/content/tags/asset.rb
comfortable_mexican_sofa-2.0.3 lib/comfortable_mexican_sofa/content/tags/asset.rb