Files
nairobi/app/models/poll/web_vote.rb
Javi Martín 8deb1964bd Show errors when submitting too many answers
This could be the case when JavaScript is disabled.

Note that, in `Poll/WebVote` we're calling `given_answers` inside a
transaction. Putting this code before the transaction resulted in a test
failing sometimes, probably because of a bug that might be possible to
reproduce by doing simultaneous requests.
2025-08-14 13:06:43 +02:00

82 lines
1.8 KiB
Ruby

class Poll::WebVote
include ActiveModel::Validations
attr_reader :poll, :user
delegate :t, to: "ApplicationController.helpers"
validate :max_answers
def initialize(poll, user)
@poll = poll
@user = user
end
def questions
poll.questions.for_render.sort_for_list
end
def answers
@answers ||= questions.to_h do |question|
[question.id, question.answers.where(author: user)]
end
end
def update(params)
all_valid = true
user.with_lock do
self.answers = given_answers(params)
questions.each do |question|
question.answers.where(author: user).where.not(id: answers[question.id].map(&:id)).destroy_all
if valid? && answers[question.id].all?(&:valid?)
Poll::Voter.find_or_create_by!(user: user, poll: poll, origin: "web")
answers[question.id].each(&:save!)
else
all_valid = false
end
end
raise ActiveRecord::Rollback unless all_valid
end
all_valid
end
def to_key
end
def persisted?
Poll::Voter.where(user: user, poll: poll, origin: "web").exists?
end
private
attr_writer :answers
def given_answers(params)
questions.to_h do |question|
[question.id, answers_for_question(question, params[question.id.to_s])]
end
end
def answers_for_question(question, question_params)
return [] unless question_params
Array(question_params[:option_id]).map do |option_id|
question.find_or_initialize_user_answer(user, option_id)
end
end
def max_answers
questions.each do |question|
if answers[question.id].count > question.max_votes
errors.add(
:"question_#{question.id}",
t("polls.form.maximum_exceeded", maximum: question.max_votes, given: answers[question.id].count)
)
end
end
end
end