FIX: reset all votes when the # of options changes

This commit is contained in:
Régis Hanol 2015-05-06 18:52:09 +02:00
parent 2ad6711c62
commit 033761d2f6
4 changed files with 26 additions and 6 deletions

View File

@ -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

View File

@ -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());

View File

@ -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

View File

@ -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