Skip to content

Commit

Permalink
Make author decorators able to handle authors without websites
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Jul 7, 2012
1 parent 45c0df9 commit 4f59275
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
20 changes: 13 additions & 7 deletions app/decorators/author_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,23 @@ def absolute_avatar_url

# Creates a link to the user's website for their profile
def website_link
h.link_to(
absolute_website_url,
absolute_website_url,
:rel => 'website',
:class => 'url'
)
if absolute_website_url.present?
h.link_to(
absolute_website_url,
absolute_website_url,
:rel => 'website',
:class => 'url'
)
else
""
end
end

# adds http:// if it isn't there
def absolute_website_url
if author.website[0,7] == "http://" or author.website[0,8] == "https://"
if author.website.blank?
nil
elsif author.website[0,7] == "http://" or author.website[0,8] == "https://"
author.website
else
"http://#{author.website}"
Expand Down
29 changes: 27 additions & 2 deletions test/decorators/author_decorator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,37 @@
end

it 'returns link to authors website' do
assert_match 'http://example.com', @author.website_link
@author.website_link.must_match('http://example.com')
end

it 'returns link to authors website when website is without http prefix' do
@author.website = 'test.com'
assert_match 'http://test.com', @author.website_link
@author.website_link.must_match('http://test.com')
end

it "returns empty string if the author doesn't have a website" do
@author.website = nil
@author.website_link.must_equal("")
end
end

describe "#absolute_website_url" do
before do
@author = AuthorDecorator.decorate(Fabricate(:author))
end

it 'returns url to authors website' do
@author.absolute_website_url.must_equal('http://example.com')
end

it 'returns url to authors website when website is without http prefix' do
@author.website = 'test.com'
@author.absolute_website_url.must_equal('http://test.com')
end

it "returns nil if the author doesn't have a website" do
@author.website = nil
@author.absolute_website_url.must_equal(nil)
end
end
end

0 comments on commit 4f59275

Please sign in to comment.