Module: Padrino::Helpers::AssetTagHelpers

Defined in:
padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb

Overview

Helpers related to producing assets (images, stylesheets, js, etc) within templates.

Constant Summary

APPEND_ASSET_EXTENSIONS =
["js", "css"]
ABSOLUTE_URL_PATTERN =
%r{^(https?://)}
ASSET_FOLDERS =
{
  :js => 'javascripts',
  :css => 'stylesheets',
}

Instance Method Summary collapse

Instance Method Details

#asset_path(kind, source = nil) ⇒ String

Returns the path to the specified asset (css or javascript).

Examples:

# Generates: /javascripts/application.js?1269008689
asset_path :js, :application

# Generates: /stylesheets/application.css?1269008689
asset_path :css, :application

# Generates: /images/example.jpg?1269008689
asset_path :images, 'example.jpg'

# Generates: /uploads/file.ext?1269008689
asset_path 'uploads/file.ext'


315
316
317
318
319
320
321
322
323
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 315

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

#favicon_tag(source, options = {}) ⇒ String

Generates a favicon link. Looks inside images folder

Examples:

favicon_tag 'favicon.png'
favicon_tag 'icons/favicon.png'
# or override some options
favicon_tag 'favicon.png', :type => 'image/ico'


190
191
192
193
194
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 190

def favicon_tag(source, options={})
  type = File.extname(source).sub('.','')
  options = { :href => image_path(source), :rel => 'icon', :type => "image/#{type}" }.update(options)
  tag(:link, options)
end

#feed_tag(mime, url, options = {}) ⇒ String

Creates a link tag that browsers and news readers can use to auto-detect an RSS or ATOM feed.

@param options

The options for the feed tag.

Examples:

feed_tag :atom, url(:blog, :posts, :format => :atom), :title => "ATOM"
# Generates: <link type="application/atom+xml" rel="alternate" href="/blog/posts.atom" title="ATOM" />
feed_tag :rss, url(:blog, :posts, :format => :rss)
# Generates: <link type="application/rss+xml" rel="alternate" href="/blog/posts.rss" title="rss" />

Options Hash (options):

  • :rel (String) — default: "alternate"

    Specify the relation of this link.

  • :type (String)

    Override the auto-generated mime type.

  • :title (String)

    Specify the title of the link, defaults to the type.



117
118
119
120
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 117

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

#flash_tag(*args) ⇒ String

Creates a div to display the flash of given type if it exists.

Examples:

flash_tag(:notice, :id => 'flash-notice')
# Generates: <div class="notice" id="flash-notice">flash-notice</div>
flash_tag(:error, :success)
# Generates: <div class="error">flash-error</div>
# <div class="success">flash-success</div>


32
33
34
35
36
37
38
39
40
41
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 32

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 << (:button, '&times;'.html_safe, {:type => :button, :class => :close, :'data-dismiss' => :alert}) if bootstrap
    html << (:div, flash_text, { :class => kind }.update(options))
  end
end

#image_alt(src) ⇒ String

Returns a string suitable for an alt attribute of img element.



222
223
224
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 222

def image_alt(src)
  File.basename(src, '.*').sub(/-[[:xdigit:]]{32,64}\z/, '').tr('-_', ' ').capitalize
end

#image_path(src) ⇒ String

Returns the path to the image, either relative or absolute. We search it in your appname.public_folder like app/public/images for inclusion. You can provide also a full path.

Examples:

# Generates: /images/foo.jpg?1269008689
image_path("foo.jpg")


288
289
290
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 288

def image_path(src)
  asset_path(:images, src)
end

#image_tag(url, options = {}) ⇒ String

Creates an image element with given url and options.

Examples:

image_tag('icons/avatar.png')


209
210
211
212
213
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 209

def image_tag(url, options={})
  options = { :src => image_path(url) }.update(options)
  options[:alt] ||= image_alt(url) unless url.to_s =~ /\A(?:cid|data):|\A\Z/
  tag(:img, options)
end

#javascript_include_tag(*sources, options = {}) ⇒ String

Returns a html script tag for each of the sources provided. You can pass in the filename without extension or a symbol and we search it in your appname.public_folder like app/public/javascript for inclusion. You can provide also a full path.

Examples:

javascript_include_tag 'application', :extjs


265
266
267
268
269
270
271
272
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 265

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 << (:script, nil, { :src => asset_path(:js, source) }.update(options))
  end
end

Creates a link element with given name, url and options.

Note that you can pass :if or :unless conditions, but if you provide :current as condition padrino return true/false if the request.path_info match the given url.

Examples:

link_to('click me', '/dashboard', :class => 'linky')
# Generates <a class="linky" href="/dashboard">click me</a>

link_to('click me', '/dashboard', :remote => true)
# Generates <a href="/dashboard" data-remote="true">click me</a>

link_to('click me', '/dashboard', :method => :delete)
# Generates <a href="/dashboard" data-method="delete" rel="nofollow">click me</a>

link_to('click me', :class => 'blocky') do; end
# Generates <a class="blocky" href="#">click me</a>


84
85
86
87
88
89
90
91
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 84

def link_to(*args, &block)
  options  = args.extract_options!
  name = block_given? ? '' : args.shift
  href = args.first
  options = { :href => href || '#' }.update(options)
  return name unless parse_conditions(href, options)
  block_given? ? (:a, options, &block) : (:a, name, options)
end

#mail_to(email, caption = nil, mail_options = {}) ⇒ String

Creates a mail link element with given name and caption.

Examples:

mail_to "me@demo.com"
# Generates: <a href="mailto:me@demo.com">me@demo.com</a>

mail_to "me@demo.com", "My Email"
# Generates: <a href="mailto:me@demo.com">My Email</a>

Options Hash (mail_options):

  • cc (String)

    The cc recipients.

  • bcc (String)

    The bcc recipients.

  • subject (String)

    The subject line.

  • body (String)

    The email body.



145
146
147
148
149
150
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 145

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

#meta_tag(content, options = {}) ⇒ String

Creates a meta element with the content and given options.

Examples:

meta_tag "weblog,news", :name => "keywords"
# Generates: <meta name="keywords" content="weblog,news" />

meta_tag "text/html; charset=UTF-8", 'http-equiv' => "Content-Type"
# Generates: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />


169
170
171
172
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 169

def meta_tag(content, options={})
  options = { "content" => content }.update(options)
  tag(:meta, options)
end

Returns a html link tag for each of the sources provided. You can pass in the filename without extension or a symbol and we search it in your appname.public_folder like app/public/stylesheets for inclusion. You can provide also a full path.

Examples:

stylesheet_link_tag 'style', 'application', 'layout'


241
242
243
244
245
246
247
248
249
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 241

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