From 2508aeedee2ca744ddeb139d1b2e3436149404f8 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Fri, 3 Sep 2010 01:22:53 -0300 Subject: [PATCH] Prevent crash when looking for lead in article without paragraphs --- app/models/article.rb | 3 ++- test/unit/article_test.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/article.rb b/app/models/article.rb index fe11233..3f76782 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -342,7 +342,8 @@ class Article < ActiveRecord::Base end def first_paragraph - Hpricot(to_html).search('p').first.to_html + paragraphs = Hpricot(to_html).search('p') + paragraphs.empty? ? '' : paragraphs.first.to_html end def lead diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index ff47d99..924816a 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -913,4 +913,9 @@ class ArticleTest < Test::Unit::TestCase assert_equal '

first

', a.lead end + should 'return blank as lead when article has no paragraphs' do + a = Article.new(:body => "
an article with content but without a paragraph
") + assert_equal '', a.lead + end + end -- libgit2 0.21.2