improved code for poll participation

This commit is contained in:
Jannik Streek 2014-03-22 15:36:17 +01:00
parent c02414c36d
commit 95d98ff2b6
2 changed files with 5 additions and 8 deletions

View file

@ -46,17 +46,14 @@ class PollParticipation < ActiveRecord::Base
end
def not_already_participated
if self.poll == nil
return
end
return if poll.nil?
existing = PollParticipation.where(author_id: self.author.id, poll_id: self.poll.id)
if existing.first != self and existing.count != 0
other_participations = PollParticipation.where(author_id: self.author.id, poll_id: self.poll.id).to_a-[self]
if other_participations.present?
self.errors.add(:poll, I18n.t("activerecord.errors.models.poll_participations.attributes.poll.already_participated"))
end
end
class Generator < Federated::Generator
def self.federated_class
PollParticipation

View file

@ -13,7 +13,7 @@ describe PollParticipation do
end
describe 'validation' do
it 'does forbid multiple participations in the same poll' do
it 'forbids multiple participations in the same poll' do
expect {
2.times do |run|
bob.participate_in_poll!(@status, @poll.poll_answers.first)
@ -21,7 +21,7 @@ describe PollParticipation do
}.to raise_error
end
it 'does allow a one time participation in a poll' do
it 'allows a one time participation in a poll' do
expect {
bob.participate_in_poll!(@status, @poll.poll_answers.first)
}.to_not raise_error