Skip to content

Commit

Permalink
Review how to build author link
Browse files Browse the repository at this point in the history
Remove spec that test helper in view
Move method to author_helper
Improve readability
  • Loading branch information
Yannick Francois committed Sep 10, 2013
1 parent 4898c5f commit 68b28b3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 30 deletions.
10 changes: 0 additions & 10 deletions app/helpers/application_helper.rb
Expand Up @@ -80,16 +80,6 @@ def html(content, what = :all, deprecated = false)
content.html(what)
end

def author_link(article)
if this_blog.link_to_author and article.user and article.user.email.to_s.size>0
"<a href=\"mailto:#{h article.user.email}\">#{h article.user.name}</a>"
elsif article.user and article.user.name.to_s.size>0
h article.user.name
else
h article.author
end
end

def display_user_avatar(user_id)
user = User.find(user_id)

Expand Down
20 changes: 20 additions & 0 deletions app/helpers/authors_helper.rb
Expand Up @@ -15,4 +15,24 @@ def is_url?(str)
false
end
end

def author_link(article)
return h(article.author) if just_author?(article.user)
return h(article.user.name) if just_name?(article.user)
content_tag(:a, href: "mailto:#{h article.user.email}") { h(article.user.name) }
end

private

def just_author?(author)
author.name.blank? || author.nil?
end

def just_name?(author)
author.present? && !this_blog.link_to_author
end

def this_blog
@blog ||= Blog.default
end
end
62 changes: 49 additions & 13 deletions spec/helpers/author_helper_spec.rb
@@ -1,22 +1,58 @@
require 'spec_helper'

describe AuthorsHelper, 'display_profile_item' do
include AuthorsHelper
describe AuthorsHelper do

it 'should display the item as a list item if show_item is true' do
item = display_profile_item(item = 'my@jabber.org', item_desc = 'Jabber:')
item.should have_selector('li', :content => 'Jabber: my@jabber.org')
end
describe :display_profile_item do
it 'should display the item as a list item if show_item is true' do
item = display_profile_item(item = 'my@jabber.org', show_item = true, item_desc = 'Jabber:')
item.should have_selector('li', :content => 'Jabber: my@jabber.org')
end

it 'should NOT display the item as a list item if show_item is false' do
item = display_profile_item(item = 'my@jabber.org', show_item = false, item_desc = 'Jabber:')
item.should be_nil
end

it 'should NOT display the item as a list item if show_item is false' do
item = display_profile_item(item = '', item_desc = 'Jabber:')
item.should be_nil
it 'should display a link if the item is an url' do
item = display_profile_item(item = 'http://twitter.com/mytwitter', show_item = true, item_desc = 'Twitter:')
item.should have_selector('li') do
have_selector('a', :content => 'http://twitter.com/mytwitter')
end
end
end

it 'should display a link if the item is an url' do
item = display_profile_item(item = 'http://twitter.com/mytwitter', item_desc = 'Twitter:')
item.should have_selector('li') do
have_selector('a', :content => 'http://twitter.com/mytwitter')
describe :author_link do
context "with an article" do
let(:article) { build(:article, user: author) }

context "with an author with a name to this article" do
let(:author) { build(:user, name: "Henri") }

context "with a link_to_author set in blog" do
let!(:blog) { create(:blog, link_to_author: true) }
it { expect(author_link(article)).to have_selector('a', href: "mailto:#{author.email}", content: author.name) }
end

context "with a no link_to_author set in blog" do
let!(:blog) { create(:blog, link_to_author: false) }
it { expect(author_link(article)).to eq(author.name) }
end
end

context "with an author without a name to this article" do
let(:author) { build(:user, name: "") }
let(:article) { build(:article, user: author, author: "Emile") }

context "with a link_to_author set in blog" do
let!(:blog) { create(:blog, link_to_author: true) }
it { expect(author_link(article)).to eq(article.author) }
end

context "with a no link_to_author set in blog" do
let!(:blog) { create(:blog, link_to_author: false) }
it { expect(author_link(article)).to eq(article.author) }
end
end
end
end

Expand Down
7 changes: 0 additions & 7 deletions spec/views/articles/read_spec.rb
Expand Up @@ -51,13 +51,6 @@
rendered.should_not have_selector("p>p>em", :content => "italic")
end

it "should automatically add links" do
rendered.should have_selector("a", :href => "mailto:foo@bar.com",
:content => "foo@bar.com")
rendered.should have_selector("a", :href=>"http://www.bar.com",
:content => "http://www.bar.com")
end

it "should show the comment creation times in the comment list" do
rendered.should =~ /#{@c1.created_at.to_s}/
rendered.should =~ /#{@c2.created_at.to_s}/
Expand Down

0 comments on commit 68b28b3

Please sign in to comment.