diff --git a/app/decorators/author_decorator.rb b/app/decorators/author_decorator.rb index 73ccb09c..b12c5cef 100644 --- a/app/decorators/author_decorator.rb +++ b/app/decorators/author_decorator.rb @@ -5,22 +5,30 @@ class AuthorDecorator < ApplicationDecorator # to the user's page def avatar h.content_tag "div", :class => "avatar" do - h.link_to( + if author + h.link_to( + h.image_tag( + absolute_avatar_url, + :class => "photo user-image", + :alt => "avatar" + ), + author.url + ) + else h.image_tag( absolute_avatar_url, :class => "photo user-image", :alt => "avatar" - ), - author.url - ) + ) + end end end # Make sure we're using the asset path if the user's avatar is the default # (local) avatar def absolute_avatar_url - if author.avatar_url.eql? RstatUs::DEFAULT_AVATAR - h.asset_path(author.avatar_url) + if !author || author.avatar_url.eql?(RstatUs::DEFAULT_AVATAR) + h.asset_path(RstatUs::DEFAULT_AVATAR) else author.avatar_url end diff --git a/test/decorators/author_decorator_test.rb b/test/decorators/author_decorator_test.rb index 4c5a59a5..63819354 100644 --- a/test/decorators/author_decorator_test.rb +++ b/test/decorators/author_decorator_test.rb @@ -12,7 +12,7 @@ @author.website_link.must_match('http://example.com') end - it 'returns link to authors website when website is without http prefix' do + it 'returns link to authors website when website has no http prefix' do @author.website = 'test.com' @author.website_link.must_match('http://test.com') end @@ -42,4 +42,69 @@ @author.absolute_website_url.must_equal(nil) end end + + describe "#avatar" do + before do + @author = AuthorDecorator.decorate(Fabricate(:author)) + end + + it "has a link to the author's page around the author's image" do + @avatar_html = Nokogiri::HTML.parse(@author.avatar) + link = @avatar_html.at_xpath("//a") + + assert link + link["href"].must_equal(@author.url) + + image_tag = link.at_xpath("//img") + + assert image_tag + image_tag["src"].must_equal(@author.absolute_avatar_url) + end + + it "has no link and the default avatar if author is nil" do + nil_author = AuthorDecorator.decorate(nil) + @avatar_html = Nokogiri::HTML.parse(nil_author.avatar) + + link = @avatar_html.at_xpath("//a") + refute link + + image_tag = @avatar_html.at_xpath("//img") + + assert image_tag + image_tag["src"].must_equal(nil_author.absolute_avatar_url) + end + end + + describe "#absolute_avatar_url" do + before do + @fabricated_author = Fabricate(:author) + @author = AuthorDecorator.decorate(@fabricated_author) + end + + it "uses the author's avatar url if present" do + avatar_url = "http://example.com/avatar.png" + @fabricated_author.image_url = avatar_url + + @author.absolute_avatar_url.must_equal(avatar_url) + end + + it "creates a gravatar url if avatar url isnt present but email is" do + @author.absolute_avatar_url.must_match("gravatar.com") + end + + it "uses the rstat.us default avatar if avatar url isn't specified" do + @fabricated_author.email = nil + + @author.absolute_avatar_url.must_equal( + "/assets/#{RstatUs::DEFAULT_AVATAR}" + ) + end + + it "uses the rstat.us default avatar if author is nil" do + nil_author = AuthorDecorator.decorate(nil) + nil_author.absolute_avatar_url.must_equal( + "/assets/#{RstatUs::DEFAULT_AVATAR}" + ) + end + end end