2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
class PollSerializer < ApplicationSerializer
|
2024-07-04 19:34:48 +08:00
|
|
|
attributes :id,
|
|
|
|
:name,
|
2018-11-19 21:50:00 +08:00
|
|
|
:type,
|
|
|
|
:status,
|
|
|
|
:public,
|
|
|
|
:results,
|
|
|
|
:min,
|
|
|
|
:max,
|
|
|
|
:step,
|
|
|
|
:options,
|
|
|
|
:voters,
|
2019-02-28 00:00:21 +08:00
|
|
|
:close,
|
2019-11-26 01:51:01 +08:00
|
|
|
:preloaded_voters,
|
2020-01-28 20:30:04 +08:00
|
|
|
:chart_type,
|
2020-10-02 15:21:24 +08:00
|
|
|
:groups,
|
2024-07-17 17:49:14 +08:00
|
|
|
:title,
|
|
|
|
:ranked_choice_outcome
|
2018-11-19 21:50:00 +08: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 20:30:04 +08:00
|
|
|
def include_groups?
|
|
|
|
groups.present?
|
|
|
|
end
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
def options
|
2020-11-25 05:19:06 +08:00
|
|
|
can_see_results = object.can_see_results?(scope.user)
|
|
|
|
|
|
|
|
object.poll_options.map do |option|
|
|
|
|
PollOptionSerializer.new(
|
|
|
|
option,
|
|
|
|
root: false,
|
|
|
|
scope: {
|
|
|
|
can_see_results: can_see_results,
|
|
|
|
},
|
|
|
|
).as_json
|
|
|
|
end
|
2018-11-19 21:50:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def voters
|
2024-09-10 23:41:08 +08:00
|
|
|
object.voters_count + object.anonymous_voters.to_i
|
2018-11-19 21:50:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
object.close_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_close?
|
|
|
|
object.close_at.present?
|
|
|
|
end
|
|
|
|
|
2019-02-28 00:00:21 +08:00
|
|
|
def preloaded_voters
|
|
|
|
DiscoursePoll::Poll.serialized_voters(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_preloaded_voters?
|
2020-03-21 02:29:00 +08:00
|
|
|
object.can_see_voters?(scope.user)
|
2019-02-28 00:00:21 +08:00
|
|
|
end
|
2024-07-17 17:49:14 +08:00
|
|
|
|
|
|
|
def include_ranked_choice_outcome?
|
|
|
|
object.ranked_choice?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ranked_choice_outcome
|
|
|
|
DiscoursePoll::RankedChoice.outcome(object.id)
|
|
|
|
end
|
2018-11-19 21:50:00 +08:00
|
|
|
end
|