Skip to content
Permalink
Browse files

Updating Scaffold to work with any single prefix. You can set $scaffo…

…ld = to any prefix defined in Routing.prefixes.

Tests added.
  • Loading branch information...
markstory committed Oct 31, 2009
1 parent 15a98fa commit 3a59bf5ed6dbdec469052eacae2944fe27be94fc
Showing with 80 additions and 24 deletions.
  1. +23 −14 cake/libs/controller/scaffold.php
  2. +57 −10 cake/tests/cases/libs/controller/scaffold.test.php
@@ -434,34 +434,38 @@ function __scaffoldError() {
/**
* When methods are now present in a controller
* scaffoldView is used to call default Scaffold methods if:
* <code>
* var $scaffold;
* </code>
* is placed in the controller's class definition.
* `var $scaffold;` is placed in the controller's class definition.
*
* @param array $params Parameters for scaffolding
* @return mixed A rendered view of scaffold action, or showing the error
* @access private
*/
function __scaffold($params) {
$db = &ConnectionManager::getDataSource($this->ScaffoldModel->useDbConfig);
$admin = Configure::read('Routing.admin');
$prefixes = Configure::read('Routing.prefixes');
$scaffoldPrefix = $this->scaffoldActions;
if (isset($db)) {
if (empty($this->scaffoldActions)) {
$this->scaffoldActions = array(
'index', 'list', 'view', 'add', 'create', 'edit', 'update', 'delete'
);
} elseif (!empty($admin) && $this->scaffoldActions === $admin) {
} elseif (!empty($prefixes) && in_array($this->scaffoldActions, $prefixes)) {
$this->scaffoldActions = array(
$admin .'_index', $admin .'_list', $admin .'_view', $admin .'_add',
$admin .'_create', $admin .'_edit', $admin .'_update', $admin .'_delete'
$scaffoldPrefix . '_index',
$scaffoldPrefix . '_list',
$scaffoldPrefix . '_view',
$scaffoldPrefix . '_add',
$scaffoldPrefix . '_create',
$scaffoldPrefix . '_edit',
$scaffoldPrefix . '_update',
$scaffoldPrefix . '_delete'
);
}
if (in_array($params['action'], $this->scaffoldActions)) {
if (!empty($admin)) {
$params['action'] = str_replace($admin . '_', '', $params['action']);
if (!empty($prefixes)) {
$params['action'] = str_replace($scaffoldPrefix . '_', '', $params['action']);
}
switch ($params['action']) {
case 'index':
@@ -556,10 +560,15 @@ function _getViewFileName($name = null) {
$name = $this->action;
}
$name = Inflector::underscore($name);
$admin = Configure::read('Routing.admin');
$prefixes = Configure::read('Routing.prefixes');
if (!empty($admin) && strpos($name, $admin . '_') !== false) {
$name = substr($name, strlen($admin) + 1);
if (!empty($prefixes)) {
foreach ($prefixes as $prefix) {
if (strpos($name, $prefix . '_') !== false) {
$name = substr($name, strlen($prefix) + 1);
break;
}
}
}
if ($name === 'add') {
@@ -315,8 +315,8 @@ function endTest() {
* @return void
*/
function testGetViewFilename() {
$_admin = Configure::read('Routing.admin');
Configure::write('Routing.admin', 'admin');
$_admin = Configure::read('Routing.prefixes');
Configure::write('Routing.prefixes', array('admin'));
$this->Controller->action = 'index';
$ScaffoldView =& new TestScaffoldView($this->Controller);
@@ -385,7 +385,7 @@ function testGetViewFilename() {
. DS .'test_plugin' . DS . 'views' . DS . 'tests' . DS . 'scaffold.edit.ctp';
$this->assertEqual($result, $expected);
Configure::write('Routing.admin', $_admin);
Configure::write('Routing.prefixes', $_admin);
}
/**
@@ -499,7 +499,7 @@ function testEditScaffold() {
new Scaffold($this->Controller, $params);
$result = ob_get_clean();
$this->assertPattern('/<form id="ScaffoldMockEditForm" method="post" action="\/scaffold_mock\/edit\/1">/', $result);
$this->assertPattern('/<form id="ScaffoldMockEditForm" method="post" action="\/scaffold_mock\/edit\/1"/', $result);
$this->assertPattern('/<legend>Edit Scaffold Mock<\/legend>/', $result);
$this->assertPattern('/input type="hidden" name="data\[ScaffoldMock\]\[id\]" value="1" id="ScaffoldMockId"/', $result);
@@ -509,16 +509,17 @@ function testEditScaffold() {
$this->assertPattern('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result);
$this->assertPattern('/<li><a href="\/scaffold_mock\/delete\/1"[^>]*>Delete<\/a>\s*<\/li>/', $result);
}
/**
* Test Admin Index Scaffolding.
*
* @access public
* @return void
**/
function testAdminIndexScaffold() {
$_backAdmin = Configure::read('Routing.admin');
$_backAdmin = Configure::read('Routing.prefixes');
Configure::write('Routing.admin', 'admin');
Configure::write('Routing.prefixes', array('admin'));
$params = array(
'plugin' => null,
'pass' => array(),
@@ -551,7 +552,7 @@ function testAdminIndexScaffold() {
//TODO: add testing for table generation
$this->assertPattern('/<li><a href="\/admin\/scaffold_mock\/add\/">New Scaffold Mock<\/a><\/li>/', $result);
Configure::write('Routing.admin', $_backAdmin);
Configure::write('Routing.prefixes', $_backAdmin);
}
/**
@@ -561,9 +562,9 @@ function testAdminIndexScaffold() {
* @return void
**/
function testAdminEditScaffold() {
$_backAdmin = Configure::read('Routing.admin');
$_backAdmin = Configure::read('Routing.prefixes');
Configure::write('Routing.admin', 'admin');
Configure::write('Routing.prefixes', array('admin'));
$params = array(
'plugin' => null,
'pass' => array(),
@@ -594,8 +595,54 @@ function testAdminEditScaffold() {
$this->assertPattern('#admin/scaffold_mock/edit/1#', $result);
$this->assertPattern('#Scaffold Mock#', $result);
Configure::write('Routing.admin', $_backAdmin);
Configure::write('Routing.prefixes', $_backAdmin);
}
/**
* Test Admin Index Scaffolding.
*
* @access public
* @return void
**/
function testMultiplePrefixScaffold() {
$_backAdmin = Configure::read('Routing.prefixes');
Configure::write('Routing.prefixes', array('admin', 'member'));
$params = array(
'plugin' => null,
'pass' => array(),
'form' => array(),
'named' => array(),
'prefix' => 'member',
'url' => array('url' =>'member/scaffold_mock'),
'controller' => 'scaffold_mock',
'action' => 'member_index',
'member' => 1,
);
//reset, and set router.
Router::reload();
Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/member/scaffold_mock', 'webroot' => '/')));
$this->Controller->params = $params;
$this->Controller->controller = 'scaffold_mock';
$this->Controller->base = '/';
$this->Controller->action = 'member_index';
$this->Controller->here = '/tests/member/scaffold_mock';
$this->Controller->webroot = '/';
$this->Controller->scaffold = 'member';
$this->Controller->constructClasses();
ob_start();
$Scaffold = new Scaffold($this->Controller, $params);
$result = ob_get_clean();
$this->assertPattern('/<h2>Scaffold Mock<\/h2>/', $result);
$this->assertPattern('/<table cellpadding="0" cellspacing="0">/', $result);
//TODO: add testing for table generation
$this->assertPattern('/<li><a href="\/member\/scaffold_mock\/add\/">New Scaffold Mock<\/a><\/li>/', $result);
Configure::write('Routing.prefixes', $_backAdmin);
}
}
/**

0 comments on commit 3a59bf5

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