Module: SinatraMore::AssetTagHelpers

Defined in:
lib/sinatra_more/markup_plugin/asset_tag_helpers.rb

Instance Method Summary collapse

Instance Method Details

#flash_tag(kind, options = {}) ⇒ Object

Creates a div to display the flash of given type if it exists flash_tag(:notice, :class => ‘flash’, :id => ‘flash-notice’)



6
7
8
9
10
11
# File 'lib/sinatra_more/markup_plugin/asset_tag_helpers.rb', line 6

def flash_tag(kind, options={})
  flash_text = flash[kind]
  return '' if flash_text.blank?
  options.reverse_merge!(:class => 'flash')
  (:div, flash_text, options)
end

#image_path(src) ⇒ Object

Returns the path to the image, either relative or absolute



70
71
72
73
# File 'lib/sinatra_more/markup_plugin/asset_tag_helpers.rb', line 70

def image_path(src)
  src.gsub!(/\s/, '')
  src =~ %r{^\s*(/|http)} ? src : File.join('/images', src)
end

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

Creates an image element with given url and options image_tag(‘icons/avatar.png’)



51
52
53
54
# File 'lib/sinatra_more/markup_plugin/asset_tag_helpers.rb', line 51

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

#javascript_include_tag(*sources) ⇒ Object

javascript_include_tag ‘application’, ‘special’



64
65
66
67
# File 'lib/sinatra_more/markup_plugin/asset_tag_helpers.rb', line 64

def javascript_include_tag(*sources)
  options = sources.extract_options!.symbolize_keys
  sources.collect { |script| javascript_tag(script, options) }.join("\n")
end

Creates a link element with given name, url and options link_to ‘click me’, ‘/dashboard’, :class => ‘linky’ link_to(‘/dashboard’, :class => ‘blocky’) do … end parameters: name, url=‘javascript:void(0)’, options={}, &block



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sinatra_more/markup_plugin/asset_tag_helpers.rb', line 17

def link_to(*args, &block)
  if block_given?
    url, options = (args[0] || 'javascript:void(0);'), (args[1] || {})
    options.reverse_merge!(:href => url)
    link_content = capture_html(&block)
    result_link = (:a, link_content, options)
    block_is_template?(block) ? concat_content(result_link) : result_link
  else
    name, url, options = args.first, (args[1] || 'javascript:void(0);'), (args[2] || {})
    options.reverse_merge!(:href => url)
    (:a, name, options)
  end
end

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

Creates a mail link element with given name and caption mail_to “[email protected]” => <a href=“[email protected]”>[email protected]</a> mail_to “[email protected]”, “My Email” => <a href=“[email protected]”>My Email</a>



34
35
36
37
38
39
# File 'lib/sinatra_more/markup_plugin/asset_tag_helpers.rb', line 34

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

Creates a meta element with the content and given options meta_tag “weblog,news”, :name => “keywords” => <meta name=“keywords” content=“weblog,news”> meta_tag “text/html; charset=UTF-8”, :http-equiv => “Content-Type” => <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>



44
45
46
47
# File 'lib/sinatra_more/markup_plugin/asset_tag_helpers.rb', line 44

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

Returns a stylesheet link tag for the sources specified as arguments stylesheet_link_tag ‘style’, ‘application’, ‘layout’



58
59
60
61
# File 'lib/sinatra_more/markup_plugin/asset_tag_helpers.rb', line 58

def stylesheet_link_tag(*sources)
  options = sources.extract_options!.symbolize_keys
  sources.collect { |sheet| stylesheet_tag(sheet, options) }.join("\n")
end