poll_serializer.rb 712 Bytes
Newer Older
Eugen Rochko's avatar
Eugen Rochko committed
1
2
3
4
# frozen_string_literal: true

class REST::PollSerializer < ActiveModel::Serializer
  attributes :id, :expires_at, :expired,
ThibG's avatar
ThibG committed
5
             :multiple, :votes_count, :voters_count
Eugen Rochko's avatar
Eugen Rochko committed
6

7
  has_many :loaded_options, key: :options
8
  has_many :emojis, serializer: REST::CustomEmojiSerializer
Eugen Rochko's avatar
Eugen Rochko committed
9
10

  attribute :voted, if: :current_user?
11
  attribute :own_votes, if: :current_user?
Eugen Rochko's avatar
Eugen Rochko committed
12
13
14
15
16
17
18
19
20
21

  def id
    object.id.to_s
  end

  def expired
    object.expired?
  end

  def voted
22
    object.voted?(current_user.account)
Eugen Rochko's avatar
Eugen Rochko committed
23
24
  end

25
26
27
28
  def own_votes
    object.own_votes(current_user.account)
  end

Eugen Rochko's avatar
Eugen Rochko committed
29
30
31
32
33
34
35
36
  def current_user?
    !current_user.nil?
  end

  class OptionSerializer < ActiveModel::Serializer
    attributes :title, :votes_count
  end
end