diff --git a/app/controllers/reducers_controller.rb b/app/controllers/reducers_controller.rb index 3afaabdd6..330194d9d 100644 --- a/app/controllers/reducers_controller.rb +++ b/app/controllers/reducers_controller.rb @@ -43,9 +43,14 @@ 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) + @reducer = reducer_class.new(new_params) respond_to do |format| @@ -69,9 +74,14 @@ 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) + @reducer.update(params) respond_to do |format| @@ -122,6 +132,8 @@ def reducer_params(klass) params.require(:reducer).permit( :key, :topic, + :user_reducer_keys, + :subject_reducer_keys, *klass.configuration_fields.keys, filters: {}, grouping: {}, @@ -131,4 +143,15 @@ def reducer_params(klass) def record_not_valid(exception) render json: { error: exception.message }, status: 422 end + + def reset_config_reducer_keys(param_object) + if param_object[:topic] == 'reduce_by_subject' + param_object[:subject_reducer_keys] = nil + elsif param_object[:topic] == 'reduce_by_user' + param_object[:user_reducer_keys] = nil + else + param_object[:subject_reducer_keys] = nil + param_object[:user_reducer_keys] = nil + end + end end diff --git a/app/views/reducers/_form.html.erb b/app/views/reducers/_form.html.erb index 6045e10c5..16b3c5bbd 100644 --- a/app/views/reducers/_form.html.erb +++ b/app/views/reducers/_form.html.erb @@ -15,6 +15,15 @@ <%= f.input :reducible_type, :as => :hidden %> +
+ <%= f.input :user_reducer_keys%> +
+ + +
+ <%= f.input :subject_reducer_keys%> +
+
@@ -50,3 +59,25 @@ <%= link_to 'Back', project_path(@reducible, anchor: 'reducers'), class: 'btn btn-default' %> <% end %> <% end %> + + \ No newline at end of file diff --git a/spec/controllers/reducers_controller_spec.rb b/spec/controllers/reducers_controller_spec.rb index b9547228b..1b9671ab0 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, @@ -147,6 +157,44 @@ reducer: {key: nil, type: 'external'}} expect(response.status).to eq(200) end + + context 'when topic is reduce_by_subject' do + it 'sets subject_reducer_keys to nil' do + put :update, params: { + workflow_id: workflow.id, + id: reducer.id, + reducer: { + url: 'https://example.org/2', + user_reducer_keys: 'user_reducer_keys_value', + subject_reducer_keys: 'subject_reducer_keys', + topic: 'reduce_by_subject' + } + } + + reducer.reload + expect(reducer.subject_reducer_keys).to be_nil + expect(reducer.user_reducer_keys).to eq('user_reducer_keys_value') + end + end + + context 'when topic is not reduce_by_subject' do + it 'sets user_reducer_keys to nil' do + put :update, params: { + workflow_id: workflow.id, + id: reducer.id, + reducer: { + url: 'https://example.org/2', + user_reducer_keys: 'user_reducer_keys_value', + subject_reducer_keys: 'subject_reducer_keys_value', + topic: 'reduce_by_user' + } + } + + reducer.reload + expect(reducer.subject_reducer_keys).to eq('subject_reducer_keys_value') + expect(reducer.user_reducer_keys).to be_nil + end + end end end end