From 95d98ff2b630a2fb314cfdad1bef187671fd518b Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Sat, 22 Mar 2014 15:36:17 +0100 Subject: [PATCH] improved code for poll participation --- app/models/poll_participation.rb | 9 +++------ spec/models/poll_participation_spec.rb | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/app/models/poll_participation.rb b/app/models/poll_participation.rb index c666e5030..3e1f7c368 100644 --- a/app/models/poll_participation.rb +++ b/app/models/poll_participation.rb @@ -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 diff --git a/spec/models/poll_participation_spec.rb b/spec/models/poll_participation_spec.rb index c09ae06aa..951d14144 100644 --- a/spec/models/poll_participation_spec.rb +++ b/spec/models/poll_participation_spec.rb @@ -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