module Padrino::Helpers::AssetTagHelpers
def asset_path(kind, source)
- Api: - semipublic
Returns:
-
(String)
- Path for the asset given the +kind+ and +source+.
Parameters:
-
source
(String
) -- -
kind
(String
) --
def asset_path(kind, source) return source if source =~ /^http/ asset_folder = case kind when :css then 'stylesheets' when :js then 'javascripts' else kind.to_s end source = source.to_s.gsub(/\s/, '%20') ignore_extension = (asset_folder.to_s == kind.to_s) # don't append extension source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/ result_path = source if source =~ %r{^/} # absolute path result_path ||= uri_root_path(asset_folder, source) timestamp = asset_timestamp(result_path) "#{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 = Padrino.root("public", file_path) if Padrino.respond_to?(:root) stamp = Time.now.to_i unless public_path && File.exist?(public_path) stamp ||= File.mtime(public_path).to_i "?#{stamp}" end
def button_to(*args, &block)
- Api: - public
Returns:
-
(String)
- Form and button html with specified +options+.
Options Hash:
(**options)
-
:method
(Symbol
) -- -
:remote
(String
) -- -
:multipart
(Boolean
) --
Parameters:
-
block
(Proc
) -- The button 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:
-
button_to(name, options={}, &block)
-
button_to(name, url, options={})
def button_to(*args, &block) name, url = args[0], args[1] options = args.extract_options! desired_method = options[:method] options.delete(:method) if options[:method].to_s !~ /get|post/i options.reverse_merge!(:method => 'post', :action => url) options[:enctype] = "multipart/form-data" if options.delete(:multipart) options["data-remote"] = "true" if options.delete(:remote) inner_form_html = hidden_form_method_field(desired_method) inner_form_html += block_given? ? capture_html(&block) : submit_tag(name) content_tag('form', inner_form_html, options) end
def favicon_tag(source, options={})
- 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={})
- 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' content_tag(:link, options.reverse_merge(:rel => 'alternate', :type => full_mime, :title => mime, :href => url)) end
def flash_tag(kind, options={})
- Api: - public
Returns:
-
(String)
- Flash tag html with specified +options+.
Parameters:
-
options
(Hash
) -- -
kind
(Symbol
) --
def flash_tag(kind, options={}) flash_text = flash[kind] return '' if flash_text.blank? options.reverse_merge!(:class => kind) content_tag(:div, flash_text, options) end
def image_path(src)
- 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={})
- 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)
- 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', :content => "") sources.flatten.map { |source| tag(:script, options.reverse_merge(:src => asset_path(:js, source))) }.join("\n") end
def link_to(*args, &block)
- Api: - public
Returns:
-
(String)
- Link tag html with specified +options+.
Options Hash:
(**options)
-
:method
(Symbol
) -- -
:confirm
(String
) -- -
:remote
(Boolean
) -- -
:unless
(Boolean
) -- -
:if
(Boolean
) -- -
: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! options = parse_js_attributes(options) # parses remote, method and confirm options anchor = "##{CGI.escape options.delete(:anchor).to_s}" if options[:anchor] if block_given? url = args[0] ? args[0] + anchor.to_s : anchor || 'javascript:void(0);' 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 name, url = args[0], (args[1] ? args[1] + anchor.to_s : anchor || 'javascript:void(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={})
- 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', '@') 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={})
- 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 parse_js_attributes(options)
=> { "data-remote" => true, "data-method" => "delete", "data-confirm" => "test" }
parse_js_attributes(:remote => true, :confirm => "test", :method => :delete)
Not destructive on options; returns updated options
Parses link_to options for given js declarations (remote, method, confirm)
#
def parse_js_attributes(options) options = options.dup options["data-remote"] = "true" if options.delete(:remote) if link_confirm = options.delete(:confirm) options["data-confirm"] = link_confirm end if link_method = options.delete(:method) options["data-method"] = link_method options["rel"] = "nofollow" end options end
def stylesheet_link_tag(*sources)
- 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") end
def uri_root_path(*paths)
uri_root_path("/some/path") => "/base/some/path"
@example
Returns the uri root of the application.
#
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