Skip to content

Commit

Permalink
add logic to toggle reducer keys based off user input (#1610)
Browse files Browse the repository at this point in the history
* add logic to toggle reducer keys based off user input

* hound cleanups

* add tests

* hound cleanups

* use jquery, remove form.js and rename mehod
  • Loading branch information
Tooyosi authored Oct 10, 2024
1 parent 6046ee4 commit b07b52f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/controllers/reducers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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|
Expand Down Expand Up @@ -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: {},
Expand All @@ -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
31 changes: 31 additions & 0 deletions app/views/reducers/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@

<%= f.input :reducible_type, :as => :hidden %>

<div id='user_reducer_keys'>
<%= f.input :user_reducer_keys%>
</div>


<div id='subject_reducer_keys'>
<%= f.input :subject_reducer_keys%>
</div>

<div class="panel panel-info">
<div class="panel-heading"><a role="button" data-toggle="collapse" href="#groupingInputs" aria-expanded="false" aria-controls="groupingInputs">Grouping <span class="caret"></span></a></div>
<div class="panel-body collapse" id="groupingInputs">
Expand Down Expand Up @@ -50,3 +59,25 @@
<%= link_to 'Back', project_path(@reducible, anchor: 'reducers'), class: 'btn btn-default' %>
<% end %>
<% end %>

<script type="text/javascript">
function toggle_reducer_keys_inputs(topic) {
if (topic.includes('subject')){
$('#subject_reducer_keys').hide()
$('#user_reducer_keys').show()
} else {
$('#subject_reducer_keys').show()
$('#user_reducer_keys').hide()
}
}

$(function() {
var reducer_topic = $('#reducer_topic').val();
toggle_reducer_keys_inputs(reducer_topic)
});

$('#reducer_topic').on('change', function(event){
var reducer_topic = event.target.value;
toggle_reducer_keys_inputs(reducer_topic)
})
</script>
38 changes: 38 additions & 0 deletions spec/controllers/reducers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b07b52f

Please sign in to comment.