diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 9e393394b..dcbe1c613 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -89,7 +89,7 @@ class Poll::Question < ApplicationRecord when "unique", nil { author: user } when "multiple" - { author: user, answer: option.title } + { author: user, option: option } end end end diff --git a/spec/models/poll/question_spec.rb b/spec/models/poll/question_spec.rb index ddd57aa58..320debbdb 100644 --- a/spec/models/poll/question_spec.rb +++ b/spec/models/poll/question_spec.rb @@ -86,4 +86,68 @@ RSpec.describe Poll::Question do expect(question.options_total_votes).to eq 3 end end + + describe "#find_or_initialize_user_answer" do + let(:user) { create(:user) } + let(:other_user) { create(:user) } + + context "unique question" do + let(:question) { create(:poll_question_unique, :abc) } + let(:answer_a) { question.question_options.find_by(title: "Answer A") } + let(:answer_b) { question.question_options.find_by(title: "Answer B") } + + it "finds the existing answer for the same user" do + existing_answer = create(:poll_answer, question: question, author: user, option: answer_a) + create(:poll_answer, question: question, author: other_user, option: answer_b) + + answer = question.find_or_initialize_user_answer(user, answer_b.id) + + expect(answer).to eq existing_answer + expect(answer.author).to eq user + expect(answer.option).to eq answer_b + expect(answer.answer).to eq "Answer B" + end + + it "initializes a new answer when only another user has answered" do + create(:poll_answer, question: question, author: other_user, option: answer_a) + + answer = question.find_or_initialize_user_answer(user, answer_a.id) + + expect(answer).to be_new_record + expect(answer.author).to eq user + expect(answer.option).to eq answer_a + expect(answer.answer).to eq "Answer A" + end + end + + context "multiple question" do + let(:question) { create(:poll_question_multiple, :abc, max_votes: 3) } + let(:answer_a) { question.question_options.find_by(title: "Answer A") } + let(:answer_b) { question.question_options.find_by(title: "Answer B") } + + it "finds the existing answer for the same user and option" do + existing_answer = create(:poll_answer, question: question, author: user, option: answer_a) + create(:poll_answer, question: question, author: other_user, option: answer_a) + + answer = question.find_or_initialize_user_answer(user, answer_a.id) + + expect(answer).to eq existing_answer + expect(answer.author).to eq user + expect(answer.option).to eq answer_a + expect(answer.answer).to eq "Answer A" + end + + it "initializes a new answer when selecting a different option" do + create(:poll_answer, question: question, author: user, option: answer_a) + create(:poll_answer, question: question, author: other_user, option: answer_b) + + answer = question.find_or_initialize_user_answer(user, answer_b.id) + + expect(answer).to be_new_record + expect(answer.author).to eq user + expect(answer.option).to eq answer_b + expect(answer.answer).to eq "Answer B" + end + end + end end