Added new methods to Poll question and Poll answers

This commit is contained in:
María Checa
2017-10-17 18:30:20 +02:00
parent bfe0addf3b
commit 610aea2c72
2 changed files with 18 additions and 0 deletions

View File

@@ -60,4 +60,8 @@ class Poll::Question < ActiveRecord::Base
where(poll_id: Poll.answerable_by(user).pluck(:id))
end
def answers_total_votes
question_answers.map { |a| Poll::Answer.where(question_id: self, answer: a.title).count }.sum
end
end

View File

@@ -31,4 +31,18 @@ class Poll::Question::Answer < ActiveRecord::Base
def self.last_position(question_id)
where(question_id: question_id).maximum('given_order') || 0
end
def total_votes
Poll::Answer.where(question_id: question, answer: title).count
end
def is_winner?
answers = question.question_answers
.map { |a| Poll::Answer.where(question_id: a.question, answer: a.title).count }
!answers.any?{ |a| a > total_votes }
end
def total_votes_percentage
((total_votes*100) / question.answers_total_votes).round(2)
end
end