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.

Instance Method Summary (collapse)

Instance Method Details

- (String) asset_path(kind, source)

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'

Parameters:

  • kind (String)

    The kind of asset (i.e :images, :js, :css)

  • source (String)

    The path to the asset (relative or absolute).

Returns:

  • (String)

    Path for the asset given the kind and source.



343
344
345
346
347
348
349
350
351
352
353
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 343

def asset_path(kind, source)
  return source if source =~ /^http/
  is_absolute  = source =~ %r{^/}
  asset_folder = asset_folder_name(kind)
  source = source.to_s.gsub(/\s/, '%20')
  ignore_extension = (asset_folder.to_s == kind.to_s) # don't append an extension
  source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
  result_path = is_absolute ? source : uri_root_path(asset_folder, source)
  timestamp = asset_timestamp(result_path, is_absolute)
  "#{result_path}#{timestamp}"
end

- (String) button_to(name, url, options = {}) - (String) button_to(name, options = {}, &block)

Creates a form containing a single button that submits to the url.

Examples:

button_to 'Delete', url(:accounts_destroy, :id => ), :method => :delete, :class => :form
# Generates:
# <form class="form" action="/admin/accounts/destroy/2" method="post">
#   <input type="hidden" value="delete" name="_method" />
#   <input type="submit" value="Delete" />
# </form>

Overloads:

  • - (String) button_to(name, url, options = {})

    Parameters:

    • caption (String)

      The text caption.

    • url (String)

      The url href.

    • options (Hash)

      The html options.

  • - (String) button_to(name, options = {}, &block)

    Parameters:

    • url (String)

      The url href.

    • options (Hash)

      The html options.

    • block (Proc)

      The button content.

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • (String)

    Form and button html with specified options.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 120

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)
  ('form', inner_form_html, options)
end

- (String) favicon_tag(source, options = {})

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'

Parameters:

  • source (String)

    The source image path for the favicon link tag.

  • options (Hash) (defaults to: {})

    The html options for the favicon link tag.

Returns:

  • (String)

    The favicon link html tag with specified options.



233
234
235
236
237
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 233

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

- (String) feed_tag(mime, url, options = {})

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" />

Parameters:

  • mime (Symbol)

    The mime type of the feed (i.e :atom or :rss).

  • url (String)

    The url for the feed tag to reference.

  • options (Hash) (defaults to: {})

    a customizable set of options

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

Returns:

  • (String)

    Feed link html tag with specified options.



158
159
160
161
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 158

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

- (String) flash_tag(*args)

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

Examples:

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

Parameters:

  • kind (Symbol)

    The type of flash to display in the tag.

  • options (Hash)

    The html options for this section.

Returns:

  • (String)

    Flash tag html with specified options.



25
26
27
28
29
30
31
32
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 25

def flash_tag(*args)
  options = args.extract_options!
  args.map do |kind|
    flash_text = flash[kind]
    next if flash_text.blank?
    (:div, flash_text, options.reverse_merge(:class => kind))
  end.compact * "\n"
end

- (String) image_path(src)

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")

Parameters:

  • src (String)

    The path to the image file (relative or absolute)

Returns:

  • (String)

    Path to an image given the kind and source.



318
319
320
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 318

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

- (String) image_tag(url, options = {})

Creates an image element with given url and options

Examples:

image_tag('icons/avatar.png')

Parameters:

  • url (String)

    The source path for the image tag.

  • options (Hash) (defaults to: {})

    The html options for the image tag.

Returns:

  • (String)

    Image html tag with url and specified options.



253
254
255
256
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 253

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

- (String) javascript_include_tag(*sources, options = {})

Returns an 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

Parameters:

  • sources (Array<String>)

    Splat of js source paths

  • options (Hash)

    The html options for the script tag

Returns:

  • (String)

    Script tag for sources with specified options.



296
297
298
299
300
301
302
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 296

def javascript_include_tag(*sources)
  options = sources.extract_options!.symbolize_keys
  options.reverse_merge!(:type => 'text/javascript')
  sources.flatten.map { |source|
    (:script, nil, options.reverse_merge(:src => asset_path(:js, source)))
  }.join("\n")
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')
link_to('click me', '/dashboard', :remote => true)
link_to('click me', '/dashboard', :method => :delete)
link_to('click me', :class => 'blocky') do; end

Overloads:

  • - (String) link_to(caption, url, options = {})

    Parameters:

    • caption (String)

      The text caption.

    • url (String)

      The url href.

    • options (Hash)

      The html options.

  • - (String) link_to(url, options = {}, &block)

    Parameters:

    • url (String)

      The url href.

    • options (Hash)

      The html options.

    • block (Proc)

      The link content.

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • (String)

    Link tag html with specified options.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 71

def link_to(*args, &block)
  options = args.extract_options!
  anchor  = "##{CGI.escape options.delete(:anchor).to_s}" if options[:anchor]

  if block_given?
    url = args[0] ? args[0] + anchor.to_s : anchor || '#'
    options.reverse_merge!(:href => url)
    link_content = capture_html(&block)
    return '' unless parse_conditions(url, options)
    result_link = (: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 || '#')
    return name unless parse_conditions(url, options)
    options.reverse_merge!(:href => url)
    (:a, name, options)
  end
end

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

Creates a mail link element with given name and caption.

Examples:

# Generates: <a href="mailto:[email protected]">[email protected]</a>
mail_to "[email protected]"
# Generates: <a href="mailto:[email protected]">My Email</a>
mail_to "[email protected]", "My Email"

Parameters:

  • email (String)

    The email address for the link.

  • caption (String) (defaults to: nil)

    The caption for the link.

  • mail_options (Hash) (defaults to: {})

    The options for the mail link. Accepts html options.

Options Hash (mail_options):

  • cc (String)

    The cc recipients.

  • bcc (String)

    The bcc recipients.

  • subject (String)

    The subject line.

  • body (String)

    The email body.

Returns:

  • (String)

    Mail link html tag with specified options.



186
187
188
189
190
191
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 186

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

- (String) meta_tag(content, options = {})

Creates a meta element with the content and given options.

Examples:

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

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

Parameters:

  • content (String)

    The content for the meta tag.

  • options (Hash) (defaults to: {})

    The html options for the meta tag.

Returns:

  • (String)

    Meta html tag with specified options.



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

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

Returns an 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/stylesheets for inclusion. You can provide also a full path.

Examples:

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

Parameters:

  • sources (Array<String>)

    Splat of css source paths

  • options (Hash)

    The html options for the link tag

Returns:

  • (String)

    Stylesheet link html tag for sources with specified options.



273
274
275
276
277
278
279
# File 'padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb', line 273

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