Permalink
Browse files

Adding connection prompts.

Fixing hard coded $ds
Adding i18n
Extracting customAction()
Test cases added.
  • Loading branch information...
markstory committed Jun 4, 2009
1 parent 3c5e4a6 commit 8040cc31e5199a7e787e05c5f36a68595d232661
Showing with 100 additions and 35 deletions.
  1. +54 −35 cake/console/libs/tasks/view.php
  2. +46 −0 cake/tests/cases/console/libs/tasks/view.test.php
@@ -45,7 +45,7 @@ class ViewTask extends Shell {
* @var array
* @access public
*/
- var $tasks = array('Project', 'Controller', 'Template');
+ var $tasks = array('Project', 'Controller', 'DbConfig', 'Template');
/**
* path to VIEWS directory
*
@@ -88,6 +88,7 @@ class ViewTask extends Shell {
*/
function initialize() {
}
+
/**
* Execution method always used for tasks
*
@@ -99,6 +100,9 @@ function execute() {
}
if (isset($this->args[0])) {
+ if (!isset($this->connection)) {
+ $this->connection = 'default';
+ }
$controller = $action = $alias = null;
$this->controllerName = Inflector::camelize($this->args[0]);
$this->controllerPath = Inflector::underscore($this->controllerName);
@@ -114,7 +118,7 @@ function execute() {
if (!$action) {
$action = $this->template;
}
-
+
if (strtolower($this->args[0]) == 'all') {
return $this->all();
}
@@ -157,9 +161,8 @@ function execute() {
* @return void
**/
function all() {
- $ds = 'default';
$actions = $this->scaffoldActions;
- $tables = $this->Controller->listAll($ds, false);
+ $tables = $this->Controller->listAll($this->connection, false);
$this->interactive = false;
foreach ($tables as $table) {
$model = $this->_modelName($table);
@@ -184,24 +187,31 @@ function __interactive() {
$this->out(sprintf("Bake View\nPath: %s", $this->path));
$this->hr();
+ if (empty($this->connection)) {
+ $this->connection = $this->DbConfig->getConfig();
+ }
+
$wannaDoAdmin = 'n';
$wannaDoScaffold = 'y';
$admin = false;
- $this->interactive = false;
+ $this->Controller->connection = $this->connection;
$this->controllerName = $this->Controller->getName();
$this->controllerPath = strtolower(Inflector::underscore($this->controllerName));
- $interactive = $this->in("Would you like bake to build your views interactively?\nWarning: Choosing no will overwrite {$this->controllerName} views if it exist.", array('y','n'), 'y');
+ $prompt = sprintf(__("Would you like bake to build your views interactively?\nWarning: Choosing no will overwrite %s views if it exist.", true), $this->controllerName);
+ $interactive = $this->in($prompt, array('y', 'n'), 'n');
- if (strtolower($interactive) == 'y') {
- $this->interactive = true;
- $wannaDoScaffold = $this->in("Would you like to create some scaffolded views (index, add, view, edit) for this controller?\nNOTE: Before doing so, you'll need to create your controller and model classes (including associated models).", array('y','n'), 'n');
+ if (strtolower($interactive) == 'n') {
+ $this->interactive = false;
}
+ $prompt = __("Would you like to create some CRUD views\n(index, add, view, edit) for this controller?\nNOTE: Before doing so, you'll need to create your controller\nand model classes (including associated models).", true);
+ $wannaDoScaffold = $this->in($prompt, array('y','n'), 'n');
+
if (strtolower($wannaDoScaffold) == 'y') {
- $wannaDoAdmin = $this->in("Would you like to create the views for admin routing?", array('y','n'), 'y');
+ $wannaDoAdmin = $this->in(__("Would you like to create the views for admin routing?", true), array('y','n'), 'y');
}
if (strtolower($wannaDoAdmin) == 'y') {
@@ -221,30 +231,9 @@ function __interactive() {
}
$this->hr();
$this->out('');
- $this->out('View Scaffolding Complete.'."\n");
+ $this->out(__("View Scaffolding Complete.\n", true));
} else {
- $action = '';
- while ($action == '') {
- $action = $this->in('Action Name? (use camelCased function name)');
- if ($action == '') {
- $this->out('The action name you supplied was empty. Please try again.');
- }
- }
- $this->out('');
- $this->hr();
- $this->out('The following view will be created:');
- $this->hr();
- $this->out("Controller Name: {$this->controllerName}");
- $this->out("Action Name: {$action}");
- $this->out("Path: ".$this->params['app'] . DS . $this->controllerPath . DS . Inflector::underscore($action) . ".ctp");
- $this->hr();
- $looksGood = $this->in('Look okay?', array('y','n'), 'y');
- if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
- $this->bake($action);
- $this->_stop();
- } else {
- $this->out('Bake Aborted.');
- }
+ $this->customAction();
}
}
@@ -274,7 +263,7 @@ function __loadController() {
$this->_stop();
}
$controllerClassName = $this->controllerName . 'Controller';
- $controllerObj = & new $controllerClassName();
+ $controllerObj =& new $controllerClassName();
$controllerObj->constructClasses();
$modelClass = $controllerObj->modelClass;
$modelObj =& ClassRegistry::getObject($controllerObj->modelKey);
@@ -318,6 +307,36 @@ function bakeActions($actions, $vars) {
}
}
+/**
+ * handle creation of baking a custom action view file
+ *
+ * @return void
+ **/
+ function customAction() {
+ $action = '';
+ while ($action == '') {
+ $action = $this->in(__('Action Name? (use lowercase_underscored function name)', true));
+ if ($action == '') {
+ $this->out(__('The action name you supplied was empty. Please try again.', true));
+ }
+ }
+ $this->out('');
+ $this->hr();
+ $this->out(__('The following view will be created:', true));
+ $this->hr();
+ $this->out(sprintf(__('Controller Name: %s', true), $this->controllerName));
+ $this->out(sprintf(__('Action Name: %s', true), $action));
+ $this->out(sprintf(__('Path: %s', true), $this->params['app'] . DS . $this->controllerPath . DS . Inflector::underscore($action) . ".ctp"));
+ $this->hr();
+ $looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y');
+ if (strtolower($looksGood) == 'y') {
+ $this->bake($action);
+ $this->_stop();
+ } else {
+ $this->out(__('Bake Aborted.', true));
+ }
+ }
+
/**
* Assembles and writes bakes the view file.
*
@@ -334,7 +353,6 @@ function bake($action, $content = '') {
$Folder =& new Folder($this->path . $this->controllerPath, true);
$errors = $Folder->errors();
if (empty($errors)) {
- $path = $Folder->slashTerm($Folder->pwd());
return $this->createFile($filename, $content);
} else {
foreach ($errors as $error) {
@@ -343,6 +361,7 @@ function bake($action, $content = '') {
}
return false;
}
+
/**
* Builds content from template and variables
*
@@ -40,6 +40,7 @@
if (!class_exists('TestTask')) {
require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'view.php';
+ require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'controller.php';
require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
}
@@ -52,6 +53,8 @@
array('in', '_stop', 'err', 'out', 'createFile')
);
+Mock::generate('ControllerTask', 'ViewTaskMockControllerTask');
+
class ViewTaskComment extends Model {
var $name = 'ViewTaskComment';
var $useTable = 'comments';
@@ -83,6 +86,7 @@ function startTest() {
$this->Task =& new MockViewTask($this->Dispatcher);
$this->Task->Dispatch =& $this->Dispatcher;
$this->Task->Template =& new TemplateTask($this->Dispatcher);
+ $this->Task->Controller =& new ViewTaskMockControllerTask();
}
/**
@@ -164,5 +168,47 @@ function testBakeActions() {
$this->Task->bakeActions(array('view', 'edit', 'index'), array());
@rmdir(TMP . 'view_task_comments');
}
+
+/**
+ * test baking a customAction (non crud)
+ *
+ * @return void
+ **/
+ function testCustomAction() {
+ $this->Task->path = TMP;
+ $this->Task->controllerName = 'ViewTaskComments';
+ $this->Task->controllerPath = 'view_task_comments';
+ $this->Task->params['app'] = APP;
+
+ $this->Task->setReturnValueAt(0, 'in', '');
+ $this->Task->setReturnValueAt(1, 'in', 'my_action');
+ $this->Task->setReturnValueAt(2, 'in', 'y');
+ $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'my_action.ctp', '*'));
+
+ $this->Task->customAction();
+ @rmdir(TMP . 'view_task_comments');
+ }
+
+/**
+ * Test all()
+ *
+ * @return void
+ **/
+ function testExecuteIntoAll() {
+ $this->Task->path = TMP;
+ $this->Task->args[0] = 'all';
+
+ $this->Task->Controller->setReturnValue('listAll', array('view_task_comments'));
+ $this->Task->Controller->expectOnce('listAll');
+
+ $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*'));
+ $this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'view.ctp', '*'));
+ $this->Task->expectAt(2, 'createFile', array(TMP . 'view_task_comments' . DS . 'add.ctp', '*'));
+ $this->Task->expectAt(3, 'createFile', array(TMP . 'view_task_comments' . DS . 'edit.ctp', '*'));
+
+ $this->Task->execute();
+ @rmdir(TMP . 'view_task_comments');
+ }
+
}
?>

0 comments on commit 8040cc3

Please sign in to comment.