diff --git a/app/models/poll.rb b/app/models/poll.rb index 38c1a7d56..35b888c90 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -90,6 +90,10 @@ class Poll < ActiveRecord::Base Poll::Voter.where(poll: self, user: user, origin: "web").exists? end + def voted_by?(user) + Poll::Voter.where(poll: self, user: user).exists? + end + def date_range unless starts_at.present? && ends_at.present? && starts_at <= ends_at errors.add(:starts_at, I18n.t('errors.messages.invalid_date_range')) diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index d9ebaa4cd..f3c114155 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -175,6 +175,24 @@ describe Poll do end end + describe "#voted_by?" do + it "return false if the user has not voted for this poll" do + user = create(:user, :level_two) + poll = create(:poll) + + expect(poll.voted_by?(user)).to eq(false) + end + + it "returns true if the user has voted for this poll" do + user = create(:user, :level_two) + poll = create(:poll) + + voter = create(:poll_voter, user: user, poll: poll) + + expect(poll.voted_by?(user)).to eq(true) + end + end + describe "#voted_in_booth?" do it "returns true if the user has already voted in booth" do