Adding support for -admin flag when baking a controller's views. Using -admin will only bake admin methods. Refactoring some methods, removing newlines.

This commit is contained in:
mark_story 2009-07-16 23:12:47 -04:00
parent 22c98cc712
commit 7b06ba2b3d
2 changed files with 101 additions and 38 deletions

View file

@ -88,7 +88,6 @@ class ViewTask extends Shell {
*/
function initialize() {
}
/**
* Execution method always used for tasks
*
@ -129,6 +128,7 @@ class ViewTask extends Shell {
$this->bake($action, true);
} else {
$vars = $this->__loadController();
$methods = $this->_methodsToBake();
$methods = array_diff(
array_map('strtolower', get_class_methods($this->controllerName . 'Controller')),
array_map('strtolower', get_class_methods('appcontroller'))
@ -136,9 +136,15 @@ class ViewTask extends Shell {
if (empty($methods)) {
$methods = $this->scaffoldActions;
}
$adminDelete = null;
$adminRoute = Configure::read('Routing.admin');
if ($adminRoute && isset($this->params['admin'])) {
foreach ($methods as $i => $method) {
if (strpos($method, $adminRoute . '_') === false) {
unset($methods[$i]);
}
}
}
$adminDelete = null;
if (!empty($adminRoute)) {
$adminDelete = $adminRoute . '_delete';
}
@ -151,7 +157,30 @@ class ViewTask extends Shell {
}
}
}
/**
* Get a list of actions that can / should have views baked for them.
*
* @return array Array of action names that should be baked
**/
function _methodsToBake() {
$methods = array_diff(
array_map('strtolower', get_class_methods($this->controllerName . 'Controller')),
array_map('strtolower', get_class_methods('appcontroller'))
);
if (empty($methods)) {
$methods = $this->scaffoldActions;
}
$adminRoute = Configure::read('Routing.admin');
foreach ($methods as $i => $method) {
if ($method == 'delete' || $method = $adminRoute . '_delete' || $method{0} == '_') {
unset($methods[$i]);
}
if ($adminRoute && isset($this->params['admin']) && strpos($method, $adminRoute . '_') === false) {
unset($methods[$i]);
}
}
return $methods;
}
/**
* Bake All views for All controllers.
*
@ -172,7 +201,6 @@ class ViewTask extends Shell {
}
}
}
/**
* Handles interactive baking
*
@ -226,7 +254,6 @@ class ViewTask extends Shell {
$this->customAction();
}
}
/**
* Loads Controller and sets variables for the template
* Available template variables
@ -283,7 +310,6 @@ class ViewTask extends Shell {
return compact('modelClass', 'schema', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
'singularHumanName', 'pluralHumanName', 'fields','associations');
}
/**
* Bake a view file for each of the supplied actions
*
@ -296,7 +322,6 @@ class ViewTask extends Shell {
$this->bake($action, $content);
}
}
/**
* handle creation of baking a custom action view file
*
@ -326,7 +351,6 @@ class ViewTask extends Shell {
$this->out(__('Bake Aborted.', true));
}
}
/**
* Assembles and writes bakes the view file.
*
@ -346,7 +370,6 @@ class ViewTask extends Shell {
$filename = $path . $this->controllerPath . DS . Inflector::underscore($action) . '.ctp';
return $this->createFile($filename, $content);
}
/**
* Builds content from template and variables
*
@ -385,7 +408,6 @@ class ViewTask extends Shell {
$this->err(sprintf(__('Template for %s could not be found', true), $template));
return false;
}
/**
* Displays help contents
*
@ -398,16 +420,18 @@ class ViewTask extends Shell {
$this->out('Commands:');
$this->out('');
$this->out("view <controller>");
$this->out("\twill read the given controller for methods");
$this->out("\tWill read the given controller for methods");
$this->out("\tand bake corresponding views.");
$this->out("\tUsing the -admin flag will only bake views for actions");
$this->out("\tthat begin with Routing.admin.");
$this->out("\tIf var scaffold is found it will bake the CRUD actions");
$this->out("\t(index,view,add,edit)");
$this->out('');
$this->out("view <controller> <action>");
$this->out("\twill bake a template. core templates: (index, add, edit, view)");
$this->out("\tWill bake a template. core templates: (index, add, edit, view)");
$this->out('');
$this->out("view <controller> <template> <alias>");
$this->out("\twill use the template specified");
$this->out("\tWill use the template specified");
$this->out("\tbut name the file based on the alias");
$this->out('');
$this->out("view all");
@ -415,7 +439,6 @@ class ViewTask extends Shell {
$this->out("\tRequires that models and controllers exist.");
$this->_stop();
}
/**
* Returns associations for controllers models.
*

View file

@ -1,9 +1,9 @@
<?php
/* SVN FILE: $Id$ */
/**
* TestTaskTest file
* ViewTask Test file
*
* Test Case for test generation shell task
* Test Case for view generation shell task
*
* PHP versions 4 and 5
*
@ -25,7 +25,6 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Core', 'Shell');
App::import('Core', array('Controller', 'Model'));
if (!defined('DISABLE_AUTO_DISPATCH')) {
define('DISABLE_AUTO_DISPATCH', true);
@ -85,6 +84,32 @@ class ViewTaskCommentsController extends Controller {
}
}
class ViewTaskArticlesController extends Controller {
var $name = 'ViewTaskArticles';
function index() {
}
function add() {
}
function admin_index() {
}
function admin_add() {
}
function admin_view() {
}
function admin_edit() {
}
function admin_delete() {
}
}
/**
* ViewTaskTest class
@ -96,7 +121,7 @@ class ViewTaskTest extends CakeTestCase {
var $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
/**
* setUp method
* startTest method
*
* @return void
* @access public
@ -111,9 +136,8 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->Project =& new ViewTaskMockProjectTask();
$this->Task->path = TMP;
}
/**
* tearDown method
* endTest method
*
* @return void
* @access public
@ -121,7 +145,6 @@ class ViewTaskTest extends CakeTestCase {
function endTest() {
ClassRegistry::flush();
}
/**
* Test getContent and parsing of Templates.
*
@ -151,13 +174,13 @@ class ViewTaskTest extends CakeTestCase {
$this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
$this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
}
/**
* test getContent() using an admin_prefixed action.
*
* @return void
**/
function testGetContentWithAdminAction() {
$_back = Configure::read('Routing.admin');
Configure::write('Routing.admin', 'admin');
$vars = array(
'modelClass' => 'TestViewModel',
@ -181,8 +204,9 @@ class ViewTaskTest extends CakeTestCase {
$this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'id\'\]/', $result);
$this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
$this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
}
Configure::write('Routing.admin', $_back);
}
/**
* test Bake method
*
@ -202,12 +226,11 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->bake('edit', true);
$this->Task->expectAt(2, 'createFile', array(
TMP . 'view_task_comments' . DS . 'index.ctp',
TMP . 'view_task_comments' . DS . 'index.ctp',
new PatternExpectation('/\$viewTaskComment\[\'Article\'\]\[\'title\'\]/')
));
$this->Task->bake('index', true);
}
/**
* test bake() with a -plugin param
*
@ -222,7 +245,6 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->expectAt(0, 'createFile', array($path, '*'));
$this->Task->bake('view', true);
}
/**
* test bake actions baking multiple actions.
*
@ -233,7 +255,7 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->controllerPath = 'view_task_comments';
$this->Task->expectAt(0, 'createFile', array(
TMP . 'view_task_comments' . DS . 'view.ctp',
TMP . 'view_task_comments' . DS . 'view.ctp',
new PatternExpectation('/ViewTaskComments/')
));
$this->Task->expectAt(1, 'createFile', array(
@ -247,7 +269,6 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->bakeActions(array('view', 'edit', 'index'), array());
}
/**
* test baking a customAction (non crud)
*
@ -265,7 +286,6 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->customAction();
}
/**
* Test all()
*
@ -285,7 +305,6 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->execute();
}
/**
* test `cake bake view $controller view`
*
@ -299,9 +318,9 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'view.ctp', '*'));
$this->Task->execute();
}
/**
* test `cake bake view $controller`
* test `cake bake view $controller`
* Ensure that views are only baked for actions that exist in the controller.
*
* @return void
**/
@ -311,9 +330,30 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->expectCallCount('createFile', 2);
$this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*'));
$this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'add.ctp', '*'));
$this->Task->execute();
}
/**
* test `cake bake view $controller -admin`
* Which only bakes admin methods, not non-admin methods.
*
* @return void
**/
function testExecuteWithControllerAndAdminFlag() {
$_back = Configure::read('Routing.admin');
Configure::write('Routing.admin', 'admin');
$this->Task->args[0] = 'ViewTaskArticles';
$this->Task->params['admin'] = 1;
$this->Task->expectCallCount('createFile', 4);
$this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_articles' . DS . 'admin_index.ctp', '*'));
$this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_articles' . DS . 'admin_add.ctp', '*'));
$this->Task->expectAt(2, 'createFile', array(TMP . 'view_task_articles' . DS . 'admin_view.ctp', '*'));
$this->Task->expectAt(3, 'createFile', array(TMP . 'view_task_articles' . DS . 'admin_edit.ctp', '*'));
$this->Task->execute();
Configure::write('Routing.admin', $_back);
}
/**
* test execute into interactive.
*
@ -322,6 +362,7 @@ class ViewTaskTest extends CakeTestCase {
function testExecuteInteractive() {
$this->Task->connection = 'test_suite';
$this->Task->args = array();
$this->Task->params = array();
$this->Task->Controller->setReturnValue('getName', 'ViewTaskComments');
$this->Task->setReturnValue('in', 'y');
@ -349,7 +390,6 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->execute();
}
/**
* test execute into interactive() with admin methods.
*
@ -359,20 +399,20 @@ class ViewTaskTest extends CakeTestCase {
Configure::write('Routing.admin', 'admin');
$this->Task->connection = 'test_suite';
$this->Task->args = array();
$this->Task->Controller->setReturnValue('getName', 'ViewTaskComments');
$this->Task->Project->setReturnValue('getAdmin', 'admin_');
$this->Task->setReturnValueAt(0, 'in', 'y');
$this->Task->setReturnValueAt(1, 'in', 'n');
$this->Task->setReturnValueAt(2, 'in', 'y');
$this->Task->expectCallCount('createFile', 4);
$this->Task->expectAt(0, 'createFile', array(
TMP . 'view_task_comments' . DS . 'admin_index.ctp',
TMP . 'view_task_comments' . DS . 'admin_index.ctp',
new PatternExpectation('/ViewTaskComment/')
));
$this->Task->expectAt(1, 'createFile', array(
TMP . 'view_task_comments' . DS . 'admin_view.ctp',
TMP . 'view_task_comments' . DS . 'admin_view.ctp',
new PatternExpectation('/ViewTaskComment/')
));
$this->Task->expectAt(2, 'createFile', array(