FIX: polls are always editable until the first vote

This commit is contained in:
Régis Hanol 2015-09-25 18:54:15 +02:00
parent 46bef4fad0
commit 2e76c9165a
2 changed files with 41 additions and 20 deletions

View file

@ -293,8 +293,10 @@ after_initialize do
# are the polls different? # are the polls different?
if polls.keys != previous_polls.keys || current_options != previous_options if polls.keys != previous_polls.keys || current_options != previous_options
has_votes = previous_polls.keys.map { |p| previous_polls[p]["voters"].to_i }.sum > 0
# 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 && has_votes
# cannot add/remove/rename polls # cannot add/remove/rename polls
if polls.keys.sort != previous_polls.keys.sort if polls.keys.sort != previous_polls.keys.sort
post.errors.add(:base, I18n.t("poll.cannot_change_polls_after_5_minutes")) post.errors.add(:base, I18n.t("poll.cannot_change_polls_after_5_minutes"))
@ -305,7 +307,7 @@ after_initialize do
if User.staff.pluck(:id).include?(post.last_editor_id) if User.staff.pluck(:id).include?(post.last_editor_id)
# staff can only edit options # staff can only edit options
polls.each_key do |poll_name| polls.each_key do |poll_name|
if polls[poll_name]["options"].size != previous_polls[poll_name]["options"].size if polls[poll_name]["options"].size != previous_polls[poll_name]["options"].size && previous_polls[poll_name]["voters"].to_i > 0
post.errors.add(:base, I18n.t("poll.staff_cannot_add_or_remove_options_after_5_minutes")) post.errors.add(:base, I18n.t("poll.staff_cannot_add_or_remove_options_after_5_minutes"))
return return
end end

View file

@ -126,11 +126,13 @@ describe PostsController do
end end
end end
it "OP cannot change the options" do describe "with no vote" do
it "OP can change the options" do
xhr :put, :update, { id: post_id, post: { raw: new_option } } xhr :put, :update, { id: post_id, post: { raw: new_option } }
expect(response).not_to be_success expect(response).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["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
end end
it "staff can change the options" do it "staff can change the options" do
@ -148,12 +150,29 @@ describe PostsController do
expect(json["post"]["cooked"]).to match("before") expect(json["post"]["cooked"]).to match("before")
end end
end
describe "with at least one vote" do describe "with at least one vote" do
before do before do
DiscoursePoll::Poll.vote(post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"], user.id) DiscoursePoll::Poll.vote(post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"], user.id)
end end
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 "staff can change the options" do
log_in_user(Fabricate(:moderator))
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 it "support changes on the post" do
xhr :put, :update, { id: post_id, post: { raw: updated } } xhr :put, :update, { id: post_id, post: { raw: updated } }
expect(response).to be_success expect(response).to be_success