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:
@@ -9,21 +9,9 @@ class Poll::Answer < ApplicationRecord
|
|||||||
validates :author, presence: true
|
validates :author, presence: true
|
||||||
validates :answer, presence: true
|
validates :answer, presence: true
|
||||||
validates :option, uniqueness: { scope: :author_id }, allow_nil: true
|
validates :option, uniqueness: { scope: :author_id }, allow_nil: true
|
||||||
validate :max_votes
|
|
||||||
|
|
||||||
validates :answer, inclusion: { in: ->(poll_answer) { poll_answer.option.possible_answers }},
|
validates :answer, inclusion: { in: ->(poll_answer) { poll_answer.option.possible_answers }},
|
||||||
if: ->(poll_answer) { poll_answer.option.present? }
|
if: ->(poll_answer) { poll_answer.option.present? }
|
||||||
|
|
||||||
scope :by_author, ->(author_id) { where(author_id: author_id) }
|
scope :by_author, ->(author_id) { where(author_id: author_id) }
|
||||||
scope :by_question, ->(question_id) { where(question_id: question_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
|
end
|
||||||
|
|||||||
@@ -37,27 +37,6 @@ describe Poll::Answer do
|
|||||||
expect(answer).not_to be_valid
|
expect(answer).not_to be_valid
|
||||||
end
|
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
|
it "is not valid when there are two identical answers" do
|
||||||
author = create(:user)
|
author = create(:user)
|
||||||
question = create(:poll_question_multiple, :abc)
|
question = create(:poll_question_multiple, :abc)
|
||||||
|
|||||||
@@ -44,6 +44,21 @@ describe Poll::WebVote do
|
|||||||
expect(voter.poll_id).to eq answer.poll.id
|
expect(voter.poll_id).to eq answer.poll.id
|
||||||
end
|
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
|
it "does not save the answer if the voter is invalid" do
|
||||||
allow_any_instance_of(Poll::Voter).to receive(:valid?).and_return(false)
|
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
|
expect(question.answers).to be_blank
|
||||||
end
|
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
|
it "creates a voter but does not create answers when leaving everything blank" do
|
||||||
web_vote.update({})
|
web_vote.update({})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user