Adding the ability to pass additional information into the rendering of flash messages. Allows you to keep keep code DRY by moving repeated settings into the view.

Added tests.
Fixes 
This commit is contained in:
mark_story 2011-02-21 14:07:33 -05:00
parent e1b3703c20
commit c64dd9d352
2 changed files with 71 additions and 12 deletions
cake
libs/view/helpers
tests/cases/libs/view/helpers

View file

@ -32,7 +32,7 @@ class SessionHelper extends AppHelper {
/**
* Used to read a session values set in a controller for a key or return values for all keys.
*
* In your view: `$session->read('Controller.sessKey');`
* In your view: `$this->Session->read('Controller.sessKey');`
* Calling the method without a param will return all session vars
*
* @param string $name the name of the session key you want to read
@ -46,7 +46,7 @@ class SessionHelper extends AppHelper {
/**
* Used to check is a session key has been set
*
* In your view: `$session->check('Controller.sessKey');`
* In your view: `$this->Session->check('Controller.sessKey');`
*
* @param string $name
* @return boolean
@ -59,7 +59,7 @@ class SessionHelper extends AppHelper {
/**
* Returns last error encountered in a session
*
* In your view: `$session->error();`
* In your view: `$this->Session->error();`
*
* @return string last error
* @link http://book.cakephp.org/view/1466/Methods
@ -71,32 +71,64 @@ class SessionHelper extends AppHelper {
/**
* Used to render the message set in Controller::Session::setFlash()
*
* In your view: $session->flash('somekey');
* In your view: $this->Session->flash('somekey');
* Will default to flash if no param is passed
*
* You can pass additional information into the flash message generation. This allows you
* to consolidate all the parameters for a given type of flash message into the view.
*
* {{{
* echo $this->Session->flash('flash', array('params' => array('class' => 'new-flash')));
* }}}
*
* The above would generate a flash message with a custom class name. Using $attrs['params'] you
* can pass additional data into the element rendering that will be made available as local variables
* when the element is rendered:
*
* {{{
* echo $this->Session->flash('flash', array('params' => array('name' => $user['User']['name'])));
* }}}
*
* This would pass the current user's name into the flash message, so you could create peronsonalized
* messages without the controller needing access to that data.
*
* Lastly you can choose the element that is rendered when creating the flash message. Using
* custom elements allows you to fully customize how flash messages are generated.
*
* {{{
* echo $this->Session->flash('flash', array('element' => 'my_custom_element'));
* }}}
*
* @param string $key The [Message.]key you are rendering in the view.
* @return boolean|string Will return the value if $key is set, or false if not set.
* @return array $attrs Additional attributes to use for the creation of this flash message.
* Supports the 'params', and 'element' keys that are used in the helper.
* @access public
* @link http://book.cakephp.org/view/1466/Methods
* @link http://book.cakephp.org/view/1467/flash
*/
public function flash($key = 'flash') {
public function flash($key = 'flash', $attrs = array()) {
$out = false;
if (CakeSession::check('Message.' . $key)) {
$flash = CakeSession::read('Message.' . $key);
$message = $flash['message'];
unset($flash['message']);
if (!empty($attrs)) {
$flash = array_merge($flash, $attrs);
}
if ($flash['element'] == 'default') {
$class = 'message';
if (!empty($flash['params']['class'])) {
$class = $flash['params']['class'];
}
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $flash['message'] . '</div>';
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
} elseif ($flash['element'] == '' || $flash['element'] == null) {
$out = $flash['message'];
$out = $message;
} else {
$tmpVars = $flash['params'];
$tmpVars['message'] = $flash['message'];
$tmpVars['message'] = $message;
$out = $this->_View->element($flash['element'], $tmpVars);
}
CakeSession::delete('Message.' . $key);

View file

@ -119,19 +119,19 @@ class SessionHelperTest extends CakeTestCase {
* @return void
*/
function testFlash() {
$result = $this->Session->flash('flash', true);
$result = $this->Session->flash('flash');
$expected = '<div id="flashMessage" class="message">This is a calling</div>';
$this->assertEqual($result, $expected);
$this->assertFalse($this->Session->check('Message.flash'));
$expected = '<div id="classyMessage" class="positive">Recorded</div>';
$result = $this->Session->flash('classy', true);
$result = $this->Session->flash('classy');
$this->assertEqual($result, $expected);
App::build(array(
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
));
$result = $this->Session->flash('notification', true);
$result = $this->Session->flash('notification');
$result = str_replace("\r\n", "\n", $result);
$expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a test of the emergency broadcasting system</p>\n</div>";
$this->assertEqual($result, $expected);
@ -143,4 +143,31 @@ class SessionHelperTest extends CakeTestCase {
$this->assertFalse($this->Session->check('Message.bare'));
}
/**
* test flash() with the attributes.
*
* @return void
*/
function testFlashAttributes() {
$result = $this->Session->flash('flash', array('params' => array('class' => 'test-message')));
$expected = '<div id="flashMessage" class="test-message">This is a calling</div>';
$this->assertEqual($result, $expected);
$this->assertFalse($this->Session->check('Message.flash'));
}
/**
* test setting the element from the attrs.
*
* @return void
*/
function testFlashElementInAttrs() {
App::build(array(
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
));
$result = $this->Session->flash('flash', array(
'element' => 'session_helper',
'params' => array('title' => 'Notice!', 'name' => 'Alert!')
));
$expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a calling</p>\n</div>";
}
}