Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logic to toggle reducer keys based off user input #1610

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading