Added new column most_voted to Poll::Question::Answers
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user