Welcome to mirror list, hosted at ThFree Co, Russian Federation.

poll.rb « models « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fa90335775fad549f5c4704491d77443d6df2161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Poll < ActiveRecord::Base
  include Diaspora::Federated::Base
  include Diaspora::Fields::Guid

  belongs_to :status_message
  has_many :poll_answers, -> { order 'id ASC' }
  has_many :poll_participations
  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

  self.include_root_in_json = false

  def enough_poll_answers
    errors.add(:poll_answers, I18n.t("activerecord.errors.models.poll.attributes.poll_answers.not_enough_poll_answers")) if poll_answers.size < 2
  end

  def as_json(options={})
    {
      :poll_id => self.id,
      :post_id => self.status_message.id,
      :question => self.question,
      :poll_answers => self.poll_answers,
      :participation_count => self.participation_count,
    }
  end

  def participation_count
    poll_answers.sum("vote_count")
  end

  def already_participated?(user)
    poll_participations.where(:author_id => user.person.id).present?
  end
end