From 4ff7c171884206aa3885a649d45be46adeb0642a Mon Sep 17 00:00:00 2001 From: Oluwatoyosi Oyegoke <34948675+Tooyosi@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:02:50 +0100 Subject: [PATCH] add logic to save extractor keys as array if string (#1611) * add logic to save extractor keys as array if string * remove convert_to_array logic form reducers_controller * sync in base * modify test case --- app/controllers/reducers_controller.rb | 10 ++++++++-- spec/controllers/reducers_controller_spec.rb | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/reducers_controller.rb b/app/controllers/reducers_controller.rb index 2a915406..330194d9 100644 --- a/app/controllers/reducers_controller.rb +++ b/app/controllers/reducers_controller.rb @@ -43,7 +43,10 @@ def create filters = new_params.fetch('filters', {}) if filters.has_key?('extractor_keys') && filters['extractor_keys'].is_a?(String) - filters['extractor_keys'] = JSON.parse(filters['extractor_keys']) + begin + filters['extractor_keys'] = JSON.parse(filters['extractor_keys']) + rescue JSON::ParserError, TypeError; + end end reset_config_reducer_keys(new_params) @@ -71,7 +74,10 @@ def update filters = params.fetch('filters', {}) if filters.has_key?('extractor_keys') && filters['extractor_keys'].is_a?(String) - filters['extractor_keys'] = JSON.parse(filters['extractor_keys']) + begin + filters['extractor_keys'] = JSON.parse(filters['extractor_keys']) + rescue JSON::ParserError, TypeError; + end end reset_config_reducer_keys(params) diff --git a/spec/controllers/reducers_controller_spec.rb b/spec/controllers/reducers_controller_spec.rb index 5243d58b..1b9671ab 100644 --- a/spec/controllers/reducers_controller_spec.rb +++ b/spec/controllers/reducers_controller_spec.rb @@ -96,6 +96,16 @@ expect(workflow.reducers.first.filters['extractor_keys']).to eq(['test']) end + it 'saves extractor_keys in inputted format' do + nested_reducer_params[:filters][:extractor_keys] = '[\'test\']' + post :create, params: { + workflow_id: workflow.id, + reducer: nested_reducer_params + }, format: :json + + expect(workflow.reducers.first.filters['extractor_keys']).to eq('[\'test\']') + end + it 'jsonifies extractor_keys' do post :create, params: { workflow_id: workflow.id,