Skip to content

Commit 787f902

Browse files
committed
Update questions_controller.rb
1 parent 1a1f72d commit 787f902

1 file changed

Lines changed: 47 additions & 9 deletions

File tree

app/controllers/org_admin/questions_controller.rb

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class QuestionsController < ApplicationController
1414
def show
1515
question = Question.includes(:annotations,
1616
:question_options,
17+
:question_identifiers,
1718
section: { phase: :template })
1819
.find(params[:id])
1920
authorize question
@@ -44,6 +45,7 @@ def open_conditions
4445
def edit
4546
question = Question.includes(:annotations,
4647
:question_options,
48+
:question_identifiers,
4749
section: { phase: :template })
4850
.find(params[:id])
4951
authorize question
@@ -63,7 +65,6 @@ def new
6365
nbr = section.questions.maximum(:number)
6466
question_format = QuestionFormat.find_by(title: 'Text area')
6567
question = Question.new(section_id: section.id,
66-
question_format: question_format,
6768
number: nbr.present? ? nbr + 1 : 1)
6869
question_formats = allowed_question_formats
6970
authorize question
@@ -86,16 +87,20 @@ def new
8687
# rubocop:disable Metrics/AbcSize
8788
def create
8889
question = Question.new(question_params.merge(section_id: params[:section_id]))
90+
8991
authorize question
9092
begin
9193
question = get_new(question)
9294
section = question.section
95+
9396
if question.save
9497
flash[:notice] = success_message(question, _('created'))
9598
else
9699
flash[:alert] = failure_message(question, _('create'))
97100
end
98-
rescue StandardError
101+
102+
rescue StandardError => e
103+
Rails.logger.error "Unable to save template - #{e.message}"
99104
flash[:alert] = _('Unable to create a new version of this template.')
100105
end
101106
redirect_to edit_org_admin_template_phase_path(
@@ -111,6 +116,7 @@ def create
111116
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
112117
def update
113118
question = Question.find(params[:id])
119+
114120
authorize question
115121

116122
new_version = question.template.generate_version?
@@ -123,10 +129,17 @@ def update
123129
old_number_to_id[opt.number] = opt.id
124130
end
125131

132+
# question_identifiers get a map from versionable_id field to the old id
133+
old_question_identifier_ids = {}
134+
question.question_identifiers.each do |qid|
135+
old_question_identifier_ids[qid.versionable_id] = qid.id
136+
end
137+
126138
# get a map from question versionable id to old id
127139
question.template.questions.each do |q|
128140
old_question_ids[q.versionable_id] = q.id
129141
end
142+
130143
end
131144

132145
question = get_modifiable(question)
@@ -142,19 +155,29 @@ def update
142155
old_to_new_opts[old_id.to_s] = opt.id.to_s
143156
end
144157

158+
# question_identifiers get a map from versionable_id field to the new id
159+
old_to_new_question_identifiers = {}
160+
question.question_identifiers.each do |qid|
161+
old_id = old_question_identifier_ids[qid.versionable_id]
162+
old_to_new_question_identifiers[old_id.to_s] = qid.id.to_s
163+
end
164+
145165
question.template.questions.each do |q|
146166
question_id_map[old_question_ids[q.versionable_id].to_s] = q.id.to_s
147167
end
148168
end
149169

170+
attrs = question_params
150171
# rewrite the question_option ids so they match the new
151172
# version of the question
152-
# and also rewrite the remove_data question ids
153-
attrs = question_params
154173
attrs = update_option_ids(attrs, old_to_new_opts) if new_version && !attrs['question_options_attributes'].nil?
155174

175+
# rewrite the question_identifiers ids so they match the new
176+
attrs = update_question_identifiers_ids(attrs, old_to_new_question_identifiers) if new_version && !attrs['question_identifiers_attributes'].nil?
177+
156178
# Need to reattach the incoming annotation's and question_options to the
157179
# modifiable (versioned) question
180+
# and also rewrite the remove_data question ids
158181
attrs = transfer_associations(attrs, question) if new_version
159182

160183
# If the user unchecked all of the themes set the association to an empty array
@@ -163,8 +186,8 @@ def update
163186
if question.update(attrs)
164187
if question.update_conditions(sanitize_hash(params['conditions']),
165188
old_to_new_opts, question_id_map)
166-
flash[:notice] = success_message(question, _('updated'))
167189
end
190+
flash[:notice] = success_message(question, _('updated'))
168191
else
169192
flash[:alert] = flash[:alert] = failure_message(question, _('update'))
170193
end
@@ -249,22 +272,37 @@ def question_params
249272
params.require(:question)
250273
.permit(:number, :text, :question_format_id, :option_comment_display,
251274
:default_value,
252-
question_options_attributes: %i[id number text is_default _destroy],
275+
question_identifiers_attributes: %i[id question_id value name _destroy],
276+
question_options_attributes: %i[id number text answer_identifier is_default _destroy],
253277
annotations_attributes: %i[id text org_id org type _destroy],
254278
theme_ids: [])
255279
end
256280

257281
# when a template gets versioned while saving the question
258282
# options are now out of sync with the params.
259283
# This sorts that out.
260-
def update_option_ids(attrs_in, opt_map)
261-
qopts = attrs_in['question_options_attributes']
284+
def update_option_ids(attrs, opt_map)
285+
qopts = attrs['question_options_attributes']
262286
qopts.each_pair do |_, attr_hash|
263287
old_id = attr_hash['id']
264288
new_id = opt_map[old_id]
265289
attr_hash['id'] = new_id
266290
end
267-
attrs_in
291+
attrs
292+
end
293+
294+
# when a template gets versioned while saving the question
295+
# identifiers are now out of sync with the params.
296+
# This sorts that out.
297+
# This method is call above in line 177
298+
def update_question_identifiers_ids(attrs, question_identifier_map)
299+
qopts = attrs['question_identifiers_attributes']
300+
qopts.each_pair do |_, attr_hash|
301+
old_id = attr_hash['id']
302+
new_id = question_identifier_map[old_id]
303+
attr_hash['id'] = new_id
304+
end
305+
attrs
268306
end
269307

270308
# When a template gets versioned by changes to one of its questions we need to loop

0 commit comments

Comments
 (0)