Merge branch 'master' into 1786-poll_results

This commit is contained in:
María Checa
2017-10-18 17:33:16 +02:00
69 changed files with 765 additions and 432 deletions

View File

@@ -9,9 +9,8 @@ class Poll::Answer < ActiveRecord::Base
validates :author, presence: true
validates :answer, presence: true
# temporary skipping validation, review when removing valid_answers
# validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }},
# unless: ->(a) { a.question.blank? }
validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }},
unless: ->(a) { a.question.blank? }
scope :by_author, ->(author_id) { where(author_id: author_id) }
scope :by_question, ->(question_id) { where(question_id: question_id) }

View File

@@ -10,8 +10,9 @@ class Poll::PartialResult < ActiveRecord::Base
validates :question, presence: true
validates :author, presence: true
validates :answer, presence: true
validates :answer, inclusion: {in: ->(a) { a.question.valid_answers }}
validates :origin, inclusion: {in: VALID_ORIGINS}
validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }},
unless: ->(a) { a.question.blank? }
validates :origin, inclusion: { in: VALID_ORIGINS }
scope :by_author, ->(author_id) { where(author_id: author_id) }
scope :by_question, ->(question_id) { where(question_id: question_id) }

View File

@@ -39,17 +39,12 @@ class Poll::Question < ActiveRecord::Base
author_visible_name => 'C' }
end
def valid_answers
(super.try(:split, ',')&.compact || []).map(&:strip)
end
def copy_attributes_from_proposal(proposal)
if proposal.present?
self.author = proposal.author
self.author_visible_name = proposal.author.name
self.proposal_id = proposal.id
self.title = proposal.title
self.valid_answers = I18n.t('poll_questions.default_valid_answers')
end
end

125
app/models/poll/stats.rb Normal file
View File

@@ -0,0 +1,125 @@
class Poll
class Stats
def initialize(poll)
@poll = poll
end
def generate
stats = %w[total_participants total_participants_web total_web_valid total_web_white total_web_null
total_participants_booth total_booth_valid total_booth_white total_booth_null
total_valid_votes total_white_votes total_null_votes valid_percentage_web valid_percentage_booth
total_valid_percentage white_percentage_web white_percentage_booth total_white_percentage
null_percentage_web null_percentage_booth total_null_percentage total_participants_web_percentage
total_participants_booth_percentage]
stats.map { |stat_name| [stat_name.to_sym, send(stat_name)] }.to_h
end
private
def total_participants
total_participants_web + total_participants_booth
end
def total_participants_web
total_web_valid + total_web_white + total_web_null
end
def total_participants_web_percentage
(total_participants) == 0 ? 0 : total_participants_web * 100 / total_participants
end
def total_participants_booth
voters.where(origin: 'booth').count
end
def total_participants_booth_percentage
(total_participants) == 0 ? 0 : total_participants_booth * 100 / total_participants.to_f
end
def total_web_valid
voters.where(origin: 'web').count
end
def valid_percentage_web
(total_valid_votes) == 0 ? 0 : total_web_valid * 100 / total_valid_votes.to_f
end
def total_web_white
0
end
def white_percentage_web
0
end
def total_web_null
0
end
def null_percentage_web
0
end
def total_booth_valid
recounts.sum(:total_amount)
end
def valid_percentage_booth
(total_valid_votes) == 0 ? 0 : total_booth_valid * 100 / total_valid_votes.to_f
end
def total_booth_white
recounts.sum(:white_amount)
end
def white_percentage_booth
(total_white_votes) == 0 ? 0 : total_booth_white * 100 / total_white_votes.to_f
end
def total_booth_null
recounts.sum(:null_amount)
end
def null_percentage_booth
(total_null_votes == 0) ? 0 : total_booth_null * 100 / total_null_votes.to_f
end
def total_valid_votes
total_web_valid + total_booth_valid
end
def total_valid_percentage
(total_participants) == 0 ? 0 : total_valid_votes * 100 / total_participants.to_f
end
def total_white_votes
total_web_white + total_booth_white
end
def total_white_percentage
(total_participants) == 0 ? 0 : total_white_votes * 100 / total_participants.to_f
end
def total_null_votes
total_web_null + total_booth_null
end
def total_null_percentage
(total_participants) == 0 ? 0 : total_null_votes * 100 / total_participants.to_f
end
def voters
@poll.voters
end
def recounts
@poll.recounts
end
def stats_cache(key, &block)
Rails.cache.fetch("polls_stats/#{@poll.id}/#{key}/v7", &block)
end
end
end

View File

@@ -1,7 +1,7 @@
class Poll
class Voter < ActiveRecord::Base
VALID_ORIGINS = %w{ web booth }
VALID_ORIGINS = %w{web booth}.freeze
belongs_to :poll
belongs_to :user