Skip to content

Commit a840abb

Browse files
committed
Refactor answer transaction into separate methods
These changes refactor the `Answer.transaction do` block into separate `create_answer`, `update_answer`, and `handle_stale_answer_error` methods. The changes are almost entirely a direct cut/paste from the original block into the corresponding methods. The only alteration is the prior `if q.question_format.rda_metadata?` check to `return unless q.question_format.rda_metadata?`. This early return check was auto-applied by rubocop.
1 parent efecb33 commit a840abb

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

app/controllers/answers_controller.rb

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ def create_or_update
123123
private
124124

125125
def handle_answer_transaction(p_params, q)
126-
# rubocop:disable Metrics/BlockLength
127126
Answer.transaction do
128127
args = p_params
129128
# Answer model does not understand :standards so remove it from the params
@@ -134,38 +133,49 @@ def handle_answer_transaction(p_params, q)
134133
plan_id: args[:plan_id],
135134
question_id: args[:question_id]
136135
)
137-
authorize @answer
138-
139-
@answer.update(args.merge(user_id: current_user.id))
140-
if args[:question_option_ids].present?
141-
# Saves the record with the updated_at set to the current time.
142-
# Needed if only answer.question_options is updated
143-
@answer.touch
144-
end
145-
if q.question_format.rda_metadata?
146-
@answer.update_answer_hash(
147-
JSON.parse(standards.to_json), args[:text]
148-
)
149-
@answer.save!
150-
end
136+
update_answer(args, q, standards)
151137
rescue ActiveRecord::RecordNotFound
152-
@answer = Answer.new(args.merge(user_id: current_user.id))
153-
@answer.lock_version = 1
154-
authorize @answer
155-
if q.question_format.rda_metadata?
156-
@answer.update_answer_hash(
157-
JSON.parse(standards.to_json), args[:text]
158-
)
159-
end
160-
@answer.save!
138+
create_answer(args, q, standards)
161139
rescue ActiveRecord::StaleObjectError
162-
@stale_answer = @answer
163-
@answer = Answer.find_by(
164-
plan_id: args[:plan_id],
165-
question_id: args[:question_id]
140+
handle_stale_answer_error(args)
141+
end
142+
end
143+
144+
def create_answer(args, q, standards)
145+
@answer = Answer.new(args.merge(user_id: current_user.id))
146+
@answer.lock_version = 1
147+
authorize @answer
148+
if q.question_format.rda_metadata?
149+
@answer.update_answer_hash(
150+
JSON.parse(standards.to_json), args[:text]
166151
)
167152
end
168-
# rubocop:enable Metrics/BlockLength
153+
@answer.save!
154+
end
155+
156+
def update_answer(args, q, standards)
157+
authorize @answer
158+
159+
@answer.update(args.merge(user_id: current_user.id))
160+
if args[:question_option_ids].present?
161+
# Saves the record with the updated_at set to the current time.
162+
# Needed if only answer.question_options is updated
163+
@answer.touch
164+
end
165+
return unless q.question_format.rda_metadata?
166+
167+
@answer.update_answer_hash(
168+
JSON.parse(standards.to_json), args[:text]
169+
)
170+
@answer.save!
171+
end
172+
173+
def handle_stale_answer_error(args)
174+
@stale_answer = @answer
175+
@answer = Answer.find_by(
176+
plan_id: args[:plan_id],
177+
question_id: args[:question_id]
178+
)
169179
end
170180

171181
# rubocop:disable Metrics/AbcSize

0 commit comments

Comments
 (0)