From f50aa6ef75bb1774e7f3d14bf53e207d97f5f711 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 9 Dec 2013 16:38:09 -0300 Subject: [PATCH] Improve article validation at comment group plugin --- plugins/comment_group/lib/ext/article.rb | 4 ++-- plugins/comment_group/test/unit/article_test.rb | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/plugins/comment_group/lib/ext/article.rb b/plugins/comment_group/lib/ext/article.rb index 042e260..3e073c0 100644 --- a/plugins/comment_group/lib/ext/article.rb +++ b/plugins/comment_group/lib/ext/article.rb @@ -7,8 +7,8 @@ class Article validate :not_empty_group_comments_removed def not_empty_group_comments_removed - if body - groups_with_comments = group_comments.collect {|comment| comment.group_id}.uniq + if body && body_changed? + groups_with_comments = Comment.find(:all, :select => 'distinct group_id', :conditions => {:source_id => self.id}).map(&:group_id).compact groups = Hpricot(body.to_s).search('.macro').collect{|element| element['data-macro-group_id'].to_i} errors.add_to_base(N_('Not empty group comment cannot be removed')) unless (groups_with_comments-groups).empty? end diff --git a/plugins/comment_group/test/unit/article_test.rb b/plugins/comment_group/test/unit/article_test.rb index 12f9b3a..15a963f 100644 --- a/plugins/comment_group/test/unit/article_test.rb +++ b/plugins/comment_group/test/unit/article_test.rb @@ -16,16 +16,24 @@ class ArticleTest < ActiveSupport::TestCase end should 'do not allow a exclusion of a group comment macro if this group has comments' do - article.update_attribute(:body, "
") + article.body = "
" comment1 = fast_create(Comment, :group_id => 1, :source_id => article.id) assert !article.save assert_equal 'Not empty group comment cannot be removed', article.errors[:base] end should 'allow save if comment group macro is not removed for group with comments' do - article.update_attribute(:body, "
") + article.body = "
" comment1 = fast_create(Comment, :group_id => 1, :source_id => article.id) assert article.save end + should 'do not validate empty group if article body is not changed' do + article.body = "
" + assert article.save + comment1 = fast_create(Comment, :group_id => 1, :source_id => article.id) + article.name = article.name + 'changed' + assert article.save + end + end -- libgit2 0.21.2