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.
This commit is contained in:
taitus
2025-10-10 15:36:38 +02:00
parent 4e57e311dc
commit 69eaf66b93
3 changed files with 37 additions and 33 deletions

View File

@@ -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)

View File

@@ -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({})