Skip to content

Commit

Permalink
Moving entity() and its related attributes to Helper
Browse files Browse the repository at this point in the history
Removing entity() and its attributes from View.
Having that information on View allowed for unwanted side effects,
and seemed like a break of encapsulation.
  • Loading branch information
markstory committed Jun 26, 2011
1 parent e4d7010 commit 8925832
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 137 deletions.
10 changes: 5 additions & 5 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -775,23 +775,23 @@ public function testValidateHashNoModel() {
*/
public function testDuplicateFieldNameResolution() {
$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');
$this->assertEqual($this->View->entity(), array('ValidateItem', 'name'));
$this->assertEqual($this->Form->entity(), array('ValidateItem', '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('/type="text"/', $result);

$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('/<textarea/', $result);

$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('/type="text"/', $result);
}
Expand Down
42 changes: 21 additions & 21 deletions lib/Cake/Test/Case/View/HelperTest.php
Expand Up @@ -252,7 +252,7 @@ public static function entityProvider() {
*/
public function testSetEntity($entity, $expected) {
$this->Helper->setEntity($entity);
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());
}

/**
Expand All @@ -262,44 +262,44 @@ public function testSetEntity($entity, $expected) {
*/
public function testSetEntityScoped() {
$this->Helper->setEntity('HelperTestPost', true);
$this->assertEquals(array('HelperTestPost'), $this->View->entity());
$this->assertEquals(array('HelperTestPost'), $this->Helper->entity());

$this->Helper->setEntity('id');
$expected = array('HelperTestPost', 'id');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());

$this->Helper->setEntity('HelperTestComment.body');
$expected = array('HelperTestComment', 'body');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());

$this->Helper->setEntity('body');
$expected = array('HelperTestPost', 'body');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());

$this->Helper->setEntity('2.body');
$expected = array('HelperTestPost', '2', 'body');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());

$this->Helper->setEntity('Something.else');
$expected = array('Something', 'else');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());

$this->Helper->setEntity('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');
$expected = array('HelperTestComment', 'id', 'time');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());

$this->Helper->setEntity(null);
$this->Helper->setEntity('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');
$expected = array('HelperTestTag', 'HelperTestTag');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());
}

/**
Expand All @@ -312,7 +312,7 @@ public function testSetEntityAssociated() {

$this->Helper->setEntity('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());
}
Expand Down Expand Up @@ -527,15 +527,15 @@ public function testFieldsWithSameName() {

$this->Helper->setEntity('HelperTestTag.id');
$expected = array('HelperTestTag', 'id');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());

$this->Helper->setEntity('My.id');
$expected = array('My', 'id');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());

$this->Helper->setEntity('MyOther.id');
$expected = array('MyOther', 'id');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());
}

/**
Expand All @@ -549,11 +549,11 @@ public function testFieldSameAsModel() {

$this->Helper->setEntity('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');
$expected = array('HelperTestTag', 'HelperTestTag');
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());
}

/**
Expand All @@ -565,12 +565,12 @@ public function testFieldSameAsModel() {
public function testFieldSuffixForDate() {
$this->Helper->setEntity('HelperTestPost', true);
$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) {
$this->Helper->setEntity('date.' . $d);
$expected = array('HelperTestPost', 'date', $d);
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());
}
}

Expand Down Expand Up @@ -651,15 +651,15 @@ public function testMultiDimensionalField() {
$expected = array(
'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';
$this->Helper->setEntity($entity);
$expected = array(
'HelperTestPost', '1', 'HelperTestComment', '1',
'HelperTestTag', '1', 'created'
);
$this->assertEquals($expected, $this->View->entity());
$this->assertEquals($expected, $this->Helper->entity());

$entity = 'HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake';
$expected = array(
Expand Down
34 changes: 0 additions & 34 deletions lib/Cake/Test/Case/View/ViewTest.php
Expand Up @@ -907,40 +907,6 @@ public function testSet() {
$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
*
Expand Down
55 changes: 33 additions & 22 deletions lib/Cake/View/Helper.php
Expand Up @@ -114,6 +114,10 @@ class Helper extends Object {
'year', 'month', 'day', 'hour', 'min', 'second', 'meridian'
);

protected $_modelScope;
protected $_association;
protected $_entityPath;

/**
* Default Constructor
*
Expand Down Expand Up @@ -391,12 +395,11 @@ protected function _formatAttribute($key, $value, $escape = true) {
* @return void
*/
public function setEntity($entity, $setScope = false) {
$view = $this->_View;
if ($entity === null) {
$view->modelScope = false;
$this->_modelScope = false;
}
if ($setScope === true) {
$view->modelScope = $entity;
$this->_modelScope = $entity;
}
$parts = array_values(Set::filter(explode('.', $entity), true));
if (empty($parts)) {
Expand All @@ -408,13 +411,13 @@ public function setEntity($entity, $setScope = false) {
// Either 'body' or 'date.month' type inputs.
if (
($count === 1 &&
$view->modelScope &&
$this->_modelScope &&
$setScope == false) ||
(in_array($lastPart, $this->_fieldSuffixes) &&
$view->modelScope &&
$parts[0] !== $view->modelScope)
$this->_modelScope &&
$parts[0] !== $this->_modelScope)
) {
$entity = $view->modelScope . '.' . $entity;
$entity = $this->_modelScope . '.' . $entity;
}

// 0.name style inputs.
Expand All @@ -423,43 +426,51 @@ public function setEntity($entity, $setScope = false) {
is_numeric($parts[0]) &&
!is_numeric($parts[1])
) {
$entity = $view->modelScope . '.' . $entity;
$entity = $this->_modelScope . '.' . $entity;
}

$view->association = null;
$this->_association = null;

// check for associated model.
$reversed = array_reverse($parts);
foreach ($reversed as $part) {
if (preg_match('/^[A-Z]/', $part)) {
$view->association = $part;
$this->_association = $part;
break;
}
}

// habtm models are special
if (
isset($this->fieldset[$view->modelScope]['fields'][$parts[0]]['type']) &&
$this->fieldset[$view->modelScope]['fields'][$parts[0]]['type'] === 'multiple'
isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
$this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
) {
$entity = $parts[0] . '.' . $parts[0];
}

$view->entityPath = $entity;
$this->_entityPath = $entity;
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.
*
* @return string
*/
public function model() {
$entity = $this->_View->entity();
if ($this->_View->association) {
return $this->_View->association;
if ($this->_association) {
return $this->_association;
}
return $this->_View->modelScope;
return $this->_modelScope;
}

/**
Expand All @@ -468,7 +479,7 @@ public function model() {
* @return string
*/
public function field() {
$entity = $this->_View->entity();
$entity = $this->entity();
$count = count($entity);
$last = $entity[$count - 1];
if (in_array($last, $this->_fieldSuffixes)) {
Expand All @@ -488,7 +499,7 @@ public function field() {
*/
public function tagIsInvalid($model = null, $field = null, $modelID = null) {
$errors = $this->validationErrors;
$entity = $this->_View->entity();
$entity = $this->entity();
if (!empty($entity)) {
return Set::extract($errors, join('.', $entity));
}
Expand All @@ -514,7 +525,7 @@ public function domId($options = null, $id = 'id') {
return $this->domId();
}

$entity = $this->_View->entity();
$entity = $this->entity();
$model = array_shift($entity);
$dom = $model . join('', array_map(array('Inflector', 'camelize'), $entity));

Expand Down Expand Up @@ -560,7 +571,7 @@ protected function _name($options = array(), $field = null, $key = 'name') {
$name = $field;
break;
default:
$name = 'data[' . implode('][', $this->_View->entity()) . ']';
$name = 'data[' . implode('][', $this->entity()) . ']';
break;
}

Expand Down Expand Up @@ -602,7 +613,7 @@ public function value($options = array(), $field = null, $key = 'value') {
$result = null;
$data = $this->request->data;

$entity = $this->_View->entity();
$entity = $this->entity();
if (!empty($data) && !empty($entity)) {
$result = Set::extract($data, implode('.', $entity));
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -483,7 +483,7 @@ public function unlockField($name = null) {
*/
protected function __secure($lock, $field = null, $value = null) {
if (!$field) {
$field = $this->_View->entity();
$field = $this->entity();
} elseif (is_string($field)) {
$field = Set::filter(explode('.', $field), true);
}
Expand Down Expand Up @@ -627,7 +627,7 @@ public function error($field, $text = null, $options = array()) {
*/
public function label($fieldName = null, $text = null, $options = array()) {
if (empty($fieldName)) {
$fieldName = implode('.', $this->_View->entity());
$fieldName = implode('.', $this->entity());
}

if ($text === null) {
Expand Down Expand Up @@ -1321,7 +1321,7 @@ public function file($fieldName, $options = array()) {
$options['secure'] = self::SECURE_SKIP;

$options = $this->_initInputField($fieldName, $options);
$field = $this->_View->entity();
$field = $this->entity();

foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
$this->__secure($secure, array_merge($field, array($suffix)));
Expand Down Expand Up @@ -2122,7 +2122,7 @@ protected function _name($options = array(), $field = null, $key = 'name') {
return $options;
}

$entity = $this->_View->entity();
$entity = $this->entity();
$model = $this->model();
$name = $model === $entity[0] && isset($entity[1]) ? $entity[1] : $entity[0];
$last = $entity[count($entity) - 1];
Expand Down

0 comments on commit 8925832

Please sign in to comment.