Skip to content

Commit

Permalink
Adding displayField detection and interaction to ModelTask. Test Case…
Browse files Browse the repository at this point in the history
…s added.
  • Loading branch information
markstory committed Jul 8, 2009
1 parent dc98184 commit d671056
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
27 changes: 23 additions & 4 deletions cake/console/libs/tasks/model.php
Expand Up @@ -198,6 +198,10 @@ function __interactive() {
$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');
Expand Down Expand Up @@ -285,13 +289,28 @@ function findPrimaryKey($fields) {
}
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) {
Expand Down
21 changes: 21 additions & 0 deletions cake/tests/cases/console/libs/tasks/model.test.php
Expand Up @@ -307,6 +307,26 @@ function testFindPrimaryKey() {
$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.
*
Expand Down Expand Up @@ -680,6 +700,7 @@ function testExecuteIntoInteractive() {
$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
Expand Down

0 comments on commit d671056

Please sign in to comment.