Skip to content
Permalink
Browse files

Adding support for -admin flag when baking a controller's views. Usin…

…g -admin will only bake admin methods. Refactoring some methods, removing newlines.
  • Loading branch information...
markstory committed Jul 17, 2009
1 parent 22c98cc commit 7b06ba2b3d74a41e086333135376a9b66a694d17
Showing with 101 additions and 38 deletions.
  1. +38 −15 cake/console/libs/tasks/view.php
  2. +63 −23 cake/tests/cases/console/libs/tasks/view.test.php
@@ -88,7 +88,6 @@ class ViewTask extends Shell {
*/
function initialize() {
}
/**
* Execution method always used for tasks
*
@@ -129,16 +128,23 @@ function execute() {
$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'))
);
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 @@ function execute() {
}
}
}
/**
* 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 @@ function all() {
}
}
}
/**
* Handles interactive baking
*
@@ -226,7 +254,6 @@ function __interactive() {
$this->customAction();
}
}
/**
* Loads Controller and sets variables for the template
* Available template variables
@@ -283,7 +310,6 @@ function __loadController() {
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 @@ function bakeActions($actions, $vars) {
$this->bake($action, $content);
}
}
/**
* handle creation of baking a custom action view file
*
@@ -326,7 +351,6 @@ function customAction() {
$this->out(__('Bake Aborted.', true));
}
}
/**
* Assembles and writes bakes the view file.
*
@@ -346,7 +370,6 @@ function bake($action, $content = '') {
$filename = $path . $this->controllerPath . DS . Inflector::underscore($action) . '.ctp';
return $this->createFile($filename, $content);
}
/**
* Builds content from template and variables
*
@@ -385,7 +408,6 @@ function getContent($template = null, $vars = null) {
$this->err(sprintf(__('Template for %s could not be found', true), $template));
return false;
}
/**
* Displays help contents
*
@@ -398,24 +420,25 @@ function help() {
$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");
$this->out("\tBake all CRUD action views for all controllers.");
$this->out("\tRequires that models and controllers exist.");
$this->_stop();
}
/**
* Returns associations for controllers models.
*
@@ -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 @@ function add() {
}
}
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,17 +136,15 @@ function startTest() {
$this->Task->Project =& new ViewTaskMockProjectTask();
$this->Task->path = TMP;
}
/**
* tearDown method
* endTest method
*
* @return void
* @access public
*/
function endTest() {
ClassRegistry::flush();
}
/**
* Test getContent and parsing of Templates.
*
@@ -151,13 +174,13 @@ function testGetContent() {
$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 @@ function testGetContentWithAdminAction() {
$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 @@ function testBake() {
$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 @@ function testBakeWithPlugin() {
$this->Task->expectAt(0, 'createFile', array($path, '*'));
$this->Task->bake('view', true);
}
/**
* test bake actions baking multiple actions.
*
@@ -233,7 +255,7 @@ function testBakeActions() {
$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 @@ function testBakeActions() {
$this->Task->bakeActions(array('view', 'edit', 'index'), array());
}
/**
* test baking a customAction (non crud)
*
@@ -265,7 +286,6 @@ function testCustomAction() {
$this->Task->customAction();
}
/**
* Test all()
*
@@ -285,7 +305,6 @@ function testExecuteIntoAll() {
$this->Task->execute();
}
/**
* test `cake bake view $controller view`
*
@@ -299,9 +318,9 @@ function testExecuteWithActionParam() {
$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 @@ function testExecuteWithController() {
$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 @@ function testExecuteWithController() {
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 @@ function testExecuteInteractive() {
$this->Task->execute();
}
/**
* test execute into interactive() with admin methods.
*
@@ -359,20 +399,20 @@ function testExecuteInteractiveWithAdmin() {
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(

0 comments on commit 7b06ba2

Please sign in to comment.
You can’t perform that action at this time.