2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
class PollSerializer < ApplicationSerializer
|
|
|
|
attributes :name,
|
|
|
|
:type,
|
|
|
|
:status,
|
|
|
|
:public,
|
|
|
|
:results,
|
|
|
|
:min,
|
|
|
|
:max,
|
|
|
|
:step,
|
|
|
|
:options,
|
|
|
|
:voters,
|
2019-02-27 11:00:21 -05:00
|
|
|
:close,
|
2019-11-25 12:51:01 -05:00
|
|
|
:preloaded_voters,
|
2020-01-28 07:30:04 -05:00
|
|
|
:chart_type,
|
2020-10-02 03:21:24 -04:00
|
|
|
:groups,
|
|
|
|
:title
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
def public
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_public?
|
|
|
|
object.everyone?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_min?
|
|
|
|
object.min.present? && (object.number? || object.multiple?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_max?
|
|
|
|
object.max.present? && (object.number? || object.multiple?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_step?
|
|
|
|
object.step.present? && object.number?
|
|
|
|
end
|
|
|
|
|
2020-01-28 07:30:04 -05:00
|
|
|
def include_groups?
|
|
|
|
groups.present?
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
def options
|
|
|
|
object.poll_options.map { |o| PollOptionSerializer.new(o, root: false).as_json }
|
|
|
|
end
|
|
|
|
|
|
|
|
def voters
|
2020-09-05 11:14:45 -04:00
|
|
|
object.poll_votes.count('DISTINCT user_id') + object.anonymous_voters.to_i
|
2018-11-19 08:50:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
object.close_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_close?
|
|
|
|
object.close_at.present?
|
|
|
|
end
|
|
|
|
|
2019-02-27 11:00:21 -05:00
|
|
|
def preloaded_voters
|
|
|
|
DiscoursePoll::Poll.serialized_voters(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_preloaded_voters?
|
2020-03-20 14:29:00 -04:00
|
|
|
object.can_see_voters?(scope.user)
|
2019-02-27 11:00:21 -05:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
end
|