From b07b52fed18b55923e9d82592c10c9564635c04c Mon Sep 17 00:00:00 2001 From: Oluwatoyosi Oyegoke <34948675+Tooyosi@users.noreply.github.com> Date: Thu, 10 Oct 2024 06:19:49 +0100 Subject: [PATCH] add logic to toggle reducer keys based off user input (#1610) * add logic to toggle reducer keys based off user input * hound cleanups * add tests * hound cleanups * use jquery, remove form.js and rename mehod --- app/controllers/reducers_controller.rb | 17 +++++++++ app/views/reducers/_form.html.erb | 31 ++++++++++++++++ spec/controllers/reducers_controller_spec.rb | 38 ++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/app/controllers/reducers_controller.rb b/app/controllers/reducers_controller.rb index 3afaabdd6..2a9154066 100644 --- a/app/controllers/reducers_controller.rb +++ b/app/controllers/reducers_controller.rb @@ -46,6 +46,8 @@ def create filters['extractor_keys'] = JSON.parse(filters['extractor_keys']) end + reset_config_reducer_keys(new_params) + @reducer = reducer_class.new(new_params) respond_to do |format| @@ -72,6 +74,8 @@ def update filters['extractor_keys'] = JSON.parse(filters['extractor_keys']) end + reset_config_reducer_keys(params) + @reducer.update(params) respond_to do |format| @@ -122,6 +126,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 +137,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..5243d58ba 100644 --- a/spec/controllers/reducers_controller_spec.rb +++ b/spec/controllers/reducers_controller_spec.rb @@ -147,6 +147,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