From 8213da20f205b42ec549e66fb1fc276de893659e Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 10 Aug 2016 15:15:29 +0800 Subject: [PATCH] Extract polls edit window to a site settings. --- plugins/poll/config/locales/server.en.yml | 8 ++++--- plugins/poll/config/settings.yml | 2 ++ plugins/poll/lib/polls_updater.rb | 23 +++++++++++++++---- .../spec/controllers/posts_controller_spec.rb | 13 +++++++++-- plugins/poll/spec/lib/polls_updater_spec.rb | 17 +++++++++----- 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/plugins/poll/config/locales/server.en.yml b/plugins/poll/config/locales/server.en.yml index d3716baa4..6ae0bea3c 100644 --- a/plugins/poll/config/locales/server.en.yml +++ b/plugins/poll/config/locales/server.en.yml @@ -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 'name' 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 %{name} 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 %{name} associated with this post." diff --git a/plugins/poll/config/settings.yml b/plugins/poll/config/settings.yml index 2d01ba6b8..04bf21f5e 100644 --- a/plugins/poll/config/settings.yml +++ b/plugins/poll/config/settings.yml @@ -5,3 +5,5 @@ plugins: poll_maximum_options: default: 20 client: true + poll_edit_window_mins: + default: 5 diff --git a/plugins/poll/lib/polls_updater.rb b/plugins/poll/lib/polls_updater.rb index df3b22432..42d67f219 100644 --- a/plugins/poll/lib/polls_updater.rb +++ b/plugins/poll/lib/polls_updater.rb @@ -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 diff --git a/plugins/poll/spec/controllers/posts_controller_spec.rb b/plugins/poll/spec/controllers/posts_controller_spec.rb index a79ea44e7..d5bf98b15 100644 --- a/plugins/poll/spec/controllers/posts_controller_spec.rb +++ b/plugins/poll/spec/controllers/posts_controller_spec.rb @@ -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 diff --git a/plugins/poll/spec/lib/polls_updater_spec.rb b/plugins/poll/spec/lib/polls_updater_spec.rb index 1e07cf8fe..4210b12d5 100644 --- a/plugins/poll/spec/lib/polls_updater_spec.rb +++ b/plugins/poll/spec/lib/polls_updater_spec.rb @@ -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([])