discourse/plugins/poll/app/serializers/poll_serializer.rb
Jarek Radosz babbebfb35
FEATURE: Add the title attribute to polls (#10759)
Adds an optional title attribute to polls. The rationale for this addition is that polls themselves didn't contain context/question and relied on post body to explain them. That context wasn't always obvious (e.g. when there are multiple polls in a single post) or available (e.g. when you display the poll breakdown - you see the answers, but not the question)

As a side note, here's a word on how the poll plugin works:

> We have a markdown poll renderer, which we use in the builder UI and the composer preview, but… when you submit a post, raw markdown is cooked into html (twice), then we extract data from the generated html and save it to the database. When it's render time, we first display the cooked html poll, and then extract some data from that html, get the data from the post's JSON (and identify that poll using the extracted html stuff) to then render the poll using widgets and the JSON data.
2020-10-02 09:21:24 +02:00

69 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class PollSerializer < ApplicationSerializer
attributes :name,
:type,
:status,
:public,
:results,
:min,
:max,
:step,
:options,
:voters,
:close,
:preloaded_voters,
:chart_type,
:groups,
:title
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
def include_groups?
groups.present?
end
def options
object.poll_options.map { |o| PollOptionSerializer.new(o, root: false).as_json }
end
def voters
object.poll_votes.count('DISTINCT user_id') + object.anonymous_voters.to_i
end
def close
object.close_at
end
def include_close?
object.close_at.present?
end
def preloaded_voters
DiscoursePoll::Poll.serialized_voters(object)
end
def include_preloaded_voters?
object.can_see_voters?(scope.user)
end
end