1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 14:00:56 +01:00

Move Slowvote vote types to a separate object

Summary: Ref T13682. Extract Slowvote vote types to a separate object, to prepare for turning them into API-friendly string constants.

Test Plan: Created, edited, and voted in Slowvote polls. Grepped for affected constants.

Maniphest Tasks: T13682

Differential Revision: https://secure.phabricator.com/D21845
This commit is contained in:
epriestley 2022-05-26 10:04:11 -07:00
parent 9f075839a2
commit 9dad49472c
7 changed files with 151 additions and 16 deletions

View file

@ -4859,6 +4859,7 @@ phutil_register_library_map(array(
'PhabricatorSlowvoteTransactionQuery' => 'applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php', 'PhabricatorSlowvoteTransactionQuery' => 'applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php',
'PhabricatorSlowvoteTransactionType' => 'applications/slowvote/xaction/PhabricatorSlowvoteTransactionType.php', 'PhabricatorSlowvoteTransactionType' => 'applications/slowvote/xaction/PhabricatorSlowvoteTransactionType.php',
'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php', 'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php',
'PhabricatorSlowvoteVotingMethodTransaction' => 'applications/slowvote/xaction/PhabricatorSlowvoteVotingMethodTransaction.php',
'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php', 'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php',
'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php', 'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php',
'PhabricatorSourceCodeView' => 'view/layout/PhabricatorSourceCodeView.php', 'PhabricatorSourceCodeView' => 'view/layout/PhabricatorSourceCodeView.php',
@ -5873,6 +5874,7 @@ phutil_register_library_map(array(
'SlowvoteEmbedView' => 'applications/slowvote/view/SlowvoteEmbedView.php', 'SlowvoteEmbedView' => 'applications/slowvote/view/SlowvoteEmbedView.php',
'SlowvoteInfoConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteInfoConduitAPIMethod.php', 'SlowvoteInfoConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteInfoConduitAPIMethod.php',
'SlowvotePollResponseVisibility' => 'applications/slowvote/constants/SlowvotePollResponseVisibility.php', 'SlowvotePollResponseVisibility' => 'applications/slowvote/constants/SlowvotePollResponseVisibility.php',
'SlowvotePollVotingMethod' => 'applications/slowvote/constants/SlowvotePollVotingMethod.php',
'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php', 'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php',
'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php', 'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php',
'SubscriptionListDialogBuilder' => 'applications/subscriptions/view/SubscriptionListDialogBuilder.php', 'SubscriptionListDialogBuilder' => 'applications/subscriptions/view/SubscriptionListDialogBuilder.php',
@ -11573,6 +11575,7 @@ phutil_register_library_map(array(
'PhabricatorSlowvoteTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 'PhabricatorSlowvoteTransactionQuery' => 'PhabricatorApplicationTransactionQuery',
'PhabricatorSlowvoteTransactionType' => 'PhabricatorModularTransactionType', 'PhabricatorSlowvoteTransactionType' => 'PhabricatorModularTransactionType',
'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController',
'PhabricatorSlowvoteVotingMethodTransaction' => 'PhabricatorSlowvoteTransactionType',
'PhabricatorSlug' => 'Phobject', 'PhabricatorSlug' => 'Phobject',
'PhabricatorSlugTestCase' => 'PhabricatorTestCase', 'PhabricatorSlugTestCase' => 'PhabricatorTestCase',
'PhabricatorSourceCodeView' => 'AphrontView', 'PhabricatorSourceCodeView' => 'AphrontView',
@ -12775,6 +12778,7 @@ phutil_register_library_map(array(
'SlowvoteEmbedView' => 'AphrontView', 'SlowvoteEmbedView' => 'AphrontView',
'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod', 'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod',
'SlowvotePollResponseVisibility' => 'Phobject', 'SlowvotePollResponseVisibility' => 'Phobject',
'SlowvotePollVotingMethod' => 'Phobject',
'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule', 'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule',
'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod', 'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod',
'SubscriptionListDialogBuilder' => 'Phobject', 'SubscriptionListDialogBuilder' => 'Phobject',

View file

@ -0,0 +1,70 @@
<?php
final class SlowvotePollVotingMethod
extends Phobject {
const METHOD_PLURALITY = 0;
const METHOD_APPROVAL = 1;
private $key;
public static function newVotingMethodObject($key) {
$object = new self();
$object->key = $key;
return $object;
}
public function getKey() {
return $this->key;
}
public static function getAll() {
$map = self::getMap();
$result = array();
foreach ($map as $key => $spec) {
$result[$key] = self::newVotingMethodObject($key);
}
return $result;
}
public function getName() {
$name = $this->getProperty('name');
if ($name === null) {
$name = pht('Unknown ("%s")', $this->getKey());
}
return $name;
}
public function getNameForEdit() {
$name = $this->getProperty('name.edit');
if ($name === null) {
$name = pht('Unknown ("%s")', $this->getKey());
}
return $name;
}
private function getProperty($key, $default = null) {
$spec = idx(self::getMap(), $this->getKey(), array());
return idx($spec, $key, $default);
}
private static function getMap() {
return array(
self::METHOD_PLURALITY => array(
'name' => pht('Plurality'),
'name.edit' => pht('Plurality (Single Choice)'),
),
self::METHOD_APPROVAL => array(
'name' => pht('Approval'),
'name.edit' => pht('Approval (Multiple Choice)'),
),
);
}
}

View file

@ -189,12 +189,19 @@ final class PhabricatorSlowvoteEditController
} }
} }
$poll_type_options = array( $vote_type_map = SlowvotePollVotingMethod::getAll();
PhabricatorSlowvotePoll::METHOD_PLURALITY => $vote_type_options = mpull($vote_type_map, 'getNameForEdit');
pht('Plurality (Single Choice)'),
PhabricatorSlowvotePoll::METHOD_APPROVAL => $method = $poll->getMethod();
pht('Approval (Multiple Choice)'), if (!isset($vote_type_options[$method])) {
); $method_object =
SlowvotePollVotingMethod::newVotingMethodObject(
$method);
$vote_type_options = array(
$method => $method_object->getNameForEdit(),
) + $vote_type_options;
}
$response_type_map = SlowvotePollResponseVisibility::getAll(); $response_type_map = SlowvotePollResponseVisibility::getAll();
$response_type_options = mpull($response_type_map, 'getNameForEdit'); $response_type_options = mpull($response_type_map, 'getNameForEdit');
@ -216,12 +223,12 @@ final class PhabricatorSlowvoteEditController
->setLabel(pht('Vote Type')) ->setLabel(pht('Vote Type'))
->setName('method') ->setName('method')
->setValue($poll->getMethod()) ->setValue($poll->getMethod())
->setOptions($poll_type_options)); ->setOptions($vote_type_options));
} else { } else {
$form->appendChild( $form->appendChild(
id(new AphrontFormStaticControl()) id(new AphrontFormStaticControl())
->setLabel(pht('Vote Type')) ->setLabel(pht('Vote Type'))
->setValue(idx($poll_type_options, $poll->getMethod()))); ->setValue(idx($vote_type_options, $poll->getMethod())));
} }
if ($is_new) { if ($is_new) {

View file

@ -35,7 +35,7 @@ final class PhabricatorSlowvoteVoteController
$votes = array_fuse($votes); $votes = array_fuse($votes);
$method = $poll->getMethod(); $method = $poll->getMethod();
$is_plurality = ($method == PhabricatorSlowvotePoll::METHOD_PLURALITY); $is_plurality = ($method == SlowvotePollVotingMethod::METHOD_PLURALITY);
if (!$votes) { if (!$votes) {
if ($is_plurality) { if ($is_plurality) {

View file

@ -13,9 +13,6 @@ final class PhabricatorSlowvotePoll
PhabricatorSpacesInterface, PhabricatorSpacesInterface,
PhabricatorConduitResultInterface { PhabricatorConduitResultInterface {
const METHOD_PLURALITY = 0;
const METHOD_APPROVAL = 1;
protected $question; protected $question;
protected $description; protected $description;
protected $authorPHID; protected $authorPHID;
@ -40,11 +37,13 @@ final class PhabricatorSlowvotePoll
PhabricatorSlowvoteDefaultViewCapability::CAPABILITY); PhabricatorSlowvoteDefaultViewCapability::CAPABILITY);
$default_responses = SlowvotePollResponseVisibility::RESPONSES_VISIBLE; $default_responses = SlowvotePollResponseVisibility::RESPONSES_VISIBLE;
$default_method = SlowvotePollVotingMethod::METHOD_PLURALITY;
return id(new PhabricatorSlowvotePoll()) return id(new PhabricatorSlowvotePoll())
->setAuthorPHID($actor->getPHID()) ->setAuthorPHID($actor->getPHID())
->setViewPolicy($view_policy) ->setViewPolicy($view_policy)
->setSpacePHID($actor->getDefaultSpacePHID()) ->setSpacePHID($actor->getDefaultSpacePHID())
->setMethod($default_method)
->setResponseVisibility($default_responses); ->setResponseVisibility($default_responses);
} }

View file

@ -224,8 +224,8 @@ final class SlowvoteEmbedView extends AphrontView {
private function renderControl(PhabricatorSlowvoteOption $option, $selected) { private function renderControl(PhabricatorSlowvoteOption $option, $selected) {
$types = array( $types = array(
PhabricatorSlowvotePoll::METHOD_PLURALITY => 'radio', SlowvotePollVotingMethod::METHOD_PLURALITY => 'radio',
PhabricatorSlowvotePoll::METHOD_APPROVAL => 'checkbox', SlowvotePollVotingMethod::METHOD_APPROVAL => 'checkbox',
); );
$closed = $this->getPoll()->getIsClosed(); $closed = $this->getPoll()->getIsClosed();
@ -302,10 +302,10 @@ final class SlowvoteEmbedView extends AphrontView {
$percent = sprintf('%d%%', $count ? 100 * $choices / $count : 0); $percent = sprintf('%d%%', $count ? 100 * $choices / $count : 0);
switch ($poll->getMethod()) { switch ($poll->getMethod()) {
case PhabricatorSlowvotePoll::METHOD_PLURALITY: case SlowvotePollVotingMethod::METHOD_PLURALITY:
$status = pht('%s (%d / %d)', $percent, $choices, $count); $status = pht('%s (%d / %d)', $percent, $choices, $count);
break; break;
case PhabricatorSlowvotePoll::METHOD_APPROVAL: case SlowvotePollVotingMethod::METHOD_APPROVAL:
$status = pht('%s Approval (%d / %d)', $percent, $choices, $count); $status = pht('%s Approval (%d / %d)', $percent, $choices, $count);
break; break;
} }

View file

@ -0,0 +1,55 @@
<?php
final class PhabricatorSlowvoteVotingMethodTransaction
extends PhabricatorSlowvoteTransactionType {
const TRANSACTIONTYPE = 'vote:method';
public function generateOldValue($object) {
return (string)$object->getMethod();
}
public function generateNewValue($object, $value) {
return (string)$value;
}
public function applyInternalEffects($object, $value) {
$object->setMethod($value);
}
public function getTitle() {
$old_name = $this->getOldVotingMethodObject()->getName();
$new_name = $this->getNewVotingMethodObject()->getName();
return pht(
'%s changed the voting method from %s to %s.',
$this->renderAuthor(),
$this->renderValue($old_name),
$this->renderValue($new_name));
}
public function getTitleForFeed() {
$old_name = $this->getOldVotingMethodObject()->getName();
$new_name = $this->getNewVotingMethodObject()->getName();
return pht(
'%s changed the voting method of %s from %s to %s.',
$this->renderAuthor(),
$this->renderObject(),
$this->renderValue($old_name),
$this->renderValue($new_name));
}
private function getOldVotingMethodObject() {
return $this->newVotingMethodObject($this->getOldValue());
}
private function getNewVotingMethodObject() {
return $this->newVotingMethodObject($this->getNewValue());
}
private function newVotingMethodObject($value) {
return SlowvotePollVotingMethod::newVotingMethodObject($value);
}
}