Permalink
Browse files

Merge branch '2.0-form-helper' into 2.0

  • Loading branch information...
2 parents c6c1bf1 + d5ce09c commit 108505a6a0c414128e3a241b6ea4505e28d7f793 @lorenzo lorenzo committed Jul 16, 2011
@@ -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
@@ -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
@@ -806,34 +804,25 @@ 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, $className) = pluginSplit($model);
+ $this->request->params['models'][$model] = compact('plugin', 'className');
}
- $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;
- }
- }
+ } if ($this->uses === false || $this->uses === array()) {
+ $this->request->params['models'][$this->modelClass] = array('plugin' => $this->plugin, 'className' => $this->modelClass);
+ }
+
+ $models = ClassRegistry::keys();
+ foreach ($models as $currentModel) {
+ $currentObject = ClassRegistry::getObject($currentModel);
+ if (is_a($currentObject, 'Model')) {
+ $className = get_class($currentObject);
+ list($plugin, $package) = pluginSplit(App::location($className));
+ $this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
+ $View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
}
}
View
@@ -587,6 +587,19 @@ public static function load($className) {
return false;
}
+/**
+ * Returns the package name where a class was defined to be located at
+ *
+ * @param string $className name of the class to obtain the package name from
+ * @return string package name or null if not declared
+ */
+ public static function location($className) {
+ if (!empty(self::$__classMap[$className])) {
+ return self::$__classMap[$className];
+ }
+ return null;
+ }
+
/**
* Finds classes based on $name or specific file(s) to search. Calling App::import() will
* not construct any classes contained in the files. It will only find and require() the file.
View
@@ -702,10 +702,13 @@ private function __constructLinkedModel($assoc, $className = null, $plugin = nul
}
if (!isset($this->{$assoc}) || $this->{$assoc}->name !== $className) {
- $model = array('class' => $plugin . '.' . $className, 'alias' => $assoc);
+ if ($plugin) {
+ $plugin .= '.';
+ }
+ $model = array('class' => $plugin . $className, 'alias' => $assoc);
$this->{$assoc} = ClassRegistry::init($model);
if ($plugin) {
- ClassRegistry::addObject($plugin . '.' . $className, $this->{$assoc});
+ ClassRegistry::addObject($plugin . $className, $this->{$assoc});
}
if ($assoc) {
$this->tableToModel[$this->{$assoc}->table] = $assoc;
@@ -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);
@@ -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);
@@ -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';
@@ -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' => array('plugin' => null, 'className' => 'ControllerAlias'),
+ 'ControllerComment' => array('plugin' => null, 'className' => 'ControllerComment'),
+ 'ControllerPost' => array('plugin' => null, 'className' => 'ControllerPost')
+ );
+ $this->assertEqual($expectedModels, $Controller->request->params['models']);
+
+
$Controller->ControllerComment->validationErrors = array();
ClassRegistry::flush();
@@ -998,6 +1006,7 @@ public function testSetAction() {
* @return void
*/
public function testValidateErrors() {
+ ClassRegistry::flush();
$request = new CakeRequest('controller_posts/index');
$TestController = new TestController($request);
@@ -736,4 +736,14 @@ public function testLoadClassInLibs() {
App::uses('TestUtilityClass', 'Utility');
$this->assertTrue(class_exists('CustomLibClass'));
}
+
+/**
+ * Tests that App::location() returns the defined path for a class
+ *
+ * @return void
+ */
+ public function testClassLocation() {
+ App::uses('MyCustomClass', 'MyPackage/Name');
+ $this->assertEquals('MyPackage/Name', App::location('MyCustomClass'));
+ }
}
@@ -280,4 +280,12 @@ public function testPluginAppModel() {
$this->assertSame($PluginUser, $PluginUserCopy);
CakePlugin::unload();
}
+
+/**
+ * Tests that passing the string parameter to init() will return false if the model does not exists
+ *
+ */
+ public function testInitStrict() {
+ $this->assertFalse(ClassRegistry::init('NonExistent', true));
+ }
}
Oops, something went wrong.

0 comments on commit 108505a

Please sign in to comment.