Add option_id to partial results and unique index

Similar to what we did in PR "Avoid duplicate records in poll answers" 5539,
specifically in commit 503369166, we want to stop relying on the plain text
"answer" and start using "option_id" to avoid issues with counts across
translations and to add consistency to the poll_partial_results table.

Note that we also moved the `possible_answers` method from Poll::Question to
Poll::Question::Option, since the list of valid answers really comes from the
options of a question and not from the question itself. Tests were updated
to validate answers against the translations of the assigned option.

Additionally, we renamed lambda parameters in validations to improve clarity.
This commit is contained in:
taitus
2025-09-10 12:22:08 +02:00
parent f2153f2b4d
commit a29eeaf2e2
13 changed files with 137 additions and 51 deletions

View File

@@ -3,11 +3,8 @@ require "rails_helper"
describe Admin::Poll::Results::QuestionComponent do
let(:poll) { create(:poll) }
let(:question) { create(:poll_question, poll: poll, title: "What do you want?") }
before do
create(:poll_question_option, question: question, title: "Yes")
create(:poll_question_option, question: question, title: "No")
end
let!(:option_yes) { create(:poll_question_option, question: question, title: "Yes") }
let!(:option_no) { create(:poll_question_option, question: question, title: "No") }
it "renders question title and headers" do
render_inline Admin::Poll::Results::QuestionComponent.new(question, poll.partial_results)
@@ -25,10 +22,10 @@ describe Admin::Poll::Results::QuestionComponent do
expect(page).to have_css "td", text: "No"
end
it "sums votes by answer title" do
create(:poll_partial_result, question: question, answer: "Yes", amount: 2)
create(:poll_partial_result, question: question, answer: "Yes", amount: 1)
create(:poll_partial_result, question: question, answer: "No", amount: 5)
it "sums votes by option" do
create(:poll_partial_result, question: question, option: option_yes, amount: 2, date: Date.current)
create(:poll_partial_result, question: question, option: option_yes, amount: 1, date: Date.yesterday)
create(:poll_partial_result, question: question, option: option_no, amount: 5)
render_inline Admin::Poll::Results::QuestionComponent.new(question, question.partial_results)