module Padrino::Helpers::AssetTagHelpers

def asset_folder_name(kind)


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

Returns the asset folder given a kind.
##
def asset_folder_name(kind)
  case kind
  when :css then 'stylesheets'
  when :js  then 'javascripts'
  else 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)

Other tags:
    Api: - semipublic

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

Parameters:
  • source (String) --
  • kind (String) --
def asset_path(kind, source)
  source = asset_normalize_extension(kind, URI.escape(source.to_s))
  return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\// # absolute 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={})

Other tags:
    Api: - public

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={})

Other tags:
    Api: - public

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)

Other tags:
    Api: - public

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(''.html_safe) do |html,kind|
    flash_text = flash[kind]
    next html if flash_text.blank?
    flash_text << safe_content_tag(:button, "&times;", {:type => :button, :class => :close, :'data-dismiss' => :alert}) if bootstrap
    html << safe_content_tag(:div, flash_text, options.reverse_merge(:class => kind))
  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={})

Other tags:
    Api: - public

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)

Other tags:
    Api: - public

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 = sources.extract_options!.symbolize_keys
  options.reverse_merge!(:type => 'text/javascript')
  sources.flatten.map { |source|
    content_tag(:script, nil, options.reverse_merge(:src => asset_path(:js, source)))
  }.join("\n").html_safe
end

def link_to(*args, &block)

Other tags:
    Api: - public

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]
  url = ActiveSupport::SafeBuffer.new
  if block_given?
    if args[0]
      url.concat(args[0])
      url.concat(FRAGMENT_HASH).concat(fragment) if fragment
    else
      url.concat(FRAGMENT_HASH)
      url.concat(fragment) if fragment
    end
    options.reverse_merge!(:href => url)
    link_content = capture_html(&block)
    return '' unless parse_conditions(url, options)
    result_link = content_tag(:a, link_content, options)
    block_is_template?(block) ? concat_content(result_link) : result_link
  else
    if args[1]
      url.concat(args[1])
      url.safe_concat(FRAGMENT_HASH).concat(fragment) if fragment
    else
      url = FRAGMENT_HASH
      url.concat(fragment) if fragment
    end
    name = args[0]
    return name unless parse_conditions(url, options)
    options.reverse_merge!(:href => url)
    content_tag(:a, name, options)
  end
end

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

Other tags:
    Api: - public

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={})

Other tags:
    Api: - public

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 = sources.extract_options!.symbolize_keys
  options.reverse_merge!(:media => 'screen', :rel => 'stylesheet', :type => 'text/css')
  sources.flatten.map { |source|
    tag(:link, options.reverse_merge(:href => asset_path(:css, source)))
  }.join("\n").html_safe
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