Sha256: db724b766a115466c96741153e88143652fe2ce5c03d155cec71c6d0edc2d551

Contents?: true

Size: 1.9 KB

Versions: 16

Compression:

Stored size: 1.9 KB

Contents

require 'action_view'

module Landable
  module Liquid

    class AssetTag < ::Liquid::Tag
      attr_accessor :tag_name, :asset_name

      # we'll be calling up to methods in here
      include ActionView::Helpers::AssetTagHelper

      # included for asset digest support
      include Sprockets::Rails::Helper

      def initialize(tag_name, param, tokens)
        @tag_name = tag_name
        @asset_name = param.strip
        @tokens = tokens
      end

      def render(context)
        tag_method = tag_name.to_sym
        tag_method = :image_tag if tag_method == :img_tag

        # if this matches an application asset, use that
        if assets_environment[asset_name]
          send tag_method, asset_name

        # otherwise, find an asset of our own
        else
          asset = lookup_asset context, asset_name

          options = {}
          options[:alt] = asset.description if tag_method == :image_tag

          send tag_method, asset.public_url, options
        end
      end

      protected

      def lookup_asset(context, name)
        assets = context.registers.fetch(:assets) do
          raise ArgumentError.new("`assets' value was never registered with the template")
        end

        assets.fetch(name) do
          raise ArgumentError.new("No `#{name}' asset available in #{assets.inspect}")
        end
      end

      # stuff for sprockets
      delegate :assets_prefix, :digest_assets, to: 'ActionView::Base'

      # ActionView::Base.assets_environment will be nil if the pipeline is
      # disabled - use this instead
      def assets_environment
        Rails.application.assets
      end
    end

    class AssetAttributeTag < AssetTag
      def render(context)
        asset = lookup_asset context, asset_name
        attribute = tag_name.sub /^asset_/, ''

        if attribute == 'url'
          asset.public_url
        else
          asset.send attribute
        end
      end
    end

  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
landable-1.13.1 lib/landable/liquid/asset_tags.rb
landable-1.12.3 lib/landable/liquid/asset_tags.rb
landable-1.12.2 lib/landable/liquid/asset_tags.rb
landable-1.12.1 lib/landable/liquid/asset_tags.rb
landable-1.11.1 lib/landable/liquid/asset_tags.rb
landable-1.11.0 lib/landable/liquid/asset_tags.rb
landable-1.10.0.rc2 lib/landable/liquid/asset_tags.rb
landable-1.10.0.rc1 lib/landable/liquid/asset_tags.rb
landable-1.9.2 lib/landable/liquid/asset_tags.rb
landable-1.9.1 lib/landable/liquid/asset_tags.rb
landable-1.9.0 lib/landable/liquid/asset_tags.rb
landable-1.9.0.rc2 lib/landable/liquid/asset_tags.rb
landable-1.9.0.rc1 lib/landable/liquid/asset_tags.rb
landable-1.8.0 lib/landable/liquid/asset_tags.rb
landable-1.7.1.rc1 lib/landable/liquid/asset_tags.rb
landable-1.7.0 lib/landable/liquid/asset_tags.rb