From d671056044ae65b713625c578a99a565867f110d Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 8 Jul 2009 09:10:18 -0400 Subject: [PATCH] Adding displayField detection and interaction to ModelTask. Test Cases added. --- cake/console/libs/tasks/model.php | 27 ++++++++++++++++--- .../cases/console/libs/tasks/model.test.php | 21 +++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/cake/console/libs/tasks/model.php b/cake/console/libs/tasks/model.php index 1725b077a..ea507ba6a 100644 --- a/cake/console/libs/tasks/model.php +++ b/cake/console/libs/tasks/model.php @@ -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) { diff --git a/cake/tests/cases/console/libs/tasks/model.test.php b/cake/tests/cases/console/libs/tasks/model.test.php index abf1b05a9..f7e65c673 100644 --- a/cake/tests/cases/console/libs/tasks/model.test.php +++ b/cake/tests/cases/console/libs/tasks/model.test.php @@ -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