Module: Merb::Assets::AssetHelpers

Included in:
AbstractAssetBundler, Merb::AssetsMixin
Defined in:
lib/merb-assets/assets.rb

Overview

Helpers for handling asset files.

Constant Summary collapse

ASSET_FILE_EXTENSIONS =
{
  :javascript => ".js",
  :stylesheet => ".css"
}

Instance Method Summary collapse

Instance Method Details

#asset_path(asset_type, filename, local_path = false) ⇒ Object

Returns the URI path to a particular asset file. If local_path is true, returns the path relative to the Merb.root, not the public directory. Uses the path_prefix, if any is configured.

Parameters

asset_type<Symbol>

Type of the asset (e.g. :javascript).

filename<~to_s>

The path to the file.

local_path<Boolean>

If true, the returned path will be relative to the Merb.root, otherwise it will be the public URI path. Defaults to false.

Returns

String

The path to the asset.

Examples

asset_path(:javascript, :dingo)
# => "/javascripts/dingo.js"

asset_path(:javascript, :dingo, true)
# => "public/javascripts/dingo.js"


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/merb-assets/assets.rb', line 42

def asset_path(asset_type, filename, local_path = false)
  filename = filename.to_s
  return filename if filename =~ %r{^https?://} #leave absolte paths alone

  # add extension if none given
  if filename !~ /#{'\\' + ASSET_FILE_EXTENSIONS[asset_type]}\Z/ && filename.index('?').nil?
    filename = "#{filename}#{ASSET_FILE_EXTENSIONS[asset_type]}" # don't modify receiver
  end

  # prepend asset type's folder path
  filename = "/#{asset_type}s/#{filename}" unless filename.index("/") == 0

  if local_path
    return "public#{filename}"
  else
    return "#{Merb::Config[:path_prefix]}#{filename}"
  end
end