Module: Sinatra::SinatraHelpers::Erb::Links

Defined in:
lib/useful/sinatra_helpers/erb/links.rb

Instance Method Summary collapse

Instance Method Details

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

helper to emulate ‘image_tag’ EX : image_tag ‘logo.jpg’

=> <img src="images/logo.jpg" />


36
37
38
39
# File 'lib/useful/sinatra_helpers/erb/links.rb', line 36

def image_tag(src,options={})
  options[:src] = ['/'].include?(src[0..0]) ? src : "/images/#{src}"
  tag(:img, options)
end

#javascript_include_tag(srcs, options = {}) ⇒ Object

helper to emulate ‘javascript_include_tag’ EX : javascript_include_tag ‘app’

=> <script src="/js/app.js" type="text/javascript" />

EX : javascript_include_tag [‘app’, ‘jquery’]

=> <script src="/js/app.js" type="text/javascript" />
=> <script src="/js/jquery.js" type="text/javascript" />


60
61
62
63
64
65
66
# File 'lib/useful/sinatra_helpers/erb/links.rb', line 60

def javascript_include_tag(srcs,options={})
  options[:type] ||= "text/javascript"
  srcs.to_a.collect do |src|
    options[:src] = "/javascripts/#{src}.js#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
    tag(:script, options) { '' }
  end.join("\n")
end

#javascript_tag(options = {}) ⇒ Object

helper to emulate ‘javascript_tag’



69
70
71
72
# File 'lib/useful/sinatra_helpers/erb/links.rb', line 69

def javascript_tag(options={})
  options[:type] ||= "text/javascript"
  tag(:script, options) { yield }
end

helper to emulate action view’s ‘link_to’ EX : link_to “google”, “google.com

> <a href=“google.com”>google</a>



12
13
14
15
# File 'lib/useful/sinatra_helpers/erb/links.rb', line 12

def link_to(content,href,options={})
  options.update :href => href
  tag(:a, options) { content }
end


26
27
28
29
30
31
# File 'lib/useful/sinatra_helpers/erb/links.rb', line 26

def link_to_function(content, function, opts={})
  opts ||= {}
  opts[:href] ||= 'javascript: void(0);'
  opts[:onclick] = "javascript: #{function}; return false;"
  link_to content, opts[:href], opts
end


22
23
24
# File 'lib/useful/sinatra_helpers/erb/links.rb', line 22

def mail_link_to(email, options={})
  link_to options[:label] || email, "mailto: #{email}"
end


17
18
19
20
# File 'lib/useful/sinatra_helpers/erb/links.rb', line 17

def open_link_to(content, href, options={})
  options[:onclick] = "javascript: window.open('#{href}'); return false;"
  link_to(content, href, options)
end

helper to emulate ‘stylesheet_link_tag’ EX : stylesheet_link_tag ‘default’

=> <link rel="stylesheet" href="/stylesheets/default.css" type="text/css" media="all" title="no title" charset="utf-8">


44
45
46
47
48
49
50
51
52
# File 'lib/useful/sinatra_helpers/erb/links.rb', line 44

def stylesheet_link_tag(srcs,options={})
  options[:media] ||=  "screen"
  options[:type] ||= "text/css"
  options[:rel] ||= "stylesheet"
  srcs.to_a.collect do |src|
    options[:href] = "/stylesheets/#{src}.css#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
    tag(:link, options)
  end.join("\n")
end