diff --git a/plugins/poll/app/controllers/polls_controller.rb b/plugins/poll/app/controllers/polls_controller.rb index 154d12402f3..1a7a29135eb 100644 --- a/plugins/poll/app/controllers/polls_controller.rb +++ b/plugins/poll/app/controllers/polls_controller.rb @@ -61,18 +61,29 @@ class DiscoursePoll::PollsController < ::ApplicationController poll_name = params.require(:poll_name) user_field_name = params.require(:user_field_name) - begin + poll = Poll.find_by(post_id: post_id, name: poll_name) + + if poll.nil? + render json: { error: I18n.t("poll.errors.poll_not_found") }, status: :not_found + elsif poll.ranked_choice? render json: { - grouped_results: - DiscoursePoll::Poll.grouped_poll_results( - current_user, - post_id, - poll_name, - user_field_name, - ), - } - rescue DiscoursePoll::Error => e - render_json_error e.message + error: I18n.t("poll.ranked_choice.no_group_results_support"), + }, + status: :unprocessable_entity + else + begin + render json: { + grouped_results: + DiscoursePoll::Poll.grouped_poll_results( + current_user, + post_id, + poll_name, + user_field_name, + ), + } + rescue DiscoursePoll::Error => e + render_json_error e.message + end end end end diff --git a/plugins/poll/app/models/poll.rb b/plugins/poll/app/models/poll.rb index 922e398dbab..e04e6b9b0d9 100644 --- a/plugins/poll/app/models/poll.rb +++ b/plugins/poll/app/models/poll.rb @@ -9,7 +9,7 @@ class Poll < ActiveRecord::Base has_many :poll_options, -> { order(:id) }, dependent: :destroy has_many :poll_votes - enum type: { regular: 0, multiple: 1, number: 2 }, _scopes: false + enum type: { regular: 0, multiple: 1, number: 2, ranked_choice: 3 }, _scopes: false enum status: { open: 0, closed: 1 }, _scopes: false @@ -43,6 +43,10 @@ class Poll < ActiveRecord::Base def can_see_voters?(user) everyone? && can_see_results?(user) end + + def ranked_choice? + type == "ranked_choice" + end end # == Schema Information diff --git a/plugins/poll/app/serializers/poll_serializer.rb b/plugins/poll/app/serializers/poll_serializer.rb index cb4272f7aa3..0e8c98071e4 100644 --- a/plugins/poll/app/serializers/poll_serializer.rb +++ b/plugins/poll/app/serializers/poll_serializer.rb @@ -16,7 +16,8 @@ class PollSerializer < ApplicationSerializer :preloaded_voters, :chart_type, :groups, - :title + :title, + :ranked_choice_outcome def public true @@ -75,4 +76,12 @@ class PollSerializer < ApplicationSerializer def include_preloaded_voters? object.can_see_voters?(scope.user) end + + def include_ranked_choice_outcome? + object.ranked_choice? + end + + def ranked_choice_outcome + DiscoursePoll::RankedChoice.outcome(object.id) + end end diff --git a/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.hbs b/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.hbs index 3768ad21668..038cad9e745 100644 --- a/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.hbs +++ b/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.hbs @@ -7,35 +7,51 @@ <:body>
@@ -103,7 +119,7 @@ {{/unless}} - {{#unless this.isRegular}} + {{#unless this.rankedChoiceOrRegular}}