Skip to content

Commit ae5786e

Browse files
authored
Merge branch 'johnpinto1-updated-port-of-dmptool-conditional-questions-fix-for-rails7' into aaron/refactor-conditions-form
2 parents 8f952be + c879520 commit ae5786e

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Bumped dependencies via `bundle update && yarn upgrade` [#3483](https://github.com/DMPRoadmap/roadmap/pull/3483)
1515
- Fixed issues with Conditional Question serialization offered by @briri from PR https://github.com/CDLUC3/dmptool/pull/667 for DMPTool. There is a migration file with code for MySQL and Postgres to update the Conditions table to convert JSON Arrays in string format records in the conditions table so that they are JSON Arrays.
1616
- Refactor `org_admin/conditions/_form.html.erb` [#3502](https://github.com/DMPRoadmap/roadmap/pull/3502)
17+
- Refactor `Question.save_condition` [#3501](https://github.com/DMPRoadmap/roadmap/pull/3501)
1718

1819
## v4.2.0
1920

app/models/question.rb

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -213,60 +213,38 @@ def update_conditions(param_conditions, old_to_new_opts, question_id_map)
213213
end
214214
end
215215

216-
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
217-
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
216+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
218217
def save_condition(value, opt_map, question_id_map)
219218
c = conditions.build
220219
c.action_type = value['action_type']
221220
c.number = value['number']
222-
# question options may have changed so rewrite them
223-
c.option_list = value['question_option']
224221

225-
if opt_map.present?
226-
new_question_options = []
227-
c.option_list.map do |qopt|
228-
new_question_options << opt_map[qopt]
229-
end
230-
c.option_list = new_question_options
222+
# question options may have changed so rewrite them
223+
c.option_list = handle_option_list(value, opt_map)
224+
# Do not save the condition if the option_list is empty
225+
if c.option_list.empty?
226+
c.destroy
227+
return
231228
end
232229

233230
if value['action_type'] == 'remove'
234-
c.remove_data = value['remove_question_id']
235-
if question_id_map.present?
236-
new_question_ids = []
237-
c.remove_data.map do |qid|
238-
new_question_ids << question_id_map[qid]
239-
end
240-
c.remove_data = new_question_ids
241-
end
242-
243-
# Do not save the condition if the option_list or remove_data is empty
244-
if c.option_list.empty? || c.remove_data.empty?
231+
c.remove_data = handle_remove_data(value, question_id_map)
232+
# Do not save the condition if remove_data is empty
233+
if c.remove_data.empty?
245234
c.destroy
246235
return
247236
end
248237
else
249-
c.webhook_data = {
250-
name: value['webhook-name'],
251-
email: value['webhook-email'],
252-
subject: value['webhook-subject'],
253-
message: value['webhook-message']
254-
}
255-
256-
# Do not save the condition if the option_list or if any webhook_data fields is empty
257-
if c.option_list.empty? ||
258-
c.webhook_data['name'].blank? ||
259-
c.webhook_data['email'].blank? ||
260-
c.webhook_data['subject'].blank? ||
261-
c.webhook_data['message'].blank?
238+
c.webhook_data = handle_webhook_data(value)
239+
# Do not save the condition if webhook_data is nil
240+
if c.webhook_data.nil?
262241
c.destroy
263242
return
264243
end
265244
end
266245
c.save
267246
end
268-
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
269-
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
247+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
270248

271249
private
272250

@@ -293,4 +271,33 @@ def check_remove_conditions
293271
end
294272
end
295273
# rubocop:enable Metrics/AbcSize
274+
275+
def handle_option_list(value, opt_map)
276+
if opt_map.present?
277+
value['question_option'].map { |qopt| opt_map[qopt] }
278+
else
279+
value['question_option']
280+
end
281+
end
282+
283+
def handle_remove_data(value, question_id_map)
284+
if question_id_map.present?
285+
value['remove_question_id'].map { |qid| question_id_map[qid] }
286+
else
287+
value['remove_question_id']
288+
end
289+
end
290+
291+
def handle_webhook_data(value)
292+
# return nil if any of the webhook fields are blank
293+
return if %w[webhook-name webhook-email webhook-subject webhook-message].any? { |key| value[key].blank? }
294+
295+
# else return the constructed webhook_data hash
296+
{
297+
name: value['webhook-name'],
298+
email: value['webhook-email'],
299+
subject: value['webhook-subject'],
300+
message: value['webhook-message']
301+
}
302+
end
296303
end

0 commit comments

Comments
 (0)