diff --git a/plugins/poll/README.md b/plugins/poll/README.md index e62077d97..5fabf2cc0 100644 --- a/plugins/poll/README.md +++ b/plugins/poll/README.md @@ -9,7 +9,7 @@ Allows you to add a poll to the first post of a topic. ## Closing the poll -Change the start of the topic title from "Poll: " to "Closed Poll: " +Change the start of the topic title from "Poll: " to "Closed Poll: ". This feature is disabled if the `allow_user_locale` site setting is enabled. _Note: closing a topic will also close the poll._ @@ -21,12 +21,12 @@ list to be used like this: ``` Intro Text - + - Item one - Item two - + Here are your choices: - + [poll] - Option 1 - Option 2 diff --git a/plugins/poll/assets/javascripts/discourse/templates/poll.js.handlebars b/plugins/poll/assets/javascripts/discourse/templates/poll.js.handlebars index 3168dc130..360499b40 100644 --- a/plugins/poll/assets/javascripts/discourse/templates/poll.js.handlebars +++ b/plugins/poll/assets/javascripts/discourse/templates/poll.js.handlebars @@ -14,14 +14,28 @@ {{/each}} - +{{#if controller.showToggleClosePoll}} + +{{/if}} + {{#if loading}} {{/if}} diff --git a/plugins/poll/assets/javascripts/poll_ui.js b/plugins/poll/assets/javascripts/poll_ui.js index 4e4bc8a56..81ebc3767 100644 --- a/plugins/poll/assets/javascripts/poll_ui.js +++ b/plugins/poll/assets/javascripts/poll_ui.js @@ -44,8 +44,10 @@ var Poll = Discourse.Model.extend({ var PollController = Discourse.Controller.extend({ poll: null, showResults: Em.computed.oneWay('poll.closed'), - disableRadio: Em.computed.any('poll.closed', 'loading'), + showToggleClosePoll: function() { + return this.get('poll.post.topic.details.can_edit') && !Discourse.SiteSettings.allow_user_locale; + }.property('poll.post.topic.details.can_edit'), actions: { selectOption: function(option) { @@ -67,6 +69,18 @@ var PollController = Discourse.Controller.extend({ toggleShowResults: function() { this.set('showResults', !this.get('showResults')); + }, + + toggleClosePoll: function() { + this.set('loading', true); + return Discourse.ajax("/poll/toggle_close", { + type: "PUT", + data: {post_id: this.get('poll.post.id')} + }).then(function(topicJson) { + this.set('poll.post.topic.title', topicJson.basic_topic.title); + this.set('poll.post.topic.fancy_title', topicJson.basic_topic.title); + this.set('loading', false); + }.bind(this)); } } }); diff --git a/plugins/poll/config/locales/client.en.yml b/plugins/poll/config/locales/client.en.yml index 0d9ed7c52..23169bcfc 100644 --- a/plugins/poll/config/locales/client.en.yml +++ b/plugins/poll/config/locales/client.en.yml @@ -15,3 +15,6 @@ en: results: show: Show Results hide: Hide Results + + close_poll: "Close Poll" + open_poll: "Open Poll" diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index b555a2fd0..fddf82516 100644 --- a/plugins/poll/plugin.rb +++ b/plugins/poll/plugin.rb @@ -58,11 +58,45 @@ after_initialize do render json: poll.serialize(current_user) end + + def toggle_close + post = Post.find(params[:post_id]) + topic = post.topic + poll = PollPlugin::Poll.new(post) + + # Make sure the user is allowed to close the poll. + Guardian.new(current_user).ensure_can_edit!(topic) + + # Make sure this is actually a poll. + unless poll.has_poll_details? + render status: 400, json: false + return + end + + # Make sure the topic is not closed. + if topic.closed? + render status: 400, json: false + return + end + + # Modify topic title. + if topic.title =~ /^(#{I18n.t('poll.prefix').strip})\s?:/i + topic.title = topic.title.gsub(/^(#{I18n.t('poll.prefix').strip})\s?:/i, I18n.t('poll.closed_prefix') + ':') + elsif topic.title =~ /^(#{I18n.t('poll.closed_prefix').strip})\s?:/i + topic.title = topic.title.gsub(/^(#{I18n.t('poll.closed_prefix').strip})\s?:/i, I18n.t('poll.prefix') + ':') + end + + topic.acting_user = current_user + topic.save! + + render json: topic, serializer: BasicTopicSerializer + end end end PollPlugin::Engine.routes.draw do put '/' => 'poll#vote' + put '/toggle_close' => 'poll#toggle_close' end Discourse::Application.routes.append do @@ -147,4 +181,8 @@ register_css <