From 69eaf66b9310912f255d270e8c92300c60ae52a6 Mon Sep 17 00:00:00 2001 From: taitus Date: Fri, 10 Oct 2025 15:36:38 +0200 Subject: [PATCH] Remove redundant `max_votes` validation from Poll::Answer Since commit 8deb1964b, the `WebVote` class enforces the maximum vote validation, making the `max_votes` method in `Poll::Answer` redundant. --- app/models/poll/answer.rb | 12 ---------- spec/models/poll/answer_spec.rb | 21 ------------------ spec/models/poll/web_vote_spec.rb | 37 +++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index 7663aa42b..664e2f426 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -9,21 +9,9 @@ class Poll::Answer < ApplicationRecord validates :author, presence: true validates :answer, presence: true validates :option, uniqueness: { scope: :author_id }, allow_nil: true - validate :max_votes - validates :answer, inclusion: { in: ->(poll_answer) { poll_answer.option.possible_answers }}, if: ->(poll_answer) { poll_answer.option.present? } scope :by_author, ->(author_id) { where(author_id: author_id) } scope :by_question, ->(question_id) { where(question_id: question_id) } - - private - - def max_votes - return if !question || !author || persisted? - - if question.answers.by_author(author).count >= question.max_votes - errors.add(:answer, "Maximum number of votes per user exceeded") - end - end end diff --git a/spec/models/poll/answer_spec.rb b/spec/models/poll/answer_spec.rb index bc5c50402..e72fd5da6 100644 --- a/spec/models/poll/answer_spec.rb +++ b/spec/models/poll/answer_spec.rb @@ -37,27 +37,6 @@ describe Poll::Answer do expect(answer).not_to be_valid end - it "is not valid if there's already an answer to that question" do - author = create(:user) - question = create(:poll_question, :yes_no) - - create(:poll_answer, author: author, question: question) - - answer = build(:poll_answer, author: author, question: question) - - expect(answer).not_to be_valid - end - - it "is not valid when user already reached multiple answers question max votes" do - author = create(:user) - question = create(:poll_question_multiple, :abc, max_votes: 2) - create(:poll_answer, author: author, question: question, answer: "Answer A") - create(:poll_answer, author: author, question: question, answer: "Answer B") - answer = build(:poll_answer, author: author, question: question, answer: "Answer C") - - expect(answer).not_to be_valid - end - it "is not valid when there are two identical answers" do author = create(:user) question = create(:poll_question_multiple, :abc) diff --git a/spec/models/poll/web_vote_spec.rb b/spec/models/poll/web_vote_spec.rb index aaa235597..d8c1caa5b 100644 --- a/spec/models/poll/web_vote_spec.rb +++ b/spec/models/poll/web_vote_spec.rb @@ -44,6 +44,21 @@ describe Poll::WebVote do expect(voter.poll_id).to eq answer.poll.id end + it "updates existing multiple options instead of adding new ones" do + question = create(:poll_question_multiple, :abc, poll: poll, max_votes: 2) + option_a = question.question_options.find_by(title: "Answer A") + option_b = question.question_options.find_by(title: "Answer B") + option_c = question.question_options.find_by(title: "Answer C") + + create(:poll_answer, author: user, question: question, option: option_a) + create(:poll_answer, author: user, question: question, option: option_b) + + web_vote.update(question.id.to_s => { option_id: [option_c.id.to_s] }) + + expect(question.reload.answers.size).to eq 1 + expect(question.reload.answers.first.option).to eq option_c + end + it "does not save the answer if the voter is invalid" do allow_any_instance_of(Poll::Voter).to receive(:valid?).and_return(false) @@ -55,6 +70,28 @@ describe Poll::WebVote do expect(question.answers).to be_blank end + it "does not save the answer if it exceeds the allowed max votes" do + question = create(:poll_question_multiple, :abc, poll: poll, max_votes: 2) + + result = web_vote.update(question.id.to_s => { option_id: question.question_options.ids.map(&:to_s) }) + + expect(result).to be false + expect(poll.voters).to be_blank + expect(question.answers).to be_blank + end + + it "does not save the answer if unique question receives multiple options" do + question = create(:poll_question, :yes_no, poll: poll) + + result = web_vote.update( + question.id.to_s => { option_id: question.question_options.ids.map(&:to_s) } + ) + + expect(result).to be false + expect(poll.voters).to be_blank + expect(question.answers).to be_blank + end + it "creates a voter but does not create answers when leaving everything blank" do web_vote.update({})