Skip to content

Commit

Permalink
Allow class name as parent_type on Construct #221
Browse files Browse the repository at this point in the history
  • Loading branch information
simonreed committed Jul 23, 2019
1 parent 3032302 commit 8f12567
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
13 changes: 7 additions & 6 deletions app/controllers/construct_controller.rb
Expand Up @@ -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'
Expand All @@ -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
end
17 changes: 16 additions & 1 deletion test/controllers/cc_conditions_controller_test.rb
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion test/controllers/cc_loops_controller_test.rb
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions test/controllers/cc_questions_controller_test.rb
Expand Up @@ -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
Expand All @@ -52,4 +67,4 @@ class CcQuestionsControllerTest < ActionController::TestCase

assert_response :success
end
end
end
17 changes: 16 additions & 1 deletion test/controllers/cc_sequences_controller_test.rb
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion test/controllers/cc_statements_controller_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 8f12567

Please sign in to comment.