From 1bc8a455c19c4796d01a292ff492551872210f5b Mon Sep 17 00:00:00 2001 From: gauravano Date: Fri, 9 Mar 2018 01:08:27 +0530 Subject: [PATCH 1/3] solved for answer deleting node --- app/models/answer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/answer.rb b/app/models/answer.rb index e30747db7f..49f993bc14 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -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 From 4534bc9dc047be400ffe558c5a170101e5ca27cf Mon Sep 17 00:00:00 2001 From: gauravano Date: Fri, 9 Mar 2018 02:44:26 +0530 Subject: [PATCH 2/3] node optimized --- app/models/node.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/node.rb b/app/models/node.rb index 9f267433f0..2d3bbdfff9 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -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' From 841178dcd55382a5fd1c0564f257237730830857 Mon Sep 17 00:00:00 2001 From: gauravano Date: Fri, 9 Mar 2018 04:09:11 +0530 Subject: [PATCH 3/3] tests added --- test/functional/answers_controller_test.rb | 13 +++++++++++++ test/functional/notes_controller_test.rb | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/test/functional/answers_controller_test.rb b/test/functional/answers_controller_test.rb index 7dad7ffb5d..867a4f4029 100644 --- a/test/functional/answers_controller_test.rb +++ b/test/functional/answers_controller_test.rb @@ -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 diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index c538548945..359449e1f3 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -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