Skip to content
Extraits de code Groupes Projets
poll.rb 1,29 ko
Newer Older
# frozen_string_literal: true

Benjamin Neff's avatar
Benjamin Neff a validé
class Poll < ApplicationRecord
Benjamin Neff's avatar
Benjamin Neff a validé
  include Diaspora::Fields::Guid
  belongs_to :status_message
  has_many :poll_answers, -> { order "id ASC" }, dependent: :destroy
  has_many :poll_participations, dependent: :destroy
  has_one :author, through: :status_message
  #forward some requests to status message, because a poll is just attached to a status message and is not sharable itself
  delegate :author_id, :diaspora_handle, :public?, :subscribers, to: :status_message
  validate :enough_poll_answers
  validates :question, presence: true
  scope :all_public, -> { joins(:status_message).where(posts: {public: true}) }

  self.include_root_in_json = false

    errors.add(:poll_answers, I18n.t("activerecord.errors.models.poll.attributes.poll_answers.not_enough_poll_answers")) if poll_answers.size < 2
Rete2's avatar
Rete2 a validé
      poll_id:             id,
      post_id:             status_message.id,
      question:            question,
      poll_answers:        poll_answers,
      participation_count: participation_count
Rete2's avatar
Rete2 a validé
  def participation_answer(user)
    poll_participations.find_by(author_id: user.person.id)
Rete2's avatar
Rete2 a validé
  def participation_count
    poll_answers.sum("vote_count")