Skip to content
This repository
Browse code

Adding displayField detection and interaction to ModelTask. Test Case…

…s added.
  • Loading branch information...
commit d671056044ae65b713625c578a99a565867f110d 1 parent dc98184
Mark Story authored July 08, 2009
27  cake/console/libs/tasks/model.php
@@ -198,6 +198,10 @@ function __interactive() {
198 198
 				$primaryKey = $this->findPrimaryKey($fields);
199 199
 			}
200 200
 		}
  201
+		$displayField = $tempModel->hasField(array('name', 'title'));
  202
+		if (!$displayField) {
  203
+			$displayField = $this->findDisplayField($tempModel->schema());
  204
+		}
201 205
 
202 206
 		$prompt = __("Would you like to supply validation criteria \nfor the fields in your model?", true);
203 207
 		$wannaDoValidation = $this->in($prompt, array('y','n'), 'y');
@@ -285,13 +289,28 @@ function findPrimaryKey($fields) {
285 289
 		}
286 290
 		return $this->in(__('What is the primaryKey?', true), null, $name);
287 291
 	}
288  
-
  292
+/**
  293
+ * interact with the user to find the displayField value for a model.
  294
+ *
  295
+ * @param array $fields Array of fields to look for and choose as a displayField
  296
+ * @return mixed Name of field to use for displayField or false if the user declines to choose
  297
+ **/
  298
+	function findDisplayField($fields) {
  299
+		$fieldNames = array_keys($fields);
  300
+		$prompt = __("A displayField could not be automatically detected\nwould you like to choose one?", true);
  301
+		$continue = $this->in($prompt, array('y', 'n'));
  302
+		if (strtolower($continue) == 'n') {
  303
+			return false;
  304
+		}
  305
+		$prompt = __('Choose a field from the options above:', true);
  306
+		$choice = $this->inOptions($fieldNames, $prompt);
  307
+		return $fieldNames[$choice];
  308
+	}
289 309
 /**
290 310
  * Handles Generation and user interaction for creating validation.
291 311
  *
292  
- * @param object $model
293  
- * @param boolean $interactive
294  
- * @return array $validate
  312
+ * @param object $model Model to have validations generated for.
  313
+ * @return array $validate Array of user selected validations.
295 314
  * @access public
296 315
  */
297 316
 	function doValidation(&$model) {
21  cake/tests/cases/console/libs/tasks/model.test.php
@@ -308,6 +308,26 @@ function testFindPrimaryKey() {
308 308
 	}
309 309
 
310 310
 /**
  311
+ * test finding Display field
  312
+ *
  313
+ * @return void
  314
+ **/
  315
+	function testFindDisplayField() {
  316
+		$fields = array('id' => array(), 'tagname' => array(), 'body' => array(), 
  317
+			'created' => array(), 'modified' => array());
  318
+		
  319
+		$this->Task->setReturnValue('in', 'n');
  320
+		$this->Task->setReturnValueAt(0, 'in', 'n');
  321
+		$result = $this->Task->findDisplayField($fields);
  322
+		$this->assertFalse($result);
  323
+
  324
+		$this->Task->setReturnValueAt(1, 'in', 'y');
  325
+		$this->Task->setReturnValueAt(2, 'in', 2);
  326
+		$result = $this->Task->findDisplayField($fields);
  327
+		$this->assertEqual($result, 'tagname');
  328
+	}
  329
+
  330
+/**
311 331
  * test that belongsTo generation works.
312 332
  *
313 333
  * @return void
@@ -680,6 +700,7 @@ function testExecuteIntoInteractive() {
680 700
 		$this->Task->path = '/my/path/';
681 701
 
682 702
 		$this->Task->setReturnValueAt(0, 'in', '1'); //choose article
  703
+	//	$this->Task->setReturnValueAt(2, 'in', 'n'); //no validation
683 704
 		$this->Task->setReturnValueAt(1, 'in', 'n'); //no validation
684 705
 		$this->Task->setReturnValueAt(2, 'in', 'y'); //yes to associations
685 706
 		$this->Task->setReturnValueAt(3, 'in', 'y'); //yes to comment relation

0 notes on commit d671056

Please sign in to comment.
Something went wrong with that request. Please try again.