Skip to content

Commit

Permalink
Fixes hotsh#534, makes some AuthorDecorator methods work even if auth…
Browse files Browse the repository at this point in the history
…or is nil
  • Loading branch information
carols10cents committed Aug 10, 2012
1 parent 25f040f commit ab788d6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
20 changes: 14 additions & 6 deletions app/decorators/author_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 66 additions & 1 deletion test/decorators/author_decorator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit ab788d6

Please sign in to comment.