Module: GeorgeDrummond::Sinatra::Helpers

Defined in:
lib/georgedrummond_sinatra_helpers.rb,
lib/georgedrummond_sinatra_helpers/version.rb

Constant Summary collapse

VERSION =
"0.0.8"

Instance Method Summary collapse

Instance Method Details

#gravatar_image(email, size = 50, opts = {}) ⇒ Object

Creates the gravatar (www.gravatar.com) html from the email address and arguments provided. If no size is specified then it defaults to 50x50px

Examples

With no size specified:

gravatar_image("[email protected]")
  # => <img src="http://www.gravatar.com/avatar/d278a12b969a495ab16fdd942e748fe5?s=50" class="gravatar" />

With a size specified:

gravatar_image("[email protected]", 150)
  # => <img src="http://www.gravatar.com/avatar/d278a12b969a495ab16fdd942e748fe5?s=150" class="gravatar" />

With options:

gravatar_image("[email protected]", 150, :class => :icon)
  # =>  <img src="http://www.gravatar.com/avatar/d278a12b969a495ab16fdd942e748fe5?s=150" class="icon" />


52
53
54
55
56
# File 'lib/georgedrummond_sinatra_helpers.rb', line 52

def gravatar_image(email, size=50, opts={})
  hash = Digest::MD5.hexdigest(email)
  url = "http://www.gravatar.com/avatar/#{hash}?s=#{size}"
  image_tag url, {:class => :gravatar}.merge(opts)
end

#image_tag(image, opts = {}) ⇒ Object

Creates a html image tag given a file name. File should be located in public/images.

Examples

With a local image:

image_tag "home.png"
  # => <img src="/images/home.png" />

With options:

image_tag "home.png", :title => "home", :class => :icon
  # => <img src="/images/home.png" title="home" class="icon" />

With a remote image:

image_tag "http://accountsapp.com/logo.png", :title => "Online Accounting Software"
  # => <img src="http://accountsapp.com/logo.png" title="Online Accounting Software" />


149
150
151
152
153
154
155
156
# File 'lib/georgedrummond_sinatra_helpers.rb', line 149

def image_tag(image, opts={})
  if is_uri?(image)
    path = image
  else
    path = url("/images/#{image}")
  end
  return "<img src=\"#{path}\"#{parse_options(opts)} />"
end

#javascript_include_tag(*sources) ⇒ Object

Creates html tags for javascript includes from the arguments provided. Dont include the .js extension as this will be automatically appended. You can specify one or multiple javascript files at once. Place your javascript files in the public/js folder of your app. URLs will link to external files

Examples

With only one javascript file

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

With an array of javascript files we want to show:

javascript_include_tag :jquery, :app # =>
  <script type="text/javascript" src="/js/jquery.js"></script>
  <script type="text/javascript" src="/js/app.js"></script>

With an external stylesheet:

javascript_include_tag :jquery, "http://accountsapp.com/js/app.js"
  <script type="text/javascript" src="/js/jquery.js"></script>
  <script type="text/javascript" src="http://accountsapp.com/js/app.js"></script>


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/georgedrummond_sinatra_helpers.rb', line 117

def javascript_include_tag(*sources)
  html = []
  sources.each do |javascript|
    if is_uri?(javascript)
      path = javascript
    else
      path = url("/js/#{javascript}.js")
    end
    html << "<script type=\"text/javascript\" src=\"#{path}\"></script>"
  end
  return html.join("\n")
end

Creates a html link tag given a page title and resource path.

Examples

With an internal link

link_to "Home", "/home"
  # => <a href="/home">Home</a>

With options:

link_to "Home", "/home", :title => "Home Page"
  # => <a href="/home" title="Home Page">Home</a>

For an external URL:

link_to "AccountsApp", "http://accountsapp.com", :title => "Online Invoicing Software"
  # => <a href="http://accountsapp.com" title="Online Invoicing Software">AccountsApp</a>


177
178
179
180
181
182
183
# File 'lib/georgedrummond_sinatra_helpers.rb', line 177

def link_to(title, path, opts={}, base=true)
  unless is_uri?(path) || base == false
    path = url(path)
  end
  
  return "<a href=\"#{path}\"#{parse_options(opts)}>#{title}</a>"
end

#mail_to(email, text = email, opts = {}) ⇒ Object

Creates the html for a mail_to link being given an email address and an optional text argument

Examples

With only an email address provided:

mail_to("[email protected]")
  # => <a href="mailto:[email protected]" class="mailto">[email protected]</a>

With an optional text field provided:

mail_to("[email protected]", "George Drummond")
  # => <a href="mailto:[email protected]" class="mailto">George Drummond</a>

With an options hash:

mail_to("[email protected]", "George Drummond", :class => :email, :title => "How do you do")
  # => <a href="mailto:[email protected]" class="email" title="How do you do">George Drummond</a>


27
28
29
30
# File 'lib/georgedrummond_sinatra_helpers.rb', line 27

def mail_to(email, text=email, opts={})
  options = {:class => :mailto}.merge(opts)
  link_to text, "mailto:#{email}", options, false
end

Creates html tags for stylesheets from the arguements provided. Dont include the .css extension as this will be automatically appended. You can specify one or multiple css files at once. Place your css files in the public/css folder of your app. URLs will link to external files

Examples

With only one CSS file:

stylesheet_link_tag :app
 # => <link href="/css/app.css" type="text/css" rel="stylesheet" />

With an array of CSS files we want to show:

stylesheet_link_tag :app, :header # =>
  <link href="/css/app.css" type="text/css" rel="stylesheet" />
  <link href="/css/header.css" type="text/css" rel="stylesheet" />

With an external stylesheet:

stylesheet_link_tag :app, "http://accountsapp.com/styles/common.css"
  <link href="/css/app.css" type="text/css" rel="stylesheet" />
  <link href="http://accountsapp.com/styles/common.css" type="text/css" rel="stylesheet" />


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/georgedrummond_sinatra_helpers.rb', line 81

def stylesheet_link_tag(*sources)
  html = []
  sources.each do |stylesheet|
    if is_uri?(stylesheet)
      path = stylesheet
    else
      path = url("/css/#{stylesheet}.css")
    end
    html << "<link href=\"#{path}\" type=\"text/css\" rel=\"stylesheet\" />"
  end
  return html.join("\n")
end