mirror of
https://github.com/discourse/discourse.git
synced 2025-03-09 14:34:35 +00:00
Merge pull request #4658 from tgxworld/fix_editing_poll_options_with_votes
FIX: Votes lost when editing a poll option with votes.
This commit is contained in:
commit
74d73f9e06
@ -64,7 +64,10 @@ module DiscoursePoll
|
|||||||
end
|
end
|
||||||
|
|
||||||
polls[poll_name]["voters"] = previous_polls[poll_name]["voters"]
|
polls[poll_name]["voters"] = previous_polls[poll_name]["voters"]
|
||||||
polls[poll_name]["anonymous_voters"] = previous_polls[poll_name]["anonymous_voters"] if previous_polls[poll_name].has_key?("anonymous_voters")
|
|
||||||
|
if previous_polls[poll_name].has_key?("anonymous_voters")
|
||||||
|
polls[poll_name]["anonymous_voters"] = previous_polls[poll_name]["anonymous_voters"]
|
||||||
|
end
|
||||||
|
|
||||||
previous_options = previous_polls[poll_name]["options"]
|
previous_options = previous_polls[poll_name]["options"]
|
||||||
public_poll = polls[poll_name]["public"] == "true"
|
public_poll = polls[poll_name]["public"] == "true"
|
||||||
@ -72,7 +75,19 @@ module DiscoursePoll
|
|||||||
polls[poll_name]["options"].each_with_index do |option, index|
|
polls[poll_name]["options"].each_with_index do |option, index|
|
||||||
previous_option = previous_options[index]
|
previous_option = previous_options[index]
|
||||||
option["votes"] = previous_option["votes"]
|
option["votes"] = previous_option["votes"]
|
||||||
option["anonymous_votes"] = previous_option["anonymous_votes"] if previous_option.has_key?("anonymous_votes")
|
|
||||||
|
if previous_option["id"] != option["id"]
|
||||||
|
if votes_fields = post.custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD]
|
||||||
|
votes_fields.each do |key, value|
|
||||||
|
index = value[poll_name].index(previous_option["id"])
|
||||||
|
votes_fields[key][poll_name][index] = option["id"] if index
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if previous_option.has_key?("anonymous_votes")
|
||||||
|
option["anonymous_votes"] = previous_option["anonymous_votes"]
|
||||||
|
end
|
||||||
|
|
||||||
if public_poll && previous_option.has_key?("voter_ids")
|
if public_poll && previous_option.has_key?("voter_ids")
|
||||||
option["voter_ids"] = previous_option["voter_ids"]
|
option["voter_ids"] = previous_option["voter_ids"]
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe DiscoursePoll::PollsUpdater do
|
describe DiscoursePoll::PollsUpdater do
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
let(:post_with_two_polls) do
|
let(:post_with_two_polls) do
|
||||||
raw = <<-RAW.strip_heredoc
|
raw = <<-RAW.strip_heredoc
|
||||||
[poll]
|
[poll]
|
||||||
@ -127,8 +129,6 @@ describe DiscoursePoll::PollsUpdater do
|
|||||||
DiscoursePoll::PollsValidator.new(Fabricate(:post, raw: raw)).validate_polls
|
DiscoursePoll::PollsValidator.new(Fabricate(:post, raw: raw)).validate_polls
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:user) { Fabricate(:user) }
|
|
||||||
|
|
||||||
before do
|
before do
|
||||||
DiscoursePoll::Poll.vote(post.id, "poll", ["5c24fc1df56d764b550ceae1b9319125"], user)
|
DiscoursePoll::Poll.vote(post.id, "poll", ["5c24fc1df56d764b550ceae1b9319125"], user)
|
||||||
post.reload
|
post.reload
|
||||||
@ -222,9 +222,16 @@ describe DiscoursePoll::PollsUpdater do
|
|||||||
let(:another_post) { Fabricate(:post, created_at: Time.zone.now - poll_edit_window_mins.minutes) }
|
let(:another_post) { Fabricate(:post, created_at: Time.zone.now - poll_edit_window_mins.minutes) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
polls.each { |key, value| value["voters"] = 2 }
|
|
||||||
described_class.update(another_post, polls)
|
described_class.update(another_post, polls)
|
||||||
|
another_post.reload
|
||||||
SiteSetting.poll_edit_window_mins = poll_edit_window_mins
|
SiteSetting.poll_edit_window_mins = poll_edit_window_mins
|
||||||
|
|
||||||
|
DiscoursePoll::Poll.vote(
|
||||||
|
another_post.id,
|
||||||
|
"poll",
|
||||||
|
[polls["poll"]["options"].first["id"]],
|
||||||
|
user
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not allow new polls to be added" do
|
it "should not allow new polls to be added" do
|
||||||
@ -254,6 +261,8 @@ describe DiscoursePoll::PollsUpdater do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "staff" do
|
context "staff" do
|
||||||
|
let(:another_user) { Fabricate(:user) }
|
||||||
|
|
||||||
it "should not allow staff to add options if votes have been casted" do
|
it "should not allow staff to add options if votes have been casted" do
|
||||||
another_post.update_attributes!(last_editor_id: User.staff.first.id)
|
another_post.update_attributes!(last_editor_id: User.staff.first.id)
|
||||||
|
|
||||||
@ -284,8 +293,15 @@ describe DiscoursePoll::PollsUpdater do
|
|||||||
expect(message.data[:polls]).to eq(polls_with_3_options)
|
expect(message.data[:polls]).to eq(polls_with_3_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should allow staff to edit options if votes have been casted" do
|
it "should allow staff to edit options even if votes have been casted" do
|
||||||
another_post.update_attributes!(last_editor_id: User.staff.first.id)
|
another_post.update!(last_editor_id: User.staff.first.id)
|
||||||
|
|
||||||
|
DiscoursePoll::Poll.vote(
|
||||||
|
another_post.id,
|
||||||
|
"poll",
|
||||||
|
[polls["poll"]["options"].first["id"]],
|
||||||
|
another_user
|
||||||
|
)
|
||||||
|
|
||||||
raw = <<-RAW.strip_heredoc
|
raw = <<-RAW.strip_heredoc
|
||||||
[poll]
|
[poll]
|
||||||
@ -301,9 +317,16 @@ describe DiscoursePoll::PollsUpdater do
|
|||||||
described_class.update(another_post, different_polls)
|
described_class.update(another_post, different_polls)
|
||||||
end.first
|
end.first
|
||||||
|
|
||||||
different_polls.each { |key, value| value["voters"] = 2 }
|
custom_fields = another_post.reload.custom_fields
|
||||||
|
|
||||||
|
expect(custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD])
|
||||||
|
.to eq(different_polls)
|
||||||
|
|
||||||
|
[user, another_user].each do |u|
|
||||||
|
expect(custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD][u.id.to_s]["poll"])
|
||||||
|
.to eq(["68b434ff88aeae7054e42cd05a4d9056"])
|
||||||
|
end
|
||||||
|
|
||||||
expect(another_post.reload.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD]).to eq(different_polls)
|
|
||||||
expect(message.data[:post_id]).to eq(another_post.id)
|
expect(message.data[:post_id]).to eq(another_post.id)
|
||||||
expect(message.data[:polls]).to eq(different_polls)
|
expect(message.data[:polls]).to eq(different_polls)
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user