Skip to content

Commit

Permalink
Node deletion problem while removing spam solved (publiclab#2450)
Browse files Browse the repository at this point in the history
* solved for answer deleting node

* node optimized

* tests added
  • Loading branch information
grvsachdeva authored and jywarren committed Mar 9, 2018
1 parent df9596c commit 707f2f7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Answer < ActiveRecord::Base

attr_accessible :uid, :nid, :content, :cached_likes, :created_at, :updated_at

belongs_to :node, foreign_key: 'nid', dependent: :destroy
belongs_to :node, foreign_key: 'nid'
belongs_to :drupal_user, foreign_key: 'uid'
has_many :answer_selections, foreign_key: 'aid'
has_many :comments, foreign_key: 'aid', dependent: :destroy
Expand Down
2 changes: 1 addition & 1 deletion app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def updated_month
has_many :drupal_content_field_map_editor, foreign_key: 'nid' #, dependent: :destroy # re-enable in Rails 5
has_many :images, foreign_key: :nid
has_many :node_selections, foreign_key: :nid
has_many :answers, foreign_key: :nid
has_many :answers, foreign_key: :nid, dependent: :destroy

belongs_to :drupal_user, foreign_key: 'uid'

Expand Down
13 changes: 13 additions & 0 deletions test/functional/answers_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,17 @@ def setup
assert answer.accepted
end

test 'answer deletion should not delete the question node' do
UserSession.create(users(:moderator))
answer = answers(:one)
answer.save
a = Answer.count
b = Node.count

xhr :post, :delete, id: answer.id

assert_equal Answer.count ,a - 1
assert_equal Node.count,b
assert_response :success
end
end
16 changes: 16 additions & 0 deletions test/functional/notes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -648,4 +648,20 @@ def test_get_rss_feed
assert_equal "Only author can access the draft note", flash[:notice]
end

test 'question deletion should delete all its answers' do
UserSession.create(users(:moderator))
node = nodes(:question)
node.save
answer1 = answers(:one)
answer1.save
answer2 = answers(:two)
answer2.save
n_count = Node.count

xhr :post, :delete, id: node.id

assert_response :success
assert_equal Node.count, n_count - 1
assert_equal Answer.count, 0
end
end

0 comments on commit 707f2f7

Please sign in to comment.