Adding displayField detection and interaction to ModelTask. Test Cases added.

This commit is contained in:
mark_story 2009-07-08 09:10:18 -04:00
parent dc98184086
commit d671056044
2 changed files with 44 additions and 4 deletions

View file

@ -198,6 +198,10 @@ class ModelTask extends Shell {
$primaryKey = $this->findPrimaryKey($fields);
}
}
$displayField = $tempModel->hasField(array('name', 'title'));
if (!$displayField) {
$displayField = $this->findDisplayField($tempModel->schema());
}
$prompt = __("Would you like to supply validation criteria \nfor the fields in your model?", true);
$wannaDoValidation = $this->in($prompt, array('y','n'), 'y');
@ -285,13 +289,28 @@ class ModelTask extends Shell {
}
return $this->in(__('What is the primaryKey?', true), null, $name);
}
/**
* interact with the user to find the displayField value for a model.
*
* @param array $fields Array of fields to look for and choose as a displayField
* @return mixed Name of field to use for displayField or false if the user declines to choose
**/
function findDisplayField($fields) {
$fieldNames = array_keys($fields);
$prompt = __("A displayField could not be automatically detected\nwould you like to choose one?", true);
$continue = $this->in($prompt, array('y', 'n'));
if (strtolower($continue) == 'n') {
return false;
}
$prompt = __('Choose a field from the options above:', true);
$choice = $this->inOptions($fieldNames, $prompt);
return $fieldNames[$choice];
}
/**
* Handles Generation and user interaction for creating validation.
*
* @param object $model
* @param boolean $interactive
* @return array $validate
* @param object $model Model to have validations generated for.
* @return array $validate Array of user selected validations.
* @access public
*/
function doValidation(&$model) {

View file

@ -307,6 +307,26 @@ class ModelTaskTest extends CakeTestCase {
$this->assertEqual($result, $expected);
}
/**
* test finding Display field
*
* @return void
**/
function testFindDisplayField() {
$fields = array('id' => array(), 'tagname' => array(), 'body' => array(),
'created' => array(), 'modified' => array());
$this->Task->setReturnValue('in', 'n');
$this->Task->setReturnValueAt(0, 'in', 'n');
$result = $this->Task->findDisplayField($fields);
$this->assertFalse($result);
$this->Task->setReturnValueAt(1, 'in', 'y');
$this->Task->setReturnValueAt(2, 'in', 2);
$result = $this->Task->findDisplayField($fields);
$this->assertEqual($result, 'tagname');
}
/**
* test that belongsTo generation works.
*
@ -680,6 +700,7 @@ class ModelTaskTest extends CakeTestCase {
$this->Task->path = '/my/path/';
$this->Task->setReturnValueAt(0, 'in', '1'); //choose article
// $this->Task->setReturnValueAt(2, 'in', 'n'); //no validation
$this->Task->setReturnValueAt(1, 'in', 'n'); //no validation
$this->Task->setReturnValueAt(2, 'in', 'y'); //yes to associations
$this->Task->setReturnValueAt(3, 'in', 'y'); //yes to comment relation