mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: polls with votes were preventing users from updating the post
This commit is contained in:
@@ -17,6 +17,9 @@ PLUGIN_NAME ||= "discourse_poll".freeze
|
|||||||
POLLS_CUSTOM_FIELD ||= "polls".freeze
|
POLLS_CUSTOM_FIELD ||= "polls".freeze
|
||||||
VOTES_CUSTOM_FIELD ||= "polls-votes".freeze
|
VOTES_CUSTOM_FIELD ||= "polls-votes".freeze
|
||||||
|
|
||||||
|
DATA_PREFIX ||= "data-poll-".freeze
|
||||||
|
DEFAULT_POLL_NAME ||= "poll".freeze
|
||||||
|
|
||||||
after_initialize do
|
after_initialize do
|
||||||
|
|
||||||
# remove "Vote Now!" & "Show Results" links in emails
|
# remove "Vote Now!" & "Show Results" links in emails
|
||||||
@@ -157,6 +160,7 @@ after_initialize do
|
|||||||
end
|
end
|
||||||
|
|
||||||
require_dependency "application_controller"
|
require_dependency "application_controller"
|
||||||
|
|
||||||
class DiscoursePoll::PollsController < ::ApplicationController
|
class DiscoursePoll::PollsController < ::ApplicationController
|
||||||
requires_plugin PLUGIN_NAME
|
requires_plugin PLUGIN_NAME
|
||||||
|
|
||||||
@@ -217,9 +221,6 @@ after_initialize do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
DATA_PREFIX ||= "data-poll-".freeze
|
|
||||||
DEFAULT_POLL_NAME ||= "poll".freeze
|
|
||||||
|
|
||||||
validate(:post, :validate_polls) do
|
validate(:post, :validate_polls) do
|
||||||
# only care when raw has changed!
|
# only care when raw has changed!
|
||||||
return unless self.raw_changed?
|
return unless self.raw_changed?
|
||||||
@@ -285,9 +286,12 @@ after_initialize do
|
|||||||
# load previous polls
|
# load previous polls
|
||||||
previous_polls = post.custom_fields[POLLS_CUSTOM_FIELD] || {}
|
previous_polls = post.custom_fields[POLLS_CUSTOM_FIELD] || {}
|
||||||
|
|
||||||
|
# extract options
|
||||||
|
current_options = polls.values.map { |p| p["options"].map { |o| o["id"] } }.flatten.sort
|
||||||
|
previous_options = previous_polls.values.map { |p| p["options"].map { |o| o["id"] } }.flatten.sort
|
||||||
|
|
||||||
# are the polls different?
|
# are the polls different?
|
||||||
if polls.keys != previous_polls.keys ||
|
if polls.keys != previous_polls.keys || current_options != previous_options
|
||||||
polls.values.map { |p| p["options"] } != previous_polls.values.map { |p| p["options"] }
|
|
||||||
|
|
||||||
# outside of the 5-minute edit window?
|
# outside of the 5-minute edit window?
|
||||||
if post.created_at < 5.minutes.ago
|
if post.created_at < 5.minutes.ago
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ describe PostsController do
|
|||||||
|
|
||||||
describe "polls" do
|
describe "polls" do
|
||||||
|
|
||||||
|
|
||||||
it "works" do
|
it "works" do
|
||||||
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
|
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
|
||||||
expect(response).to be_success
|
expect(response).to be_success
|
||||||
@@ -91,7 +90,7 @@ describe PostsController do
|
|||||||
describe "within the first 5 minutes" do
|
describe "within the first 5 minutes" do
|
||||||
|
|
||||||
let(:post_id) do
|
let(:post_id) do
|
||||||
Timecop.freeze(3.minutes.ago) do
|
Timecop.freeze(4.minutes.ago) do
|
||||||
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
|
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
|
||||||
::JSON.parse(response.body)["id"]
|
::JSON.parse(response.body)["id"]
|
||||||
end
|
end
|
||||||
@@ -116,30 +115,54 @@ describe PostsController do
|
|||||||
|
|
||||||
describe "after the first 5 minutes" do
|
describe "after the first 5 minutes" do
|
||||||
|
|
||||||
|
let(:poll) { "[poll]\n- A\n- B[/poll]" }
|
||||||
|
let(:new_option) { "[poll]\n- A\n- C[/poll]" }
|
||||||
|
let(:updated) { "before\n\n[poll]\n- A\n- B[/poll]\n\nafter" }
|
||||||
|
|
||||||
let(:post_id) do
|
let(:post_id) do
|
||||||
Timecop.freeze(6.minutes.ago) do
|
Timecop.freeze(6.minutes.ago) do
|
||||||
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
|
xhr :post, :create, { title: title, raw: poll }
|
||||||
::JSON.parse(response.body)["id"]
|
::JSON.parse(response.body)["id"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:new_raw) { "[poll]\n- A\n- C[/poll]" }
|
it "OP cannot change the options" do
|
||||||
|
xhr :put, :update, { id: post_id, post: { raw: new_option } }
|
||||||
it "cannot be changed by OP" do
|
|
||||||
xhr :put, :update, { id: post_id, post: { raw: new_raw } }
|
|
||||||
expect(response).not_to be_success
|
expect(response).not_to be_success
|
||||||
json = ::JSON.parse(response.body)
|
json = ::JSON.parse(response.body)
|
||||||
expect(json["errors"][0]).to eq(I18n.t("poll.op_cannot_edit_options_after_5_minutes"))
|
expect(json["errors"][0]).to eq(I18n.t("poll.op_cannot_edit_options_after_5_minutes"))
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can be edited by staff" do
|
it "staff can change the options" do
|
||||||
log_in_user(Fabricate(:moderator))
|
log_in_user(Fabricate(:moderator))
|
||||||
xhr :put, :update, { id: post_id, post: { raw: new_raw } }
|
xhr :put, :update, { id: post_id, post: { raw: new_option } }
|
||||||
expect(response).to be_success
|
expect(response).to be_success
|
||||||
json = ::JSON.parse(response.body)
|
json = ::JSON.parse(response.body)
|
||||||
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
|
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "support changes on the post" do
|
||||||
|
xhr :put, :update, { id: post_id, post: { raw: updated } }
|
||||||
|
expect(response).to be_success
|
||||||
|
json = ::JSON.parse(response.body)
|
||||||
|
expect(json["post"]["cooked"]).to match("before")
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "with at least one vote" do
|
||||||
|
|
||||||
|
before do
|
||||||
|
DiscoursePoll::Poll.vote(post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"], user.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "support changes on the post" do
|
||||||
|
xhr :put, :update, { id: post_id, post: { raw: updated } }
|
||||||
|
expect(response).to be_success
|
||||||
|
json = ::JSON.parse(response.body)
|
||||||
|
expect(json["post"]["cooked"]).to match("before")
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user