Skip to content

Commit

Permalink
Add controller option.
Browse files Browse the repository at this point in the history
This option allows you to bake views based on a model for a different
controller. For example bake Tasks related views into the Timesheet
controller.
  • Loading branch information
markstory committed Mar 25, 2014
1 parent dba6d32 commit 2b0f45b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 39 deletions.
73 changes: 37 additions & 36 deletions src/Console/Command/Task/ViewTask.php
Expand Up @@ -55,6 +55,13 @@ class ViewTask extends BakeTask {
*/
public $controllerClass = null;

/**
* Name of the table views are being baked against.
*
* @var string
*/
public $tableName = null;

/**
* The template file to use
*
Expand Down Expand Up @@ -119,7 +126,11 @@ public function execute() {
return $this->all();
}

$this->controller($this->args[0]);
$controller = null;
if (!empty($this->params['controller'])) {
$controller = $this->params['controller'];
}
$this->controller($this->args[0], $controller);

if (isset($this->args[1])) {
$this->template = $this->args[1];
Expand Down Expand Up @@ -148,12 +159,16 @@ public function execute() {
/**
* Set the controller related properties.
*
* @param string $name The controller name.
* @param string $table The table/model that is being baked.
* @param string $controller The controller name if specified.
* @return void
*/
public function controller($name) {
$name = $this->_controllerName($name);
$this->controllerName = $name;
public function controller($table, $controller = null) {
$this->tableName = $this->_controllerName($table);
if (empty($controller)) {
$controller = $this->tableName;
}
$this->controllerName = $controller;

$plugin = $prefix = null;
if (!empty($this->params['plugin'])) {
Expand All @@ -162,7 +177,7 @@ public function controller($name) {
if (!empty($this->params['prefix'])) {
$prefix = $this->params['prefix'] . '/';
}
$this->controllerClass = App::className($plugin . $prefix . $name, 'Controller', 'Controller');
$this->controllerClass = App::className($plugin . $prefix . $controller, 'Controller', 'Controller');
}

/**
Expand Down Expand Up @@ -238,46 +253,30 @@ public function all() {
* @return array Returns an variables to be made available to a view template
*/
protected function _loadController() {
if (!$this->controllerName) {
$this->err(__d('cake_console', 'Controller not found'));
}

$plugin = null;
if ($this->plugin) {
$plugin = $this->plugin . '.';
}

if (class_exists($this->controllerClass)) {
$controllerObj = new $this->controllerClass();
$controllerObj->plugin = $this->plugin;
$controllerObj->constructClasses();
$modelClass = $controllerObj->modelClass;
$modelObj = $controllerObj->{$modelClass};
} else {
$modelObj = TableRegistry::get($this->controllerName);
}
$modelObj = TableRegistry::get($this->tableName);

$primaryKey = [];
$displayField = null;
$singularVar = Inflector::variable(Inflector::singularize($this->controllerName));
$singularHumanName = $this->_singularHumanName($this->controllerName);
$fields = $schema = $keyFields = $associations = [];

if ($modelObj) {
$primaryKey = (array)$modelObj->primaryKey();
$displayField = $modelObj->displayField();
$singularVar = $this->_singularName($this->controllerName);
$singularHumanName = $this->_singularHumanName($this->controllerName);
$schema = $modelObj->schema();
$fields = $schema->columns();
$associations = $this->_associations($modelObj);
$keyFields = [];
if (!empty($associations['BelongsTo'])) {
foreach ($associations['BelongsTo'] as $assoc) {
$keyFields[$assoc['foreignKey']] = $assoc['variable'];
}

$primaryKey = (array)$modelObj->primaryKey();
$displayField = $modelObj->displayField();
$singularVar = $this->_singularName($this->controllerName);
$singularHumanName = $this->_singularHumanName($this->controllerName);
$schema = $modelObj->schema();
$fields = $schema->columns();
$associations = $this->_associations($modelObj);
$keyFields = [];
if (!empty($associations['BelongsTo'])) {
foreach ($associations['BelongsTo'] as $assoc) {
$keyFields[$assoc['foreignKey']] = $assoc['variable'];
}
}

$pluralVar = Inflector::variable($this->controllerName);
$pluralHumanName = $this->_pluralHumanName($this->controllerName);

Expand Down Expand Up @@ -439,6 +438,8 @@ public function getOptionParser() {
'boolean' => true,
'short' => 'f',
'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
])->addOption('controller', [
'help' => __d('cake_console', 'The controller name if you have a controller that does not follow conventions.')
])->addOption('prefix', [
'help' => __d('cake_console', 'The routing prefix to generate views for.'),
])->addSubcommand('all', [
Expand Down
53 changes: 50 additions & 3 deletions tests/TestCase/Console/Command/Task/ViewTaskTest.php
Expand Up @@ -158,6 +158,7 @@ public function testController() {
public function testControllerVariations($name) {
$this->Task->controller($name);
$this->assertEquals('ViewTaskComments', $this->Task->controllerName);
$this->assertEquals('ViewTaskComments', $this->Task->tableName);
}

/**
Expand All @@ -169,6 +170,7 @@ public function testControllerPlugin() {
$this->Task->params['plugin'] = 'TestPlugin';
$this->Task->controller('Tests');
$this->assertEquals('Tests', $this->Task->controllerName);
$this->assertEquals('Tests', $this->Task->tableName);
$this->assertEquals(
'TestPlugin\Controller\TestsController',
$this->Task->controllerClass
Expand All @@ -184,6 +186,7 @@ public function testControllerPrefix() {
$this->Task->params['prefix'] = 'Admin';
$this->Task->controller('Posts');
$this->assertEquals('Posts', $this->Task->controllerName);
$this->assertEquals('Posts', $this->Task->tableName);
$this->assertEquals(
'TestApp\Controller\Admin\PostsController',
$this->Task->controllerClass
Expand All @@ -192,12 +195,28 @@ public function testControllerPrefix() {
$this->Task->params['plugin'] = 'TestPlugin';
$this->Task->controller('Comments');
$this->assertEquals('Comments', $this->Task->controllerName);
$this->assertEquals('Comments', $this->Task->tableName);
$this->assertEquals(
'TestPlugin\Controller\Admin\CommentsController',
$this->Task->controllerClass
);
}

/**
* test controller with a non-conventional controller name
*
* @return void
*/
public function testControllerWithOverride() {
$this->Task->controller('Comments', 'Posts');
$this->assertEquals('Posts', $this->Task->controllerName);
$this->assertEquals('Comments', $this->Task->tableName);
$this->assertEquals(
'TestApp\Controller\PostsController',
$this->Task->controllerClass
);
}

/**
* Test getPath()
*
Expand Down Expand Up @@ -313,6 +332,7 @@ public function testGetContentWithRoutingPrefix() {
*/
public function testBakeView() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';

$this->Task->expects($this->at(0))
Expand All @@ -332,6 +352,7 @@ public function testBakeView() {
*/
public function testBakeEdit() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';

$this->Task->expects($this->at(0))->method('createFile')
Expand All @@ -352,6 +373,7 @@ public function testBakeEdit() {
*/
public function testBakeIndex() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';

$this->Task->expects($this->at(0))->method('createFile')
Expand All @@ -369,6 +391,7 @@ public function testBakeIndex() {
*/
public function testBakeWithNoTemplate() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';

$this->Task->expects($this->never())->method('createFile');
Expand All @@ -382,6 +405,7 @@ public function testBakeWithNoTemplate() {
*/
public function testBakeActions() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';

$this->Task->expects($this->at(0))
Expand Down Expand Up @@ -411,6 +435,7 @@ public function testBakeActions() {
*/
public function testCustomAction() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';

$this->Task->expects($this->any())->method('in')
Expand Down Expand Up @@ -495,12 +520,34 @@ public function testExecuteWithController() {
* @return void
*/
public static function nameVariations() {
return array(array('ViewTaskComments'), array('ViewTaskComment'), array('view_task_comment'));
return [['ViewTaskComments'], ['ViewTaskComment'], ['view_task_comment']];
}

/**
* test `cake bake view $table --controller Blog`
*
* @return void
*/
public function testExecuteWithControllerFlag() {
$this->Task->args[0] = 'Posts';
$this->Task->params['controller'] = 'Blog';

$this->Task->expects($this->exactly(4))
->method('createFile');

$views = array('index.ctp', 'view.ctp', 'add.ctp', 'edit.ctp');
foreach ($views as $i => $view) {
$this->Task->expects($this->at($i))->method('createFile')
->with(
TMP . 'Blog/' . $view,
$this->anything()
);
}
$this->Task->execute();
}

/**
* test `cake bake view $controller --admin`
* Which only bakes admin methods, not non-admin methods.
* test `cake bake view $controller --prefix Admin`
*
* @return void
*/
Expand Down

0 comments on commit 2b0f45b

Please sign in to comment.