Skip to content

Commit

Permalink
Removing the Controller::modelNames property and loading it's content…
Browse files Browse the repository at this point in the history
…s in the uses variable.

Simplifying code in Controller::render()
Setting all models loaded into the request parameters so it can be used later on the Form helper
  • Loading branch information
lorenzo committed Jul 12, 2011
1 parent f8ab1d3 commit e45b35c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
53 changes: 20 additions & 33 deletions lib/Cake/Controller/Controller.php
Expand Up @@ -127,12 +127,6 @@ class Controller extends Object {
*/
public $viewVars = array();

/**
* An array containing the class names of the models this controller uses.
*
* @var array Array of model objects.
*/
public $modelNames = array();

/**
* The name of the view file to render. The name specified
Expand Down Expand Up @@ -596,9 +590,13 @@ public function loadModel($modelClass = null, $id = null) {
if ($modelClass === null) {
$modelClass = $this->modelClass;
}
list($plugin, $modelClass) = pluginSplit($modelClass, true);

$this->modelNames[] = $modelClass;
$this->uses = ($this->uses) ? $this->uses : array();
if (!in_array($modelClass, $this->uses)) {
$this->uses[] = $modelClass;
}

list($plugin, $modelClass) = pluginSplit($modelClass, true);

$this->{$modelClass} = ClassRegistry::init(array(
'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id
Expand Down Expand Up @@ -806,34 +804,23 @@ public function render($view = null, $layout = null) {
App::uses($viewClass, $plugin . 'View');
}

$this->request->params['models'] = $this->modelNames;

$View = new $viewClass($this);

if (!empty($this->modelNames)) {
$models = array();
foreach ($this->modelNames as $currentModel) {
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
$models[] = Inflector::underscore($currentModel);
}
$isValidModel = (
isset($this->$currentModel) && is_a($this->$currentModel, 'Model') &&
!empty($this->$currentModel->validationErrors)
);
if ($isValidModel) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
$this->$currentModel->validationErrors;
}
if (!empty($this->uses)) {
foreach ($this->uses as $model) {
list($plugin, $model) = pluginSplit($model);
$this->request->params['models'][$model] = $plugin;
}
$models = array_diff(ClassRegistry::keys(), $models);
foreach ($models as $currentModel) {
if (ClassRegistry::isKeySet($currentModel)) {
$currentObject = ClassRegistry::getObject($currentModel);
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
$currentObject->validationErrors;
}
}
}

$models = ClassRegistry::keys();
foreach ($models as $currentModel) {
$currentObject = ClassRegistry::getObject($currentModel);
if (is_a($currentObject, 'Model')) {
list($plugin, $package) = pluginSplit(App::location(get_class($currentObject)));
$this->request->params['models'][$currentObject->alias] = $plugin;
$View->validationErrors[$currentObject->alias] =&
$currentObject->validationErrors;
}
}

Expand Down
15 changes: 12 additions & 3 deletions lib/Cake/Test/Case/Controller/ControllerTest.php
Expand Up @@ -425,7 +425,7 @@ public function testLoadModel() {
$result = $Controller->loadModel('ControllerPost');
$this->assertTrue($result);
$this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
$this->assertTrue(in_array('ControllerPost', $Controller->modelNames));
$this->assertTrue(in_array('ControllerPost', $Controller->uses));

ClassRegistry::flush();
unset($Controller);
Expand Down Expand Up @@ -456,7 +456,7 @@ public function testLoadModelInPlugins() {
$result = $Controller->loadModel('Comment');
$this->assertTrue($result);
$this->assertInstanceOf('Comment', $Controller->Comment);
$this->assertTrue(in_array('Comment', $Controller->modelNames));
$this->assertTrue(in_array('Comment', $Controller->uses));

ClassRegistry::flush();
unset($Controller);
Expand Down Expand Up @@ -613,6 +613,7 @@ public function testRender() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
), true);
ClassRegistry::flush();
$request = new CakeRequest('controller_posts/index');
$request->params['action'] = 'index';

Expand All @@ -636,13 +637,20 @@ public function testRender() {
$Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
$expected = $Controller->ControllerComment->validationErrors;

ClassRegistry::flush();
$Controller->viewPath = 'Posts';
$result = $Controller->render('index');
$View = $Controller->View;
$this->assertTrue(isset($View->validationErrors['ControllerComment']));
$this->assertEqual($expected, $View->validationErrors['ControllerComment']);

$expectedModels = array(
'ControllerAlias' => null,
'ControllerComment' => null,
'ControllerPost' => null
);
$this->assertEqual($expectedModels, $Controller->request->params['models']);


$Controller->ControllerComment->validationErrors = array();
ClassRegistry::flush();

Expand Down Expand Up @@ -998,6 +1006,7 @@ public function testSetAction() {
* @return void
*/
public function testValidateErrors() {
ClassRegistry::flush();
$request = new CakeRequest('controller_posts/index');

$TestController = new TestController($request);
Expand Down

0 comments on commit e45b35c

Please sign in to comment.