Added new column most_voted to Poll::Question::Answers

This commit is contained in:
María Checa
2017-10-19 12:17:20 +02:00
parent 68341e5a76
commit 6e680c187f
3 changed files with 58 additions and 10 deletions

View File

@@ -13,6 +13,7 @@ class Poll::Question::Answer < ActiveRecord::Base
validates :given_order, presence: true, uniqueness: { scope: :question_id }
before_validation :set_order, on: :create
before_save :most_voted
def description
super.try :html_safe
@@ -36,13 +37,15 @@ class Poll::Question::Answer < ActiveRecord::Base
Poll::Answer.where(question_id: question, answer: title).count
end
def is_winner?
def most_voted
answers = question.question_answers
.map { |a| Poll::Answer.where(question_id: a.question, answer: a.title).count }
!answers.any?{ |a| a > total_votes }
most_voted = !answers.any?{ |a| a > total_votes }
self.update_attributes(most_voted: most_voted)
end
def total_votes_percentage
((total_votes*100) / question.answers_total_votes).round(2)
((total_votes*100) / question.answers_total_votes).round(2) rescue 0
end
end