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.
This commit is contained in:
Javi Martín
2022-11-03 23:25:19 +01:00
parent 827f73d22d
commit 4355138f57
2 changed files with 1 additions and 2 deletions

View File

@@ -38,7 +38,6 @@ class Poll::Answer < ApplicationRecord
def max_votes def max_votes
return if !question || question&.unique? || persisted? return if !question || question&.unique? || persisted?
author.reload
author.lock! author.lock!
if question.answers.by_author(author).count >= question.max_votes if question.answers.by_author(author).count >= question.max_votes

View File

@@ -339,7 +339,7 @@ class User < ApplicationRecord
end end
def locale def locale
self[:locale] ||= I18n.default_locale.to_s self[:locale] || I18n.default_locale.to_s
end end
def confirmation_required? def confirmation_required?