Skip to content

Commit

Permalink
Merge pull request #4 from stbnrivas/master
Browse files Browse the repository at this point in the history
add new helper for favicon link tag
  • Loading branch information
Wlodek Bzyl committed Jun 16, 2012
2 parents de600ca + 7b6c089 commit 77ffdd1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Gem *sinatra-static-assets* implements the following helpers methods:
* `stylesheet_link_tag`
* `javascript_script_tag`
* `link_to`
* `link_favicon_tag`

To install it, run:

Expand Down
13 changes: 13 additions & 0 deletions lib/sinatra/static_assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ def link_to(desc, url, options = {})
desc
end
end

def link_favicon_tag(source = nil, options = {})
source = "favicon.ico" if source.nil? or source.empty?
unless settings.xhtml
# html5 style like <link rel="icon" href="http://example.com/myicon.ico" />
options[:rel] = options[:rel] || "icon"
else
# xhtml style like <link rel="shortcut icon" href="http://example.com/myicon.ico" />
options[:rel] = "shortcut icon"
end
options[:href] = url_for(source)
tag("link", options)
end

private

Expand Down
21 changes: 21 additions & 0 deletions test/sinatra_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,24 @@
#{link_to "Tatry Mountains Rescue Team", "/topr"}
EOD
end

get "/link_favicon_tag_empty" do
content_type "text/plain"
<<"EOD"
#{link_favicon_tag}
EOD
end

get "/link_favicon_tag_changing_filename" do
content_type "text/plain"
<<"EOD"
#{link_favicon_tag 'favicon2.ico'}
EOD
end

get "/link_favicon_tag_with_options" do
content_type "text/plain"
<<"EOD"
#{link_favicon_tag 'favicon.ico', :rel => "apple-touch-icon" }
EOD
end
5 changes: 5 additions & 0 deletions test/sinatra_baseapp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ class XHTML < Sinatra::Base
content_type "text/plain"
"#{stylesheet_link_tag("/stylesheets/winter.css")}"
end

get '/link_favicon_tag' do
content_type "text/plain"
"#{link_favicon_tag("favicon.ico")}"
end
end
end
25 changes: 25 additions & 0 deletions test/sinatra_static_assets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,29 @@ def test_link_to_tag_returns_sub_uri
EOD
end


def test_link_tag_empty
get '/link_favicon_tag_empty', {}, 'SCRIPT_NAME' => '/bar'
assert last_response.ok?
assert_equal last_response.body, <<EOD
<link href="/bar/favicon.ico" rel="icon">
EOD
end

def test_link_favicon_changing_filename
get '/link_favicon_tag_changing_filename', {}, 'SCRIPT_NAME' => '/bar'
assert last_response.ok?
assert_equal last_response.body, <<EOD
<link href="/bar/favicon2.ico" rel="icon">
EOD
end

def test_link_favicon_tag_with_options
get '/link_favicon_tag_with_options', {}, 'SCRIPT_NAME' => '/bar'
assert last_response.ok?
assert_equal last_response.body, <<EOD
<link href="/bar/favicon.ico" rel="apple-touch-icon">
EOD
end

end
7 changes: 7 additions & 0 deletions test/sinatra_static_assets_xhtml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ def test_link_tag_closes
"<link charset=\"utf-8\" href=\"/stylesheets/winter.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\"/>"
end

def test_link_favicon_tag_closes
get '/link_favicon_tag', {}, 'SCRIPT_NAME' => '/bar'
assert last_response.ok?
assert_equal last_response.body,
'<link href="/bar/favicon.ico" rel="shortcut icon"/>'
end

end

0 comments on commit 77ffdd1

Please sign in to comment.