Sha256: 3775b5d7cc4e140715242ccfaf6568e627124adfa0162e87466023fb08588d3a

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Asset
  module Helpers

    # Asset URL
    def asset_url(path)
      ::Asset.manifest.find{|i| i.path == path}.src rescue path
    end

    # Script tags
    def script_tag(*paths)
      tag('js', *paths) do |src|
        %{<script src="#{src}"></script>}
      end
    end

    # Style tags
    def style_tag(*paths)
      tag('css', *paths) do |src|
        %{<link href="#{src}" media="all" rel="stylesheet" type="text/css">}
      end
    end

    # Image tags
    def image_tag(path)
      b = ::Asset.images[path] rescue nil
      # Just slip through if the path starts with http(s) or //
      src = path =~ /^(http[s]?)?:?\/\// ? path : %{/assets/images/#{path}}
      %{<img src="#{src}#{b ? "?#{b}" : ''}">}
    end

    private

    # Build the tags
    def tag(type, *paths, &block)
      paths.map do |path|
        # Yield the source back to the tag builder
        item = ::Asset.manifest.find{|i| i.path == path}

        # Src is same as path if item not found
        item ? item.sources.map{|f| yield(asset_url(f))} : yield(path)
      end.flatten.join("\n")
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asset-0.1.3 lib/assets/helpers.rb