From 0eedef7633e102389bd16211f4b4e59d58043b44 Mon Sep 17 00:00:00 2001 From: tooyosi Date: Sat, 5 Oct 2024 07:13:01 +0100 Subject: [PATCH 1/5] add logic to toggle reducer keys based off user input --- app/assets/javascripts/views/forms/form.js | 25 ++++++++++++++++++++++ app/controllers/reducers_controller.rb | 15 +++++++++++++ app/views/reducers/_form.html.erb | 9 ++++++++ config/initializers/assets.rb | 1 + 4 files changed, 50 insertions(+) create mode 100644 app/assets/javascripts/views/forms/form.js diff --git a/app/assets/javascripts/views/forms/form.js b/app/assets/javascripts/views/forms/form.js new file mode 100644 index 000000000..5e9c4d259 --- /dev/null +++ b/app/assets/javascripts/views/forms/form.js @@ -0,0 +1,25 @@ +document.addEventListener("DOMContentLoaded", function () { + const form = document.querySelector("form"); + const prefix = 'reducer'; + const is_reducer_form = form?.id.includes(prefix); + + const toggle_input = function(value){ + const is_subject = value.includes('subject'); + let hidden_input_id = is_subject ? 'user_reducer_keys' : 'subject_reducer_keys'; + let visible_input_id = hidden_input_id.includes('user_reducer_keys') ? 'subject_reducer_keys' : 'user_reducer_keys'; + + document.getElementById(hidden_input_id)?.classList.remove('hidden'); + + document.getElementById(visible_input_id)?.classList.add('hidden'); + } + + if (is_reducer_form) { + const reducer_topic = document.getElementById("reducer_topic"); + + toggle_input(reducer_topic.value); + + reducer_topic.addEventListener("change", function (e) { + toggle_input(e.target.value); + }); + } +}); diff --git a/app/controllers/reducers_controller.rb b/app/controllers/reducers_controller.rb index 3afaabdd6..77112c9cb 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 + remove_unwanted_config_key(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 + remove_unwanted_config_key(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,13 @@ def reducer_params(klass) def record_not_valid(exception) render json: { error: exception.message }, status: 422 end + + def remove_unwanted_config_key(param_object) + if param_object[:topic] == 'reduce_by_subject' + param_object[:subject_reducer_keys] = nil + elsif + param_object[:user_reducer_keys] = nil + end + @reducer.save if @reducer + end end diff --git a/app/views/reducers/_form.html.erb b/app/views/reducers/_form.html.erb index 6045e10c5..f3d1d72ad 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%> +
+ + + +
diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index 4b828e80c..90bd7a780 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -12,3 +12,4 @@ # application.js, application.css, and all non-JS/CSS in the app/assets # folder are already added. # Rails.application.config.assets.precompile += %w( admin.js admin.css ) +Rails.application.config.assets.precompile += %w( views/forms/form.js ) \ No newline at end of file From b03e84daf46f4906a22ae01ca0917cc44d12f19b Mon Sep 17 00:00:00 2001 From: tooyosi Date: Mon, 7 Oct 2024 08:05:05 +0100 Subject: [PATCH 2/5] hound cleanups --- app/assets/javascripts/views/forms/form.js | 37 ++++++++++++++-------- app/controllers/reducers_controller.rb | 1 - config/initializers/assets.rb | 2 +- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/views/forms/form.js b/app/assets/javascripts/views/forms/form.js index 5e9c4d259..2f6c366d8 100644 --- a/app/assets/javascripts/views/forms/form.js +++ b/app/assets/javascripts/views/forms/form.js @@ -1,20 +1,31 @@ document.addEventListener("DOMContentLoaded", function () { - const form = document.querySelector("form"); - const prefix = 'reducer'; - const is_reducer_form = form?.id.includes(prefix); - - const toggle_input = function(value){ - const is_subject = value.includes('subject'); - let hidden_input_id = is_subject ? 'user_reducer_keys' : 'subject_reducer_keys'; - let visible_input_id = hidden_input_id.includes('user_reducer_keys') ? 'subject_reducer_keys' : 'user_reducer_keys'; - - document.getElementById(hidden_input_id)?.classList.remove('hidden'); - - document.getElementById(visible_input_id)?.classList.add('hidden'); + var form = document.querySelector("form"); + var prefix = 'reducer'; + var is_reducer_form = false + if(form){ + is_reducer_form = form.id.includes(prefix); } + + var toggle_input = function(value){ + var is_subject = value.includes('subject'); + var hidden_input_id = is_subject ? 'user_reducer_keys' : 'subject_reducer_keys'; + var visible_input_id = hidden_input_id.includes('user_reducer_keys') ? 'subject_reducer_keys' : 'user_reducer_keys'; + + var hidden_input = document.getElementById(hidden_input_id); + + if(hidden_input){ + hidden_input.classList.remove('hidden'); + }; + + var visible_input = document.getElementById(visible_input_id); + if(visible_input){ + visible_input.classList.add('hidden'); + }; + }; + if (is_reducer_form) { - const reducer_topic = document.getElementById("reducer_topic"); + var reducer_topic = document.getElementById("reducer_topic"); toggle_input(reducer_topic.value); diff --git a/app/controllers/reducers_controller.rb b/app/controllers/reducers_controller.rb index 77112c9cb..968a93589 100644 --- a/app/controllers/reducers_controller.rb +++ b/app/controllers/reducers_controller.rb @@ -144,6 +144,5 @@ def remove_unwanted_config_key(param_object) elsif param_object[:user_reducer_keys] = nil end - @reducer.save if @reducer end end diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index 90bd7a780..c88beb5ad 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -12,4 +12,4 @@ # application.js, application.css, and all non-JS/CSS in the app/assets # folder are already added. # Rails.application.config.assets.precompile += %w( admin.js admin.css ) -Rails.application.config.assets.precompile += %w( views/forms/form.js ) \ No newline at end of file +Rails.application.config.assets.precompile += %w[views/forms/form.js] From a4eca6fc8b1640f703c0721f5de4ac4bdf57443f Mon Sep 17 00:00:00 2001 From: tooyosi Date: Mon, 7 Oct 2024 14:43:49 +0100 Subject: [PATCH 3/5] add tests --- app/assets/javascripts/views/forms/form.js | 4 +-- app/controllers/reducers_controller.rb | 5 +++- spec/controllers/reducers_controller_spec.rb | 28 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/views/forms/form.js b/app/assets/javascripts/views/forms/form.js index 2f6c366d8..8b9d1d930 100644 --- a/app/assets/javascripts/views/forms/form.js +++ b/app/assets/javascripts/views/forms/form.js @@ -16,12 +16,12 @@ document.addEventListener("DOMContentLoaded", function () { if(hidden_input){ hidden_input.classList.remove('hidden'); - }; + } var visible_input = document.getElementById(visible_input_id); if(visible_input){ visible_input.classList.add('hidden'); - }; + } }; if (is_reducer_form) { diff --git a/app/controllers/reducers_controller.rb b/app/controllers/reducers_controller.rb index 968a93589..5902f64be 100644 --- a/app/controllers/reducers_controller.rb +++ b/app/controllers/reducers_controller.rb @@ -141,7 +141,10 @@ def record_not_valid(exception) def remove_unwanted_config_key(param_object) if param_object[:topic] == 'reduce_by_subject' param_object[:subject_reducer_keys] = nil - elsif + 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 diff --git a/spec/controllers/reducers_controller_spec.rb b/spec/controllers/reducers_controller_spec.rb index b9547228b..11a5f6df0 100644 --- a/spec/controllers/reducers_controller_spec.rb +++ b/spec/controllers/reducers_controller_spec.rb @@ -147,6 +147,34 @@ 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 From 88393565ca5ca2bc26c2d6329a5450678cae0460 Mon Sep 17 00:00:00 2001 From: tooyosi Date: Mon, 7 Oct 2024 14:48:49 +0100 Subject: [PATCH 4/5] hound cleanups --- spec/controllers/reducers_controller_spec.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/spec/controllers/reducers_controller_spec.rb b/spec/controllers/reducers_controller_spec.rb index 11a5f6df0..c37f0b47e 100644 --- a/spec/controllers/reducers_controller_spec.rb +++ b/spec/controllers/reducers_controller_spec.rb @@ -153,7 +153,12 @@ 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: { + url: 'https://example.org/2', + user_reducer_keys: 'user_reducer_keys_value', + subject_reducer_keys: 'subject_reducer_keys', + topic: 'reduce_by_subject' + }, } reducer.reload @@ -167,7 +172,12 @@ 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: { + 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 From 37e090367b542619a1941fc0c9caf967396bd415 Mon Sep 17 00:00:00 2001 From: tooyosi Date: Wed, 9 Oct 2024 15:32:20 +0100 Subject: [PATCH 5/5] use jquery, remove form.js and rename mehod --- app/assets/javascripts/views/forms/form.js | 36 -------------------- app/controllers/reducers_controller.rb | 6 ++-- app/views/reducers/_form.html.erb | 24 ++++++++++++- config/initializers/assets.rb | 1 - spec/controllers/reducers_controller_spec.rb | 2 +- 5 files changed, 27 insertions(+), 42 deletions(-) delete mode 100644 app/assets/javascripts/views/forms/form.js diff --git a/app/assets/javascripts/views/forms/form.js b/app/assets/javascripts/views/forms/form.js deleted file mode 100644 index 8b9d1d930..000000000 --- a/app/assets/javascripts/views/forms/form.js +++ /dev/null @@ -1,36 +0,0 @@ -document.addEventListener("DOMContentLoaded", function () { - var form = document.querySelector("form"); - var prefix = 'reducer'; - var is_reducer_form = false - if(form){ - is_reducer_form = form.id.includes(prefix); - } - - - var toggle_input = function(value){ - var is_subject = value.includes('subject'); - var hidden_input_id = is_subject ? 'user_reducer_keys' : 'subject_reducer_keys'; - var visible_input_id = hidden_input_id.includes('user_reducer_keys') ? 'subject_reducer_keys' : 'user_reducer_keys'; - - var hidden_input = document.getElementById(hidden_input_id); - - if(hidden_input){ - hidden_input.classList.remove('hidden'); - } - - var visible_input = document.getElementById(visible_input_id); - if(visible_input){ - visible_input.classList.add('hidden'); - } - }; - - if (is_reducer_form) { - var reducer_topic = document.getElementById("reducer_topic"); - - toggle_input(reducer_topic.value); - - reducer_topic.addEventListener("change", function (e) { - toggle_input(e.target.value); - }); - } -}); diff --git a/app/controllers/reducers_controller.rb b/app/controllers/reducers_controller.rb index 5902f64be..2a9154066 100644 --- a/app/controllers/reducers_controller.rb +++ b/app/controllers/reducers_controller.rb @@ -46,7 +46,7 @@ def create filters['extractor_keys'] = JSON.parse(filters['extractor_keys']) end - remove_unwanted_config_key(new_params) + reset_config_reducer_keys(new_params) @reducer = reducer_class.new(new_params) @@ -74,7 +74,7 @@ def update filters['extractor_keys'] = JSON.parse(filters['extractor_keys']) end - remove_unwanted_config_key(params) + reset_config_reducer_keys(params) @reducer.update(params) @@ -138,7 +138,7 @@ def record_not_valid(exception) render json: { error: exception.message }, status: 422 end - def remove_unwanted_config_key(param_object) + 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' diff --git a/app/views/reducers/_form.html.erb b/app/views/reducers/_form.html.erb index f3d1d72ad..16b3c5bbd 100644 --- a/app/views/reducers/_form.html.erb +++ b/app/views/reducers/_form.html.erb @@ -20,7 +20,7 @@
-