Commit f50aa6ef75bb1774e7f3d14bf53e207d97f5f711
1 parent
bd4954d3
Exists in
master
and in
29 other branches
Improve article validation at comment group plugin
Showing
2 changed files
with
12 additions
and
4 deletions
Show diff stats
plugins/comment_group/lib/ext/article.rb
@@ -7,8 +7,8 @@ class Article | @@ -7,8 +7,8 @@ class Article | ||
7 | validate :not_empty_group_comments_removed | 7 | validate :not_empty_group_comments_removed |
8 | 8 | ||
9 | def not_empty_group_comments_removed | 9 | def not_empty_group_comments_removed |
10 | - if body | ||
11 | - groups_with_comments = group_comments.collect {|comment| comment.group_id}.uniq | 10 | + if body && body_changed? |
11 | + groups_with_comments = Comment.find(:all, :select => 'distinct group_id', :conditions => {:source_id => self.id}).map(&:group_id).compact | ||
12 | groups = Hpricot(body.to_s).search('.macro').collect{|element| element['data-macro-group_id'].to_i} | 12 | groups = Hpricot(body.to_s).search('.macro').collect{|element| element['data-macro-group_id'].to_i} |
13 | errors.add_to_base(N_('Not empty group comment cannot be removed')) unless (groups_with_comments-groups).empty? | 13 | errors.add_to_base(N_('Not empty group comment cannot be removed')) unless (groups_with_comments-groups).empty? |
14 | end | 14 | end |
plugins/comment_group/test/unit/article_test.rb
@@ -16,16 +16,24 @@ class ArticleTest < ActiveSupport::TestCase | @@ -16,16 +16,24 @@ class ArticleTest < ActiveSupport::TestCase | ||
16 | end | 16 | end |
17 | 17 | ||
18 | should 'do not allow a exclusion of a group comment macro if this group has comments' do | 18 | should 'do not allow a exclusion of a group comment macro if this group has comments' do |
19 | - article.update_attribute(:body, "<div class=\"macro\" data-macro-group_id=2></div>") | 19 | + article.body = "<div class=\"macro\" data-macro-group_id=2></div>" |
20 | comment1 = fast_create(Comment, :group_id => 1, :source_id => article.id) | 20 | comment1 = fast_create(Comment, :group_id => 1, :source_id => article.id) |
21 | assert !article.save | 21 | assert !article.save |
22 | assert_equal 'Not empty group comment cannot be removed', article.errors[:base] | 22 | assert_equal 'Not empty group comment cannot be removed', article.errors[:base] |
23 | end | 23 | end |
24 | 24 | ||
25 | should 'allow save if comment group macro is not removed for group with comments' do | 25 | should 'allow save if comment group macro is not removed for group with comments' do |
26 | - article.update_attribute(:body, "<div class=\"macro\" data-macro-group_id=1></div>") | 26 | + article.body = "<div class=\"macro\" data-macro-group_id=1></div>" |
27 | comment1 = fast_create(Comment, :group_id => 1, :source_id => article.id) | 27 | comment1 = fast_create(Comment, :group_id => 1, :source_id => article.id) |
28 | assert article.save | 28 | assert article.save |
29 | end | 29 | end |
30 | 30 | ||
31 | + should 'do not validate empty group if article body is not changed' do | ||
32 | + article.body = "<div class=\"macro\" data-macro-group_id=2></div>" | ||
33 | + assert article.save | ||
34 | + comment1 = fast_create(Comment, :group_id => 1, :source_id => article.id) | ||
35 | + article.name = article.name + 'changed' | ||
36 | + assert article.save | ||
37 | + end | ||
38 | + | ||
31 | end | 39 | end |