From 2954c99a1ee9d1ee9815ff61462225ef35a217bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 1 May 2015 16:44:51 +0200 Subject: [PATCH] FEATURE: new 'poll_maximum_options' site setting to limit the number of options in a poll --- plugins/poll/config/locales/server.en.yml | 6 +++++- plugins/poll/config/settings.yml | 2 ++ plugins/poll/plugin.rb | 8 ++++++++ .../poll/spec/controllers/posts_controller_spec.rb | 12 ++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/plugins/poll/config/locales/server.en.yml b/plugins/poll/config/locales/server.en.yml index 3434059bc5b..1590536aff4 100644 --- a/plugins/poll/config/locales/server.en.yml +++ b/plugins/poll/config/locales/server.en.yml @@ -8,6 +8,7 @@ en: site_settings: poll_enabled: "Allow users to create polls?" + poll_maximum_options: "Maximum number of options allowed in a poll." poll: multiple_polls_without_name: "There are multiple polls without a name. Use the 'name' attribute to uniquely identify your polls." @@ -16,8 +17,11 @@ en: default_poll_must_have_at_least_2_options: "Poll must have at least 2 options." named_poll_must_have_at_least_2_options: "Poll named %{name} must have at least 2 options." + default_poll_must_have_less_options: "Poll must have less than %{max} options." + named_poll_must_have_less_options: "Poll named %{name} must have less than %{max} options." + default_poll_must_have_different_options: "Poll must have different options." - named_poll_must_have_different_options: "Poll name %{name} must have different options." + named_poll_must_have_different_options: "Poll named %{name} must have different options." requires_at_least_1_valid_option: "You must select at least 1 valid option." cannot_change_polls_after_5_minutes: "Polls cannot be changed after the first 5 minutes. Contact a moderator if you need to change them." diff --git a/plugins/poll/config/settings.yml b/plugins/poll/config/settings.yml index eb5cc0aafe1..354288bf720 100644 --- a/plugins/poll/config/settings.yml +++ b/plugins/poll/config/settings.yml @@ -1,3 +1,5 @@ plugins: poll_enabled: default: true + poll_maximum_options: + default: 10 diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index 514ef785a5e..fa3d268b6c1 100644 --- a/plugins/poll/plugin.rb +++ b/plugins/poll/plugin.rb @@ -231,6 +231,14 @@ after_initialize do return end + # maximum # of options + if poll["options"].size > SiteSetting.poll_maximum_options + poll["name"] == DEFAULT_POLL_NAME ? + self.errors.add(:base, I18n.t("poll.default_poll_must_have_less_options", max: SiteSetting.poll_maximum_options)) : + self.errors.add(:base, I18n.t("poll.named_poll_must_have_less_options", name: poll["name"], max: SiteSetting.poll_maximum_options)) + return + end + # store the valid poll polls[poll["name"]] = poll end diff --git a/plugins/poll/spec/controllers/posts_controller_spec.rb b/plugins/poll/spec/controllers/posts_controller_spec.rb index c36befe22f4..e37c4d4848f 100644 --- a/plugins/poll/spec/controllers/posts_controller_spec.rb +++ b/plugins/poll/spec/controllers/posts_controller_spec.rb @@ -37,6 +37,18 @@ describe PostsController do expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_at_least_2_options")) end + it "should have at most 'SiteSetting.poll_maximum_options' options" do + raw = "[poll]" + (SiteSetting.poll_maximum_options + 1).times { |n| raw << "\n- #{n}" } + raw << "[/poll]" + + xhr :post, :create, { title: title, raw: raw } + + expect(response).not_to be_success + json = ::JSON.parse(response.body) + expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_less_options", max: SiteSetting.poll_maximum_options)) + end + describe "edit window" do describe "within the first 5 minutes" do