Moving entity() and its related attributes to Helper

Removing entity() and its attributes from View.
Having that information on View allowed for unwanted side effects,
and seemed like a break of encapsulation.
This commit is contained in:
Mark Story 2011-06-25 20:02:34 -04:00
parent e4d701021e
commit 89258327c6
6 changed files with 63 additions and 137 deletions

View file

@ -775,23 +775,23 @@ class FormHelperTest extends CakeTestCase {
*/ */
public function testDuplicateFieldNameResolution() { public function testDuplicateFieldNameResolution() {
$result = $this->Form->create('ValidateUser'); $result = $this->Form->create('ValidateUser');
$this->assertEqual($this->View->entity(), array('ValidateUser')); $this->assertEqual($this->Form->entity(), array('ValidateUser'));
$result = $this->Form->input('ValidateItem.name'); $result = $this->Form->input('ValidateItem.name');
$this->assertEqual($this->View->entity(), array('ValidateItem', 'name')); $this->assertEqual($this->Form->entity(), array('ValidateItem', 'name'));
$result = $this->Form->input('ValidateUser.name'); $result = $this->Form->input('ValidateUser.name');
$this->assertEqual($this->View->entity(), array('ValidateUser', 'name')); $this->assertEqual($this->Form->entity(), array('ValidateUser', 'name'));
$this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result); $this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
$this->assertPattern('/type="text"/', $result); $this->assertPattern('/type="text"/', $result);
$result = $this->Form->input('ValidateItem.name'); $result = $this->Form->input('ValidateItem.name');
$this->assertEqual($this->View->entity(), array('ValidateItem', 'name')); $this->assertEqual($this->Form->entity(), array('ValidateItem', 'name'));
$this->assertPattern('/name="data\[ValidateItem\]\[name\]"/', $result); $this->assertPattern('/name="data\[ValidateItem\]\[name\]"/', $result);
$this->assertPattern('/<textarea/', $result); $this->assertPattern('/<textarea/', $result);
$result = $this->Form->input('name'); $result = $this->Form->input('name');
$this->assertEqual($this->View->entity(), array('ValidateUser', 'name')); $this->assertEqual($this->Form->entity(), array('ValidateUser', 'name'));
$this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result); $this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
$this->assertPattern('/type="text"/', $result); $this->assertPattern('/type="text"/', $result);
} }

View file

@ -252,7 +252,7 @@ class HelperTest extends CakeTestCase {
*/ */
public function testSetEntity($entity, $expected) { public function testSetEntity($entity, $expected) {
$this->Helper->setEntity($entity); $this->Helper->setEntity($entity);
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
} }
/** /**
@ -262,44 +262,44 @@ class HelperTest extends CakeTestCase {
*/ */
public function testSetEntityScoped() { public function testSetEntityScoped() {
$this->Helper->setEntity('HelperTestPost', true); $this->Helper->setEntity('HelperTestPost', true);
$this->assertEquals(array('HelperTestPost'), $this->View->entity()); $this->assertEquals(array('HelperTestPost'), $this->Helper->entity());
$this->Helper->setEntity('id'); $this->Helper->setEntity('id');
$expected = array('HelperTestPost', 'id'); $expected = array('HelperTestPost', 'id');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('HelperTestComment.body'); $this->Helper->setEntity('HelperTestComment.body');
$expected = array('HelperTestComment', 'body'); $expected = array('HelperTestComment', 'body');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('body'); $this->Helper->setEntity('body');
$expected = array('HelperTestPost', 'body'); $expected = array('HelperTestPost', 'body');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('2.body'); $this->Helper->setEntity('2.body');
$expected = array('HelperTestPost', '2', 'body'); $expected = array('HelperTestPost', '2', 'body');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('Something.else'); $this->Helper->setEntity('Something.else');
$expected = array('Something', 'else'); $expected = array('Something', 'else');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('HelperTestComment.5.id'); $this->Helper->setEntity('HelperTestComment.5.id');
$expected = array('HelperTestComment', 5, 'id'); $expected = array('HelperTestComment', 5, 'id');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('HelperTestComment.id.time'); $this->Helper->setEntity('HelperTestComment.id.time');
$expected = array('HelperTestComment', 'id', 'time'); $expected = array('HelperTestComment', 'id', 'time');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity(null); $this->Helper->setEntity(null);
$this->Helper->setEntity('ModelThatDoesntExist.field_that_doesnt_exist'); $this->Helper->setEntity('ModelThatDoesntExist.field_that_doesnt_exist');
$expected = array('ModelThatDoesntExist', 'field_that_doesnt_exist'); $expected = array('ModelThatDoesntExist', 'field_that_doesnt_exist');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('HelperTestTag'); $this->Helper->setEntity('HelperTestTag');
$expected = array('HelperTestTag', 'HelperTestTag'); $expected = array('HelperTestTag', 'HelperTestTag');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
} }
/** /**
@ -312,7 +312,7 @@ class HelperTest extends CakeTestCase {
$this->Helper->setEntity('HelperTestPost.1.HelperTestComment.1.title'); $this->Helper->setEntity('HelperTestPost.1.HelperTestComment.1.title');
$expected = array('HelperTestPost', '1', 'HelperTestComment', '1', 'title'); $expected = array('HelperTestPost', '1', 'HelperTestComment', '1', 'title');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->assertEquals('HelperTestComment', $this->Helper->model()); $this->assertEquals('HelperTestComment', $this->Helper->model());
} }
@ -527,15 +527,15 @@ class HelperTest extends CakeTestCase {
$this->Helper->setEntity('HelperTestTag.id'); $this->Helper->setEntity('HelperTestTag.id');
$expected = array('HelperTestTag', 'id'); $expected = array('HelperTestTag', 'id');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('My.id'); $this->Helper->setEntity('My.id');
$expected = array('My', 'id'); $expected = array('My', 'id');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('MyOther.id'); $this->Helper->setEntity('MyOther.id');
$expected = array('MyOther', 'id'); $expected = array('MyOther', 'id');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
} }
/** /**
@ -549,11 +549,11 @@ class HelperTest extends CakeTestCase {
$this->Helper->setEntity('helper_test_post'); $this->Helper->setEntity('helper_test_post');
$expected = array('HelperTestTag', 'helper_test_post'); $expected = array('HelperTestTag', 'helper_test_post');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$this->Helper->setEntity('HelperTestTag'); $this->Helper->setEntity('HelperTestTag');
$expected = array('HelperTestTag', 'HelperTestTag'); $expected = array('HelperTestTag', 'HelperTestTag');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
} }
/** /**
@ -565,12 +565,12 @@ class HelperTest extends CakeTestCase {
public function testFieldSuffixForDate() { public function testFieldSuffixForDate() {
$this->Helper->setEntity('HelperTestPost', true); $this->Helper->setEntity('HelperTestPost', true);
$expected = array('HelperTestPost'); $expected = array('HelperTestPost');
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
foreach (array('year', 'month', 'day', 'hour', 'min', 'meridian') as $d) { foreach (array('year', 'month', 'day', 'hour', 'min', 'meridian') as $d) {
$this->Helper->setEntity('date.' . $d); $this->Helper->setEntity('date.' . $d);
$expected = array('HelperTestPost', 'date', $d); $expected = array('HelperTestPost', 'date', $d);
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
} }
} }
@ -651,7 +651,7 @@ class HelperTest extends CakeTestCase {
$expected = array( $expected = array(
'HelperTestPost', '2', 'HelperTestComment', '1', 'title' 'HelperTestPost', '2', 'HelperTestComment', '1', 'title'
); );
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$entity = 'HelperTestPost.1.HelperTestComment.1.HelperTestTag.1.created'; $entity = 'HelperTestPost.1.HelperTestComment.1.HelperTestTag.1.created';
$this->Helper->setEntity($entity); $this->Helper->setEntity($entity);
@ -659,7 +659,7 @@ class HelperTest extends CakeTestCase {
'HelperTestPost', '1', 'HelperTestComment', '1', 'HelperTestPost', '1', 'HelperTestComment', '1',
'HelperTestTag', '1', 'created' 'HelperTestTag', '1', 'created'
); );
$this->assertEquals($expected, $this->View->entity()); $this->assertEquals($expected, $this->Helper->entity());
$entity = 'HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake'; $entity = 'HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake';
$expected = array( $expected = array(

View file

@ -907,40 +907,6 @@ class ViewTest extends CakeTestCase {
$this->assertEqual($View->viewVars, $expected); $this->assertEqual($View->viewVars, $expected);
} }
/**
* testEntityReference method
*
* @access public
* @return void
*/
public function testEntityReference() {
$View = new TestView($this->PostsController);
$View->model = 'Post';
$View->field = 'title';
$this->assertEqual($View->entity(), array('Post', 'title'));
$View->association = 'Comment';
$View->field = 'user_id';
$this->assertEqual($View->entity(), array('Comment', 'user_id'));
$View->model = 0;
$View->association = null;
$View->field = 'Node';
$View->fieldSuffix = 'title';
$View->entityPath = '0.Node.title';
$expected = array(0, 'Node', 'title');
$this->assertEqual($View->entity(), $expected);
$View->model = 'HelperTestTag';
$View->field = 'HelperTestTag';
$View->modelId = null;
$View->association = null;
$View->fieldSuffix = null;
$View->entityPath = 'HelperTestTag';
$expected = array('HelperTestTag', 'HelperTestTag');
$this->assertEqual($View->entity(), $expected);
}
/** /**
* testBadExt method * testBadExt method
* *

View file

@ -114,6 +114,10 @@ class Helper extends Object {
'year', 'month', 'day', 'hour', 'min', 'second', 'meridian' 'year', 'month', 'day', 'hour', 'min', 'second', 'meridian'
); );
protected $_modelScope;
protected $_association;
protected $_entityPath;
/** /**
* Default Constructor * Default Constructor
* *
@ -391,12 +395,11 @@ class Helper extends Object {
* @return void * @return void
*/ */
public function setEntity($entity, $setScope = false) { public function setEntity($entity, $setScope = false) {
$view = $this->_View;
if ($entity === null) { if ($entity === null) {
$view->modelScope = false; $this->_modelScope = false;
} }
if ($setScope === true) { if ($setScope === true) {
$view->modelScope = $entity; $this->_modelScope = $entity;
} }
$parts = array_values(Set::filter(explode('.', $entity), true)); $parts = array_values(Set::filter(explode('.', $entity), true));
if (empty($parts)) { if (empty($parts)) {
@ -408,13 +411,13 @@ class Helper extends Object {
// Either 'body' or 'date.month' type inputs. // Either 'body' or 'date.month' type inputs.
if ( if (
($count === 1 && ($count === 1 &&
$view->modelScope && $this->_modelScope &&
$setScope == false) || $setScope == false) ||
(in_array($lastPart, $this->_fieldSuffixes) && (in_array($lastPart, $this->_fieldSuffixes) &&
$view->modelScope && $this->_modelScope &&
$parts[0] !== $view->modelScope) $parts[0] !== $this->_modelScope)
) { ) {
$entity = $view->modelScope . '.' . $entity; $entity = $this->_modelScope . '.' . $entity;
} }
// 0.name style inputs. // 0.name style inputs.
@ -423,43 +426,51 @@ class Helper extends Object {
is_numeric($parts[0]) && is_numeric($parts[0]) &&
!is_numeric($parts[1]) !is_numeric($parts[1])
) { ) {
$entity = $view->modelScope . '.' . $entity; $entity = $this->_modelScope . '.' . $entity;
} }
$view->association = null; $this->_association = null;
// check for associated model. // check for associated model.
$reversed = array_reverse($parts); $reversed = array_reverse($parts);
foreach ($reversed as $part) { foreach ($reversed as $part) {
if (preg_match('/^[A-Z]/', $part)) { if (preg_match('/^[A-Z]/', $part)) {
$view->association = $part; $this->_association = $part;
break; break;
} }
} }
// habtm models are special // habtm models are special
if ( if (
isset($this->fieldset[$view->modelScope]['fields'][$parts[0]]['type']) && isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
$this->fieldset[$view->modelScope]['fields'][$parts[0]]['type'] === 'multiple' $this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
) { ) {
$entity = $parts[0] . '.' . $parts[0]; $entity = $parts[0] . '.' . $parts[0];
} }
$view->entityPath = $entity; $this->_entityPath = $entity;
return; return;
} }
/**
* Returns the entity reference of the current context as an array of identity parts
*
* @return array An array containing the identity elements of an entity
*/
public function entity() {
return explode('.', $this->_entityPath);
}
/** /**
* Gets the currently-used model of the rendering context. * Gets the currently-used model of the rendering context.
* *
* @return string * @return string
*/ */
public function model() { public function model() {
$entity = $this->_View->entity(); if ($this->_association) {
if ($this->_View->association) { return $this->_association;
return $this->_View->association;
} }
return $this->_View->modelScope; return $this->_modelScope;
} }
/** /**
@ -468,7 +479,7 @@ class Helper extends Object {
* @return string * @return string
*/ */
public function field() { public function field() {
$entity = $this->_View->entity(); $entity = $this->entity();
$count = count($entity); $count = count($entity);
$last = $entity[$count - 1]; $last = $entity[$count - 1];
if (in_array($last, $this->_fieldSuffixes)) { if (in_array($last, $this->_fieldSuffixes)) {
@ -488,7 +499,7 @@ class Helper extends Object {
*/ */
public function tagIsInvalid($model = null, $field = null, $modelID = null) { public function tagIsInvalid($model = null, $field = null, $modelID = null) {
$errors = $this->validationErrors; $errors = $this->validationErrors;
$entity = $this->_View->entity(); $entity = $this->entity();
if (!empty($entity)) { if (!empty($entity)) {
return Set::extract($errors, join('.', $entity)); return Set::extract($errors, join('.', $entity));
} }
@ -514,7 +525,7 @@ class Helper extends Object {
return $this->domId(); return $this->domId();
} }
$entity = $this->_View->entity(); $entity = $this->entity();
$model = array_shift($entity); $model = array_shift($entity);
$dom = $model . join('', array_map(array('Inflector', 'camelize'), $entity)); $dom = $model . join('', array_map(array('Inflector', 'camelize'), $entity));
@ -560,7 +571,7 @@ class Helper extends Object {
$name = $field; $name = $field;
break; break;
default: default:
$name = 'data[' . implode('][', $this->_View->entity()) . ']'; $name = 'data[' . implode('][', $this->entity()) . ']';
break; break;
} }
@ -602,7 +613,7 @@ class Helper extends Object {
$result = null; $result = null;
$data = $this->request->data; $data = $this->request->data;
$entity = $this->_View->entity(); $entity = $this->entity();
if (!empty($data) && !empty($entity)) { if (!empty($data) && !empty($entity)) {
$result = Set::extract($data, implode('.', $entity)); $result = Set::extract($data, implode('.', $entity));
} }

View file

@ -483,7 +483,7 @@ class FormHelper extends AppHelper {
*/ */
protected function __secure($lock, $field = null, $value = null) { protected function __secure($lock, $field = null, $value = null) {
if (!$field) { if (!$field) {
$field = $this->_View->entity(); $field = $this->entity();
} elseif (is_string($field)) { } elseif (is_string($field)) {
$field = Set::filter(explode('.', $field), true); $field = Set::filter(explode('.', $field), true);
} }
@ -627,7 +627,7 @@ class FormHelper extends AppHelper {
*/ */
public function label($fieldName = null, $text = null, $options = array()) { public function label($fieldName = null, $text = null, $options = array()) {
if (empty($fieldName)) { if (empty($fieldName)) {
$fieldName = implode('.', $this->_View->entity()); $fieldName = implode('.', $this->entity());
} }
if ($text === null) { if ($text === null) {
@ -1321,7 +1321,7 @@ class FormHelper extends AppHelper {
$options['secure'] = self::SECURE_SKIP; $options['secure'] = self::SECURE_SKIP;
$options = $this->_initInputField($fieldName, $options); $options = $this->_initInputField($fieldName, $options);
$field = $this->_View->entity(); $field = $this->entity();
foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) { foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
$this->__secure($secure, array_merge($field, array($suffix))); $this->__secure($secure, array_merge($field, array($suffix)));
@ -2122,7 +2122,7 @@ class FormHelper extends AppHelper {
return $options; return $options;
} }
$entity = $this->_View->entity(); $entity = $this->entity();
$model = $this->model(); $model = $this->model();
$name = $model === $entity[0] && isset($entity[1]) ? $entity[1] : $entity[0]; $name = $model === $entity[0] && isset($entity[1]) ? $entity[1] : $entity[0];
$last = $entity[count($entity) - 1]; $last = $entity[count($entity) - 1];

View file

@ -160,48 +160,6 @@ class View extends Object {
*/ */
public $hasRendered = false; public $hasRendered = false;
/**
* True if in scope of model-specific region
*
* @var boolean
*/
public $modelScope = false;
/**
* Name of current model this view context is attached to
*
* @var string
*/
public $model = null;
/**
* Name of association model this view context is attached to
*
* @var string
*/
public $association = null;
/**
* Name of current model field this view context is attached to
*
* @var string
*/
public $field = null;
/**
* Suffix of current field this view context is attached to
*
* @var string
*/
public $fieldSuffix = null;
/**
* The current model ID this view context is attached to
*
* @var mixed
*/
public $modelId = null;
/** /**
* List of generated DOM UUIDs * List of generated DOM UUIDs
* *
@ -545,15 +503,6 @@ class View extends Object {
return $hash; return $hash;
} }
/**
* Returns the entity reference of the current context as an array of identity parts
*
* @return array An array containing the identity elements of an entity
*/
public function entity() {
return explode('.', $this->entityPath);
}
/** /**
* Allows a template or element to set a variable that will be available in * Allows a template or element to set a variable that will be available in
* a layout or other element. Analagous to Controller::set(). * a layout or other element. Analagous to Controller::set().