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:
@@ -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
|
||||
|
||||
@@ -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?
|
||||
|
||||
Reference in New Issue
Block a user