mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
FIX: polls with votes were preventing users from updating the post
This commit is contained in:
parent
9420424a28
commit
60bbd81607
2 changed files with 41 additions and 14 deletions
|
@ -17,6 +17,9 @@ PLUGIN_NAME ||= "discourse_poll".freeze
|
|||
POLLS_CUSTOM_FIELD ||= "polls".freeze
|
||||
VOTES_CUSTOM_FIELD ||= "polls-votes".freeze
|
||||
|
||||
DATA_PREFIX ||= "data-poll-".freeze
|
||||
DEFAULT_POLL_NAME ||= "poll".freeze
|
||||
|
||||
after_initialize do
|
||||
|
||||
# remove "Vote Now!" & "Show Results" links in emails
|
||||
|
@ -157,6 +160,7 @@ after_initialize do
|
|||
end
|
||||
|
||||
require_dependency "application_controller"
|
||||
|
||||
class DiscoursePoll::PollsController < ::ApplicationController
|
||||
requires_plugin PLUGIN_NAME
|
||||
|
||||
|
@ -217,9 +221,6 @@ after_initialize do
|
|||
end
|
||||
end
|
||||
|
||||
DATA_PREFIX ||= "data-poll-".freeze
|
||||
DEFAULT_POLL_NAME ||= "poll".freeze
|
||||
|
||||
validate(:post, :validate_polls) do
|
||||
# only care when raw has changed!
|
||||
return unless self.raw_changed?
|
||||
|
@ -285,9 +286,12 @@ after_initialize do
|
|||
# load previous polls
|
||||
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?
|
||||
if polls.keys != previous_polls.keys ||
|
||||
polls.values.map { |p| p["options"] } != previous_polls.values.map { |p| p["options"] }
|
||||
if polls.keys != previous_polls.keys || current_options != previous_options
|
||||
|
||||
# outside of the 5-minute edit window?
|
||||
if post.created_at < 5.minutes.ago
|
||||
|
|
|
@ -10,7 +10,6 @@ describe PostsController do
|
|||
|
||||
describe "polls" do
|
||||
|
||||
|
||||
it "works" do
|
||||
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
|
||||
expect(response).to be_success
|
||||
|
@ -91,7 +90,7 @@ describe PostsController do
|
|||
describe "within the first 5 minutes" 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]" }
|
||||
::JSON.parse(response.body)["id"]
|
||||
end
|
||||
|
@ -116,30 +115,54 @@ describe PostsController 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
|
||||
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"]
|
||||
end
|
||||
end
|
||||
|
||||
let(:new_raw) { "[poll]\n- A\n- C[/poll]" }
|
||||
|
||||
it "cannot be changed by OP" do
|
||||
xhr :put, :update, { id: post_id, post: { raw: new_raw } }
|
||||
it "OP cannot change the options" do
|
||||
xhr :put, :update, { id: post_id, post: { raw: new_option } }
|
||||
expect(response).not_to be_success
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.op_cannot_edit_options_after_5_minutes"))
|
||||
end
|
||||
|
||||
it "can be edited by staff" do
|
||||
it "staff can change the options" do
|
||||
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
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue