Commit 2508aeedee2ca744ddeb139d1b2e3436149404f8

Authored by Joenio Costa
1 parent 8f1693a8

Prevent crash when looking for lead in article without paragraphs

(ActionItem1670)
app/models/article.rb
... ... @@ -342,7 +342,8 @@ class Article < ActiveRecord::Base
342 342 end
343 343  
344 344 def first_paragraph
345   - Hpricot(to_html).search('p').first.to_html
  345 + paragraphs = Hpricot(to_html).search('p')
  346 + paragraphs.empty? ? '' : paragraphs.first.to_html
346 347 end
347 348  
348 349 def lead
... ...
test/unit/article_test.rb
... ... @@ -913,4 +913,9 @@ class ArticleTest < Test::Unit::TestCase
913 913 assert_equal '<p>first</p>', a.lead
914 914 end
915 915  
  916 + should 'return blank as lead when article has no paragraphs' do
  917 + a = Article.new(:body => "<div>an article with content <em>but without</em> a paragraph</div>")
  918 + assert_equal '', a.lead
  919 + end
  920 +
916 921 end
... ...