FEATURE: Allow single option polls (#8853)

This commit is contained in:
Mark VanLandingham 2020-02-05 08:03:27 -06:00 committed by GitHub
parent 53529a3427
commit 5d97286fa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 25 deletions

View File

@ -272,9 +272,9 @@ export default Controller.extend({
) )
disableInsert(count, isRegular, isMultiple, isNumber, pollMin, pollMax) { disableInsert(count, isRegular, isMultiple, isNumber, pollMin, pollMax) {
return ( return (
(isRegular && count < 2) || (isRegular && count < 1) ||
(isMultiple && count < pollMin && pollMin >= pollMax) || (isMultiple && count < pollMin && pollMin >= pollMax) ||
(isNumber ? false : count < 2) (isNumber ? false : count < 1)
); );
}, },

View File

@ -100,7 +100,7 @@ en:
title: Build Poll title: Build Poll
insert: Insert Poll insert: Insert Poll
help: help:
options_count: Enter at least 2 options options_count: Enter at least 1 option
invalid_values: Minimum value must be smaller than the maximum value. invalid_values: Minimum value must be smaller than the maximum value.
min_step_value: The minimum step value is 1 min_step_value: The minimum step value is 1
poll_type: poll_type:

View File

@ -30,8 +30,8 @@ en:
multiple_polls_without_name: "There are multiple polls without a name. Use the '<code>name</code>' attribute to uniquely identify your polls." multiple_polls_without_name: "There are multiple polls without a name. Use the '<code>name</code>' attribute to uniquely identify your polls."
multiple_polls_with_same_name: "There are multiple polls with the same name: <strong>%{name}</strong>. Use the '<code>name</code>' attribute to uniquely identify your polls." multiple_polls_with_same_name: "There are multiple polls with the same name: <strong>%{name}</strong>. Use the '<code>name</code>' attribute to uniquely identify your polls."
default_poll_must_have_at_least_2_options: "Poll must have at least 2 options." default_poll_must_have_at_least_1_option: "Poll must have at least 1 option."
named_poll_must_have_at_least_2_options: "Poll named <strong>%{name}</strong> must have at least 2 options." named_poll_must_have_at_least_1_option: "Poll named <strong>%{name}</strong> must have at least 1 option."
default_poll_must_have_less_options: default_poll_must_have_less_options:
one: "Poll must have less than %{count} option." one: "Poll must have less than %{count} option."

View File

@ -18,7 +18,7 @@ module DiscoursePoll
return false unless unique_poll_name?(polls, poll) return false unless unique_poll_name?(polls, poll)
return false unless unique_options?(poll) return false unless unique_options?(poll)
return false unless any_blank_options?(poll) return false unless any_blank_options?(poll)
return false unless at_least_two_options?(poll) return false unless at_least_one_option?(poll)
return false unless valid_number_of_options?(poll) return false unless valid_number_of_options?(poll)
return false unless valid_multiple_choice_settings?(poll) return false unless valid_multiple_choice_settings?(poll)
polls[poll["name"]] = poll polls[poll["name"]] = poll
@ -92,12 +92,12 @@ module DiscoursePoll
true true
end end
def at_least_two_options?(poll) def at_least_one_option?(poll)
if poll["options"].size < 2 if poll["options"].size < 1
if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME
@post.errors.add(:base, I18n.t("poll.default_poll_must_have_at_least_2_options")) @post.errors.add(:base, I18n.t("poll.default_poll_must_have_at_least_1_option"))
else else
@post.errors.add(:base, I18n.t("poll.named_poll_must_have_at_least_2_options", name: poll["name"])) @post.errors.add(:base, I18n.t("poll.named_poll_must_have_at_least_1_option", name: poll["name"]))
end end
return false return false
@ -170,9 +170,9 @@ module DiscoursePoll
valid = false valid = false
elsif ((max - min + 1) / step) < 2 elsif ((max - min + 1) / step) < 2
if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME
@post.errors.add(:base, I18n.t("poll.default_poll_must_have_at_least_2_options")) @post.errors.add(:base, I18n.t("poll.default_poll_must_have_at_least_1_option"))
else else
@post.errors.add(:base, I18n.t("poll.named_poll_must_have_at_least_2_options", name: poll["name"])) @post.errors.add(:base, I18n.t("poll.named_poll_must_have_at_least_1_option", name: poll["name"]))
end end
valid = false valid = false
end end

View File

@ -72,14 +72,14 @@ describe PostsController do
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_different_options")) expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_different_options"))
end end
it "should have at least 2 options" do it "should have at least 1 options" do
post :create, params: { post :create, params: {
title: title, raw: "[poll]\n- A\n[/poll]" title: title, raw: "[poll]\n[/poll]"
}, format: :json }, format: :json
expect(response).not_to be_successful expect(response).not_to be_successful
json = ::JSON.parse(response.body) json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_at_least_2_options")) expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_at_least_1_option"))
end end
it "should have at most 'SiteSetting.poll_maximum_options' options" do it "should have at most 'SiteSetting.poll_maximum_options' options" do
@ -267,14 +267,14 @@ describe PostsController do
expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_different_options", name: "foo")) expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_different_options", name: "foo"))
end end
it "should have at least 2 options" do it "should have at least 1 option" do
post :create, params: { post :create, params: {
title: title, raw: "[poll name='foo']\n- A\n[/poll]" title: title, raw: "[poll name='foo']\n[/poll]"
}, format: :json }, format: :json
expect(response).not_to be_successful expect(response).not_to be_successful
json = ::JSON.parse(response.body) json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_at_least_2_options", name: "foo")) expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_at_least_1_option", name: "foo"))
end end
end end

View File

@ -154,10 +154,9 @@ describe ::DiscoursePoll::PollsValidator do
) )
end end
it "ensure that polls have at least 2 options" do it "ensure that polls have at least 1 option" do
raw = <<~RAW raw = <<~RAW
[poll] [poll]
* 1
[/poll] [/poll]
RAW RAW
@ -165,12 +164,11 @@ describe ::DiscoursePoll::PollsValidator do
expect(post.valid?).to eq(false) expect(post.valid?).to eq(false)
expect(post.errors[:base]).to include( expect(post.errors[:base]).to include(
I18n.t("poll.default_poll_must_have_at_least_2_options") I18n.t("poll.default_poll_must_have_at_least_1_option")
) )
raw = <<~RAW raw = <<~RAW
[poll name=test] [poll name=test]
* 1
[/poll] [/poll]
RAW RAW
@ -178,7 +176,7 @@ describe ::DiscoursePoll::PollsValidator do
expect(post.valid?).to eq(false) expect(post.valid?).to eq(false)
expect(post.errors[:base]).to include( expect(post.errors[:base]).to include(
I18n.t("poll.named_poll_must_have_at_least_2_options", name: "test") I18n.t("poll.named_poll_must_have_at_least_1_option", name: "test")
) )
end end
@ -365,7 +363,7 @@ describe ::DiscoursePoll::PollsValidator do
expect(post.valid?).to eq(false) expect(post.valid?).to eq(false)
expect(post.errors[:base]).to include("Min #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}") expect(post.errors[:base]).to include("Min #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}")
expect(post.errors[:base]).to include("Max #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}") expect(post.errors[:base]).to include("Max #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}")
expect(post.errors[:base]).to include(I18n.t("poll.default_poll_must_have_at_least_2_options")) expect(post.errors[:base]).to include(I18n.t("poll.default_poll_must_have_at_least_1_option"))
end end
end end
end end

View File

@ -195,10 +195,17 @@ test("disableInsert", function(assert) {
controller.setProperties({ controller.setProperties({
pollType: controller.regularPollType, pollType: controller.regularPollType,
pollOptionsCount: 1 pollOptionsCount: 0
}); });
assert.equal(controller.disableInsert, true, "it should be true"); assert.equal(controller.disableInsert, true, "it should be true");
controller.setProperties({
pollType: controller.regularPollType,
pollOptionsCount: 1
});
assert.equal(controller.disableInsert, false, "it should be false");
}); });
test("number pollOutput", function(assert) { test("number pollOutput", function(assert) {