module Padrino::Helpers::AssetTagHelpers

def asset_folder_name(kind)


asset_folder_name(:abrakadabrah) => 'abrakadabrah'
asset_folder_name(:images) => 'images'
asset_folder_name(:js) => 'javascripts'
asset_folder_name(:css) => 'stylesheets'
@example

Configureable by setting kind_asset_folder.

Returns the asset folder given a kind.
##
def asset_folder_name(kind)
  if self.class.respond_to? "#{kind}_asset_folder"
    self.class.send "#{kind}_asset_folder"
  else
    (ASSET_FOLDERS[kind] || kind).to_s
  end
end

def asset_normalize_extension(kind, source)


asset_normalize_extension(:js, "/foo/bar/baz") => "/foo/bar/baz.js"
asset_normalize_extension(:images, "/foo/bar/baz.png") => "/foo/bar/baz.png"

@example

Normalizes the extension for a given asset.
#
def asset_normalize_extension(kind, source)
  ignore_extension = !APPEND_ASSET_EXTENSIONS.include?(kind.to_s)
  source << ".#{kind}" unless ignore_extension || source =~ /\.#{kind}/ || source =~ ABSOLUTE_URL_PATTERN
  source
end

def asset_path(kind, source = nil)

Returns:
  • (String) - Path for the asset given the +kind+ and +source+.

Parameters:
  • source (String) --
  • kind (String) --
def asset_path(kind, source = nil)
  kind, source = source, kind if source.nil?
  source = asset_normalize_extension(kind, URI.escape(source.to_s))
  return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\//
  source = File.join(asset_folder_name(kind), source)
  timestamp = asset_timestamp(source)
  result_path = uri_root_path(source)
  "#{result_path}#{timestamp}"
end

def asset_timestamp(file_path)


asset_timestamp("some/path/to/file.png") => "?154543678"
@example

Returns the timestamp mtime for an asset.
#
def asset_timestamp(file_path)
  return nil if file_path =~ /\?/ || (self.class.respond_to?(:asset_stamp) && !self.class.asset_stamp)
  public_path = self.class.public_folder if self.class.respond_to?(:public_folder)
  public_path ||= Padrino.root("public") if Padrino.respond_to?(:root)
  public_file_path = File.join(public_path, file_path) if public_path
  stamp = File.mtime(public_file_path).to_i if public_file_path && File.exist?(public_file_path)
  stamp ||= Time.now.to_i
  "?#{stamp}"
end

def favicon_tag(source, options={})

Returns:
  • (String) - The favicon link html tag with specified +options+.

Parameters:
  • options (Hash) --
  • source (String) --
def favicon_tag(source, options={})
  type = File.extname(source).gsub('.','')
  options = options.dup.reverse_merge!(:href => image_path(source), :rel => 'icon', :type => "image/#{type}")
  tag(:link, options)
end

def feed_tag(mime, url, options={})

Returns:
  • (String) - Feed link html tag with specified +options+.

Options Hash: (**options)
  • :title (String) --
  • :type (String) --
  • :rel (String) --

Parameters:
  • url (String) --
  • mime (Symbol) --
def feed_tag(mime, url, options={})
  full_mime = (mime == :atom) ? 'application/atom+xml' : 'application/rss+xml'
  tag(:link, options.reverse_merge(:rel => 'alternate', :type => full_mime, :title => mime, :href => url))
end

def flash_tag(*args)

Returns:
  • (String) - Flash tag html with specified +options+.

Parameters:
  • options (Hash) --
  • kind (Symbol) --
def flash_tag(*args)
  options = args.extract_options!
  bootstrap = options.delete(:bootstrap) if options[:bootstrap]
  args.inject(ActiveSupport::SafeBuffer.new) do |html,kind|
    flash_text = ActiveSupport::SafeBuffer.new << flash[kind]
    next html if flash_text.blank?
    flash_text << content_tag(:button, '&times;'.html_safe, {:type => :button, :class => :close, :'data-dismiss' => :alert}) if bootstrap
    html << content_tag(:div, flash_text, { :class => kind }.update(options))
  end
end

def image_path(src)

Other tags:
    Api: - public

Returns:
  • (String) - Path to an image given the +kind+ and +source+.

Parameters:
  • src (String) --
def image_path(src)
  asset_path(:images, src)
end

def image_tag(url, options={})

Returns:
  • (String) - Image html tag with +url+ and specified +options+.

Parameters:
  • options (Hash) --
  • url (String) --
def image_tag(url, options={})
  options.reverse_merge!(:src => image_path(url))
  tag(:img, options)
end

def javascript_include_tag(*sources)

Returns:
  • (String) - Script tag for +sources+ with specified +options+.

Parameters:
  • options (Hash) -- The html options for the script tag
  • sources (Array) -- Splat of js source paths

Overloads:
  • javascript_include_tag(*sources, options={})
def javascript_include_tag(*sources)
  options = {
    :type => 'text/javascript'
  }.update(sources.extract_options!.symbolize_keys)
  sources.flatten.inject(ActiveSupport::SafeBuffer.new) do |all,source|
    all << content_tag(:script, nil, { :src => asset_path(:js, source) }.update(options))
  end
end

def link_to(*args, &block)

Returns:
  • (String) - Link tag html with specified +options+.

Options Hash: (**options)
  • :method (Symbol) --
  • :confirm (String) --
  • :remote (Boolean) --
  • :unless (Boolean) --
  • :if (Boolean) --
  • :fragment (String) --
  • :anchor (String) --

Parameters:
  • block (Proc) -- The link content.
  • options (Hash) -- The html options.
  • url (String) -- The url href.
  • options (Hash) -- The html options.
  • url (String) -- The url href.
  • caption (String) -- The text caption.

Overloads:
  • link_to(url, options={}, &block)
  • link_to(caption, url, options={})
def link_to(*args, &block)
  options  = args.extract_options!
  fragment = options.delete(:anchor).to_s if options[:anchor]
  fragment = options.delete(:fragment).to_s if options[:fragment]
  name = block_given? ? '' : args.shift
  if url = args.first
    url << '#' << fragment if fragment
  else
    url = '#'
    url << fragment if fragment
  end
  options.reverse_merge!(:href => url)
  return name unless parse_conditions(url, options)
  block_given? ? content_tag(:a, options, &block) : content_tag(:a, name, options)
end

def mail_to(email, caption=nil, mail_options={})

Returns:
  • (String) - Mail link html tag with specified +options+.

Options Hash: (**mail_options)
  • body (String) -- The email body.
  • subject (String) -- The subject line.
  • bcc (String) -- The bcc recipients.
  • cc (String) -- The cc recipients.

Parameters:
  • mail_options (Hash) --
  • caption (String) --
  • email (String) --
def mail_to(email, caption=nil, mail_options={})
  html_options = mail_options.slice!(:cc, :bcc, :subject, :body)
  mail_query = Rack::Utils.build_query(mail_options).gsub(/\+/, '%20').gsub('%40', '@').gsub('&', '&amp;')
  mail_href = "mailto:#{email}"; mail_href << "?#{mail_query}" if mail_query.present?
  link_to((caption || email), mail_href, html_options)
end

def meta_tag(content, options={})

Returns:
  • (String) - Meta html tag with specified +options+.

Parameters:
  • options (Hash) --
  • content (String) --
def meta_tag(content, options={})
  options.reverse_merge!("content" => content)
  tag(:meta, options)
end

def parse_conditions(url, options)


parse_conditions("/some/url", :if => false) => true
@example

Parses link_to options for given correct conditions.
#
def parse_conditions(url, options)
  if options.has_key?(:if)
    condition = options.delete(:if)
    condition == :current ? url == request.path_info : condition
  elsif condition = options.delete(:unless)
    condition == :current ? url != request.path_info : !condition
  else
    true
  end
end

def stylesheet_link_tag(*sources)

Other tags:
    Api: - public.

Returns:
  • (String) - Stylesheet link html tag for +sources+ with specified +options+.

Parameters:
  • options (Hash) -- The html options for the link tag
  • sources (Array) -- Splat of css source paths

Overloads:
  • stylesheet_link_tag(*sources, options={})
def stylesheet_link_tag(*sources)
  options = {
    :rel => 'stylesheet',
    :type => 'text/css'
  }.update(sources.extract_options!.symbolize_keys)
  sources.flatten.inject(ActiveSupport::SafeBuffer.new) do |all,source|
    all << tag(:link, { :href => asset_path(:css, source) }.update(options))
  end
end

def uri_root_path(*paths)


uri_root_path("javascripts", "test.js") => "/uri/root/javascripts/test.js"
uri_root_path("/some/path") => "/root/some/path"
@example

Returns the URI root of the application with optional paths appended.
#
def uri_root_path(*paths)
  root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
  File.join(ENV['RACK_BASE_URI'].to_s, root_uri || '/', *paths)
end