From 8f12567b5d91bbf7747a0e08a0d51b20f395e137 Mon Sep 17 00:00:00 2001 From: Simon Reed Date: Tue, 23 Jul 2019 17:06:35 +0100 Subject: [PATCH] Allow class name as parent_type on Construct #221 --- app/controllers/construct_controller.rb | 13 +++++++------ .../cc_conditions_controller_test.rb | 17 ++++++++++++++++- test/controllers/cc_loops_controller_test.rb | 17 ++++++++++++++++- .../cc_questions_controller_test.rb | 19 +++++++++++++++++-- .../cc_sequences_controller_test.rb | 17 ++++++++++++++++- .../cc_statements_controller_test.rb | 17 ++++++++++++++++- 6 files changed, 88 insertions(+), 12 deletions(-) diff --git a/app/controllers/construct_controller.rb b/app/controllers/construct_controller.rb index d463b002..78dfb919 100755 --- a/app/controllers/construct_controller.rb +++ b/app/controllers/construct_controller.rb @@ -24,7 +24,8 @@ def safe_params private def shim_construct_type(p) - unless p[:parent_type].nil? + parental_classes = %w(CcCondition CcLoop CcQuestion CcSequence CcStatement) + unless p[:parent_type].nil? || parental_classes.include?(p[:parent_type]) p[:parent_type] = case p[:parent_type].downcase when 'condition' then 'CcCondition' @@ -42,11 +43,11 @@ def shim_construct_type(p) end def wrap_parent_param(p, obj_name) - if p.has_key? :parent - p[obj_name][:parent_id] = p[:parent][:id] - p[obj_name][:parent_type] = p[:parent][:type] - p.delete(:parent) + parent_object = (p.has_key?(obj_name)) ? p[obj_name].delete(:parent) : p.delete(:parent) + if parent_object + p[obj_name][:parent_id] = parent_object[:id] + p[obj_name][:parent_type] = parent_object[:type] end p end -end \ No newline at end of file +end diff --git a/test/controllers/cc_conditions_controller_test.rb b/test/controllers/cc_conditions_controller_test.rb index d503d8cb..2a4ac6cc 100755 --- a/test/controllers/cc_conditions_controller_test.rb +++ b/test/controllers/cc_conditions_controller_test.rb @@ -39,8 +39,23 @@ class CcConditionsControllerTest < ActionController::TestCase end test "should update cc_condition" do - patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_condition, cc_condition: {literal: @cc_condition.literal, logic: @cc_condition.logic} } + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_condition, cc_condition: {literal: @cc_condition.literal, logic: @cc_condition.logic, parent: { + id: @instrument.cc_sequences.first.id, + type: 'sequence' + }} } assert_response :success + + assert_equal @cc_condition.reload.parent, @instrument.cc_sequences.first + end + + test "should update cc_condition when parent type matches class name" do + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_condition, cc_condition: {literal: @cc_condition.literal, logic: @cc_condition.logic, parent: { + id: @instrument.cc_sequences.first.id, + type: 'CcSequence' + }} } + assert_response :success + + assert_equal @cc_condition.reload.parent, @instrument.cc_sequences.first end test "should destroy cc_condition" do diff --git a/test/controllers/cc_loops_controller_test.rb b/test/controllers/cc_loops_controller_test.rb index 97dc7cf9..be930e87 100755 --- a/test/controllers/cc_loops_controller_test.rb +++ b/test/controllers/cc_loops_controller_test.rb @@ -42,8 +42,23 @@ class CcLoopsControllerTest < ActionController::TestCase end test "should update cc_loop" do - patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_loop, cc_loop: {end_val: @cc_loop.end_val, loop_var: @cc_loop.loop_var, loop_while: @cc_loop.loop_while, start_val: @cc_loop.start_val} } + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_loop, cc_loop: {end_val: @cc_loop.end_val, loop_var: @cc_loop.loop_var, loop_while: @cc_loop.loop_while, start_val: @cc_loop.start_val, parent: { + id: @instrument.cc_sequences.first.id, + type: 'sequence' + }} } assert_response :success + + assert_equal @cc_loop.reload.parent, @instrument.cc_sequences.first + end + + test "should update cc_loop when parent type matches class name" do + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_loop, cc_loop: {end_val: @cc_loop.end_val, loop_var: @cc_loop.loop_var, loop_while: @cc_loop.loop_while, start_val: @cc_loop.start_val, parent: { + id: @instrument.cc_sequences.first.id, + type: 'CcSequence' + }} } + assert_response :success + + assert_equal @cc_loop.reload.parent, @instrument.cc_sequences.first end test "should destroy cc_loop" do diff --git a/test/controllers/cc_questions_controller_test.rb b/test/controllers/cc_questions_controller_test.rb index fb95bb2a..f0fc72ac 100755 --- a/test/controllers/cc_questions_controller_test.rb +++ b/test/controllers/cc_questions_controller_test.rb @@ -41,8 +41,23 @@ class CcQuestionsControllerTest < ActionController::TestCase end test "should update cc_question" do - patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_question, cc_question: {question_id: @cc_question.question_id, question_type: @cc_question.question_type} } + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_question, cc_question: {question_id: @cc_question.question_id, question_type: @cc_question.question_type, parent: { + id: @instrument.cc_sequences.first.id, + type: 'sequence' + }} } assert_response :success + + assert_equal @cc_question.reload.parent, @instrument.cc_sequences.first + end + + test "should update cc_question when parent type matches class name" do + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_question, cc_question: {question_id: @cc_question.question_id, question_type: @cc_question.question_type, parent: { + id: @instrument.cc_sequences.first.id, + type: 'CcSequence' + }} } + assert_response :success + + assert_equal @cc_question.reload.parent, @instrument.cc_sequences.first end test "should destroy cc_question" do @@ -52,4 +67,4 @@ class CcQuestionsControllerTest < ActionController::TestCase assert_response :success end -end \ No newline at end of file +end diff --git a/test/controllers/cc_sequences_controller_test.rb b/test/controllers/cc_sequences_controller_test.rb index bada447b..e4343f65 100755 --- a/test/controllers/cc_sequences_controller_test.rb +++ b/test/controllers/cc_sequences_controller_test.rb @@ -40,8 +40,23 @@ class CcSequencesControllerTest < ActionController::TestCase end test "should update cc_sequence" do - patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_sequence, cc_sequence: {literal: @cc_sequence.literal} } + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_sequence, cc_sequence: {literal: @cc_sequence.literal, parent: { + id: @instrument.cc_sequences.first.id, + type: 'sequence' + }} } + assert_response :success + + assert_equal @cc_sequence.reload.parent, @instrument.cc_sequences.first + end + + test "should update cc_sequence when parent type matches class name" do + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_sequence, cc_sequence: {literal: @cc_sequence.literal, parent: { + id: @instrument.cc_sequences.first.id, + type: 'CcSequence' + }} } assert_response :success + + assert_equal @cc_sequence.reload.parent, @instrument.cc_sequences.first end test "should destroy cc_sequence" do diff --git a/test/controllers/cc_statements_controller_test.rb b/test/controllers/cc_statements_controller_test.rb index 43812e61..7ef85dd8 100755 --- a/test/controllers/cc_statements_controller_test.rb +++ b/test/controllers/cc_statements_controller_test.rb @@ -40,8 +40,23 @@ class CcStatementsControllerTest < ActionController::TestCase end test "should update cc_statement" do - patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_statement, cc_statement: {literal: @cc_statement.literal} } + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_statement, cc_statement: {literal: @cc_statement.literal, parent: { + id: @instrument.cc_sequences.first.id, + type: 'sequence' + }} } + assert_response :success + + assert_equal @cc_statement.reload.parent, @instrument.cc_sequences.first + end + + test "should update cc_statement when parent type matches class name" do + patch :update, format: :json, params: { instrument_id: @instrument.id, id: @cc_statement, cc_statement: {literal: @cc_statement.literal, parent: { + id: @instrument.cc_sequences.first.id, + type: 'CcSequence' + }} } assert_response :success + + assert_equal @cc_statement.reload.parent, @instrument.cc_sequences.first end test "should destroy cc_statement" do