adds method voted_by?(user) to polls

This commit is contained in:
rgarcia
2017-02-08 20:35:33 +01:00
committed by Javi Martín
parent 54fbae6339
commit b601f6c33f
2 changed files with 22 additions and 0 deletions

View File

@@ -90,6 +90,10 @@ class Poll < ActiveRecord::Base
Poll::Voter.where(poll: self, user: user, origin: "web").exists? Poll::Voter.where(poll: self, user: user, origin: "web").exists?
end end
def voted_by?(user)
Poll::Voter.where(poll: self, user: user).exists?
end
def date_range def date_range
unless starts_at.present? && ends_at.present? && starts_at <= ends_at unless starts_at.present? && ends_at.present? && starts_at <= ends_at
errors.add(:starts_at, I18n.t('errors.messages.invalid_date_range')) errors.add(:starts_at, I18n.t('errors.messages.invalid_date_range'))

View File

@@ -175,6 +175,24 @@ describe Poll do
end end
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 describe "#voted_in_booth?" do
it "returns true if the user has already voted in booth" do it "returns true if the user has already voted in booth" do