From 4355138f577b2a38bcd5893c0f20c724ce4ddce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 3 Nov 2022 23:25:19 +0100 Subject: [PATCH] Simplify locking current user when voting Back in commit 36e452437, we wrote: > The `reload` method added to max_votes validation is needed because > the author gets here with some changes because of the around_action > `switch_locale`, which adds some changes to the current user record > and therefore, the lock method raises an exception when trying to lock > it requiring us to save or discard those record changes. This happened when `current_user` didn't have a locale stored in the database and the `current_locale` method returned the default locale. And the test "Poll Votation Type Multiple answers" would indeed fail if we removed the `reload` method. However, we can remove the need to reload the record by avoiding the mentioned changes on the current user record. So we're changing the `User#locale` method so it doesn't modify the user record. --- app/models/poll/answer.rb | 1 - app/models/user.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index d9d86e9d6..41d76e925 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -38,7 +38,6 @@ class Poll::Answer < ApplicationRecord def max_votes return if !question || question&.unique? || persisted? - author.reload author.lock! if question.answers.by_author(author).count >= question.max_votes diff --git a/app/models/user.rb b/app/models/user.rb index d19072fe5..6a400c5bc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -339,7 +339,7 @@ class User < ApplicationRecord end def locale - self[:locale] ||= I18n.default_locale.to_s + self[:locale] || I18n.default_locale.to_s end def confirmation_required?