Skip to content

Commit

Permalink
remove convert_to_array logic form reducers_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Tooyosi committed Oct 9, 2024
1 parent 0cef175 commit dbdb3d9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
22 changes: 8 additions & 14 deletions app/controllers/reducers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = convert_to_array(filters['extractor_keys'])
begin
filters['extractor_keys'] = JSON.parse(filters['extractor_keys'])
rescue JSON::ParserError, TypeError;
end
end

@reducer = reducer_class.new(new_params)
Expand All @@ -69,7 +72,10 @@ def update

filters = params.fetch('filters', {})
if filters.has_key?('extractor_keys') && filters['extractor_keys'].is_a?(String)
filters['extractor_keys'] = convert_to_array(filters['extractor_keys'])
begin
filters['extractor_keys'] = JSON.parse(filters['extractor_keys'])
rescue JSON::ParserError, TypeError;
end
end

@reducer.update(params)
Expand Down Expand Up @@ -132,16 +138,4 @@ def record_not_valid(exception)
render json: { error: exception.message }, status: 422
end

def convert_to_array(input)
if input.strip.start_with?("[") && input.strip.end_with?("]")
json_string = input.gsub("'", '"')
begin
JSON.parse(json_string)
rescue JSON::ParserError
[input]
end
else
[input]
end
end
end
8 changes: 0 additions & 8 deletions spec/controllers/reducers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,6 @@
expect(workflow.reducers.first.url).to eq('https://example.org/2')
end

it 'saves extractor_keys as an array' do
put :update, params: {workflow_id: workflow.id,
id: reducer.id,
reducer: {url: 'https://example.org/2', filters: { extractor_keys: 'test' }}}

reducer.reload
expect(workflow.reducers.first.filters['extractor_keys']).to eq(['test'])
end
it 'renders form on errors' do
put :update, params: {workflow_id: workflow.id,
id: reducer.id,
Expand Down

0 comments on commit dbdb3d9

Please sign in to comment.