Commit 0af76ab383924eb64d48cb9963462c932b9cca09
1 parent
b2673656
Exists in
master
and in
28 other branches
ActionItem231: fixed reference to unexisting (e.g. removed) article.
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1614 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
30 additions
and
1 deletions
Show diff stats
app/models/article_block.rb
@@ -19,7 +19,14 @@ class ArticleBlock < Block | @@ -19,7 +19,14 @@ class ArticleBlock < Block | ||
19 | def article(reload = false) | 19 | def article(reload = false) |
20 | @article = nil if reload | 20 | @article = nil if reload |
21 | if @article || article_id | 21 | if @article || article_id |
22 | - @article = Article.find(article_id) | 22 | + begin |
23 | + @article = Article.find(article_id) | ||
24 | + rescue ActiveRecord::RecordNotFound | ||
25 | + # dangling reference, clear it | ||
26 | + @article = nil | ||
27 | + self.article_id = nil | ||
28 | + self.save! | ||
29 | + end | ||
23 | end | 30 | end |
24 | @article | 31 | @article |
25 | end | 32 | end |
test/unit/article_block_test.rb
@@ -30,4 +30,26 @@ class ArticleBlockTest < Test::Unit::TestCase | @@ -30,4 +30,26 @@ class ArticleBlockTest < Test::Unit::TestCase | ||
30 | 30 | ||
31 | end | 31 | end |
32 | 32 | ||
33 | + should 'not crash when referenced article is removed' do | ||
34 | + person = create_user('testuser').person | ||
35 | + a = person.articles.create!(:name => 'test') | ||
36 | + block = ArticleBlock.create(:article => a) | ||
37 | + person.boxes.first.blocks << block | ||
38 | + block.save! | ||
39 | + | ||
40 | + a.destroy | ||
41 | + block.reload | ||
42 | + assert_nil block.article | ||
43 | + end | ||
44 | + | ||
45 | + should 'nullify reference to unexisting article' do | ||
46 | + Article.delete_all | ||
47 | + | ||
48 | + block = ArticleBlock.new | ||
49 | + block.article_id = 999 | ||
50 | + | ||
51 | + block.article | ||
52 | + assert_nil block.article_id | ||
53 | + end | ||
54 | + | ||
33 | end | 55 | end |