Extract polls edit window to a site settings.

This commit is contained in:
Guo Xiang Tan 2016-08-10 15:15:29 +08:00
parent 2b0268e74a
commit 8213da20f2
5 changed files with 47 additions and 16 deletions

View File

@ -18,6 +18,7 @@ en:
site_settings:
poll_enabled: "Allow users to create polls?"
poll_maximum_options: "Maximum number of options allowed in a poll."
poll_edit_window_mins: "Number of minutes after post creation during which polls can be edited."
poll:
multiple_polls_without_name: "There are multiple polls without a name. Use the '<code>name</code>' attribute to uniquely identify your polls."
@ -44,9 +45,10 @@ en:
default_cannot_be_made_public: "Poll with votes cannot be made public."
named_cannot_be_made_public: "Poll named <strong>%{name}</strong> has votes cannot be made public."
cannot_change_polls_after_5_minutes: "You cannot add, remove or rename polls after the first 5 minutes."
op_cannot_edit_options_after_5_minutes: "You cannot add or remove poll options after the first 5 minutes. Please contact a moderator if you need to edit a poll option."
staff_cannot_add_or_remove_options_after_5_minutes: "You cannot add or remove poll options after the first 5 minutes. You should close this topic and create a new one instead."
edit_window_expired:
cannot_change_polls: "You cannot add, remove or rename polls after the first %{minutes} minutes."
op_cannot_edit_options: "You cannot add or remove poll options after the first %{minutes} minutes. Please contact a moderator if you need to edit a poll option."
staff_cannot_add_or_remove_options: "You cannot add or remove poll options after the first %{minutes} minutes. You should close this topic and create a new one instead."
no_polls_associated_with_this_post: "No polls are associated with this post."
no_poll_with_this_name: "No poll named <strong>%{name}</strong> associated with this post."

View File

@ -5,3 +5,5 @@ plugins:
poll_maximum_options:
default: 20
client: true
poll_edit_window_mins:
default: 5

View File

@ -14,11 +14,16 @@ module DiscoursePoll
if polls_updated?(polls, previous_polls) || (current_option_ids != previous_option_ids)
has_votes = total_votes(previous_polls) > 0
# outside of the 5-minute edit window?
if post.created_at < 5.minutes.ago && has_votes
# outside of the edit window?
poll_edit_window_mins = SiteSetting.poll_edit_window_mins
if post.created_at < poll_edit_window_mins.minutes.ago && has_votes
# cannot add/remove/rename polls
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.edit_window_expired.cannot_change_polls", minutes: poll_edit_window_mins
))
return
end
@ -27,13 +32,21 @@ module DiscoursePoll
# staff can only edit options
polls.each_key do |poll_name|
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.edit_window_expired.staff_cannot_add_or_remove_options",
minutes: poll_edit_window_mins
))
return
end
end
else
# OP cannot edit poll options
post.errors.add(:base, I18n.t("poll.op_cannot_edit_options_after_5_minutes"))
post.errors.add(:base, I18n.t(
"poll.edit_window_expired.op_cannot_edit_options",
minutes: poll_edit_window_mins
))
return
end
end

View File

@ -114,7 +114,7 @@ describe PostsController do
end
describe "after the first 5 minutes" do
describe "after the poll edit window has expired" do
let(:poll) { "[poll]\n- A\n- B[/poll]" }
let(:new_option) { "[poll]\n- A\n- C[/poll]" }
@ -127,6 +127,12 @@ describe PostsController do
end
end
let(:poll_edit_window_mins) { 6 }
before do
SiteSetting.poll_edit_window_mins = poll_edit_window_mins
end
describe "with no vote" do
it "OP can change the options" do
@ -163,7 +169,10 @@ describe PostsController 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"))
expect(json["errors"][0]).to eq(I18n.t(
"poll.edit_window_expired.op_cannot_edit_options",
minutes: poll_edit_window_mins
))
end
it "staff can change the options and votes are merged" do

View File

@ -217,12 +217,14 @@ describe DiscoursePoll::PollsUpdater do
end
end
describe "when post has been created more than 5 minutes ago" do
let(:another_post) { Fabricate(:post, created_at: Time.zone.now - 5.minutes) }
describe "when poll edit window has expired" do
let(:poll_edit_window_mins) { 6 }
let(:another_post) { Fabricate(:post, created_at: Time.zone.now - poll_edit_window_mins.minutes) }
before do
polls.each { |key, value| value["voters"] = 2 }
described_class.update(another_post, polls)
SiteSetting.poll_edit_window_mins = poll_edit_window_mins
end
it "should not allow new polls to be added" do
@ -231,8 +233,9 @@ describe DiscoursePoll::PollsUpdater do
end
expect(another_post.errors[:base]).to include(I18n.t(
"poll.cannot_change_polls_after_5_minutes")
)
"poll.edit_window_expired.cannot_change_polls",
minutes: poll_edit_window_mins
))
expect(messages).to eq([])
end
@ -243,7 +246,8 @@ describe DiscoursePoll::PollsUpdater do
end
expect(another_post.errors[:base]).to include(I18n.t(
"poll.op_cannot_edit_options_after_5_minutes"
"poll.edit_window_expired.op_cannot_edit_options",
minutes: poll_edit_window_mins
))
expect(messages).to eq([])
@ -258,7 +262,8 @@ describe DiscoursePoll::PollsUpdater do
end
expect(another_post.errors[:base]).to include(I18n.t(
"poll.staff_cannot_add_or_remove_options_after_5_minutes"
"poll.edit_window_expired.staff_cannot_add_or_remove_options",
minutes: poll_edit_window_mins
))
expect(messages).to eq([])