Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding displayField detection and interaction to ModelTask. Test Case…

…s added.
  • Loading branch information...
commit d671056044ae65b713625c578a99a565867f110d 1 parent dc98184
@markstory markstory authored
View
27 cake/console/libs/tasks/model.php
@@ -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');
@@ -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) {
View
21 cake/tests/cases/console/libs/tasks/model.test.php
@@ -308,6 +308,26 @@ function testFindPrimaryKey() {
}
/**
+ * 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.
*
* @return void
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.