From 033761d2f629f45d8409ce7c5a547ff30ebb3543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 6 May 2015 18:52:09 +0200 Subject: [PATCH] FIX: reset all votes when the # of options changes --- app/models/concerns/has_custom_fields.rb | 6 +++++- .../javascripts/initializers/extend-for-poll.js.es6 | 6 +++--- plugins/poll/plugin.rb | 12 ++++++++++-- .../poll/spec/controllers/posts_controller_spec.rb | 8 ++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rb index 0b2f45488..b97e6cf3f 100644 --- a/app/models/concerns/has_custom_fields.rb +++ b/app/models/concerns/has_custom_fields.rb @@ -76,9 +76,13 @@ module HasCustomFields end def reload(options = nil) + clear_custom_fields + super + end + + def clear_custom_fields @custom_fields = nil @custom_fields_orig = nil - super end def custom_fields diff --git a/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6 b/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6 index 1153308eb..e9ff8d09f 100644 --- a/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6 +++ b/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6 @@ -27,7 +27,7 @@ export default { messageBus.subscribe("/polls", data => { const post = container.lookup("controller:topic").get("postStream").findLoadedPost(data.post_id); // HACK to trigger the "postViewUpdated" event - post.set("cooked", post.get("cooked") + " "); + Em.run.next(_ => post.set("cooked", post.get("cooked") + " ")); }); // overwrite polls @@ -58,7 +58,7 @@ export default { pollViews[pollName] = pollView; }); - this.messageBus.subscribe("/polls/" + this.get("post.id"), results => { + messageBus.subscribe("/polls/" + this.get("post.id"), results => { if (results && results.polls) { _.forEach(results.polls, poll => { if (pollViews[poll.name]) { @@ -72,7 +72,7 @@ export default { }.on("postViewInserted", "postViewUpdated"), _cleanUpPollViews: function() { - this.messageBus.unsubscribe("/polls/" + this.get("post.id")); + messageBus.unsubscribe("/polls/" + this.get("post.id")); if (this.get("pollViews")) { _.forEach(this.get("pollViews"), v => v.destroy()); diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index 3155d20b2..e623fdb37 100644 --- a/plugins/poll/plugin.rb +++ b/plugins/poll/plugin.rb @@ -284,10 +284,18 @@ after_initialize do end end - # merge votes when same number of options + # try to merge votes polls.each_key do |poll_name| next unless previous_polls.has_key?(poll_name) - next unless polls[poll_name]["options"].size == previous_polls[poll_name]["options"].size + + # when the # of options has changed, reset all the votes + if polls[poll_name]["options"].size != previous_polls[poll_name]["options"].size + PostCustomField.where(post_id: post.id) + .where("name LIKE '#{VOTES_CUSTOM_FIELD}-%'") + .destroy_all + post.clear_custom_fields + next + end polls[poll_name]["voters"] = previous_polls[poll_name]["voters"] for o in 0...polls[poll_name]["options"].size diff --git a/plugins/poll/spec/controllers/posts_controller_spec.rb b/plugins/poll/spec/controllers/posts_controller_spec.rb index e37c4d484..086258653 100644 --- a/plugins/poll/spec/controllers/posts_controller_spec.rb +++ b/plugins/poll/spec/controllers/posts_controller_spec.rb @@ -67,6 +67,14 @@ describe PostsController do expect(json["post"]["polls"]["poll"]["options"][2]["html"]).to eq("C") end + it "resets the votes" do + DiscoursePoll::Poll.vote(post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"], user.id) + xhr :put, :update, { id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" } } + expect(response).to be_success + json = ::JSON.parse(response.body) + expect(json["post"]["polls_votes"]).to_not be + end + end describe "after the first 5 minutes" do