Skip to content

Commit

Permalink
Use default flash element.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jul 13, 2014
1 parent 1ff7bf5 commit c50e730
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 25 deletions.
16 changes: 7 additions & 9 deletions src/Controller/Component/FlashComponent.php
Expand Up @@ -40,7 +40,7 @@ class FlashComponent extends Component {
*/
protected $_defaultConfig = [
'key' => 'flash',
'element' => null,
'element' => 'default',
'params' => []
];

Expand All @@ -63,7 +63,7 @@ public function __construct(ComponentRegistry $collection, array $config = []) {
* ### Options:
*
* - `key` The key to set under the session's Flash key
* - `element` The element used to render the flash message
* - `element` The element used to render the flash message. Default to 'default'.
* - `params` An array of variables to make available when using an element
*
* @param string|\Exception $message Message to be flashed. If an instance
Expand All @@ -80,14 +80,12 @@ public function set($message, array $options = []) {
$message = $message->getMessage();
}

if ($opts['element'] !== null) {
list($plugin, $element) = pluginSplit($opts['element']);
list($plugin, $element) = pluginSplit($opts['element']);

if ($plugin) {
$opts['element'] = $plugin . '.Flash/' . $element;
} else {
$opts['element'] = 'Flash/' . $element;
}
if ($plugin) {
$opts['element'] = $plugin . '.Flash/' . $element;
} else {
$opts['element'] = 'Flash/' . $element;
}

$this->_session->write('Flash.' . $opts['key'], [
Expand Down
12 changes: 3 additions & 9 deletions src/View/Helper/FlashHelper.php
Expand Up @@ -62,18 +62,12 @@ class FlashHelper extends Helper {
* @return string
*/
public function render($key = 'flash', array $options = []) {
$flash = $this->request->session()->read("Flash.$key");
$this->request->session()->delete("Flash.$key");

if (!$flash) {
if (!$this->request->session()->check("Flash.$key")) {
return '';
}

$flash = $options + $flash;

if ($flash['element'] === null) {
return $flash['message'];
}
$flash = $options + $this->request->session()->read("Flash.$key");
$this->request->session()->delete("Flash.$key");

return $this->_View->element($flash['element'], $flash);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Controller/Component/FlashComponentTest.php
Expand Up @@ -65,7 +65,7 @@ public function testSet() {
$expected = [
'message' => 'This is a test message',
'key' => 'flash',
'element' => null,
'element' => 'Flash/default',
'params' => []
];
$result = $this->Session->read('Flash.flash');
Expand Down Expand Up @@ -95,7 +95,7 @@ public function testSet() {
$expected = [
'message' => 'This is a test message',
'key' => 'foobar',
'element' => null,
'element' => 'Flash/default',
'params' => []
];
$result = $this->Session->read('Flash.foobar');
Expand All @@ -115,7 +115,7 @@ public function testSetWithException() {
$expected = [
'message' => 'This is a test message',
'key' => 'flash',
'element' => null,
'element' => 'Flash/default',
'params' => ['code' => 404]
];
$result = $this->Session->read('Flash.flash');
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/View/Helper/FlashHelperTest.php
Expand Up @@ -46,7 +46,7 @@ public function setUp() {
'flash' => array(
'key' => 'flash',
'message' => 'This is a calling',
'element' => null,
'element' => 'Flash/default',
'params' => array()
),
'notification' => array(
Expand Down Expand Up @@ -75,7 +75,7 @@ public function setUp() {
*/
public function tearDown() {
parent::tearDown();
unset($this->View, $this->Session);
unset($this->View, $this->Flash);
}

/**
Expand All @@ -85,8 +85,8 @@ public function tearDown() {
*/
public function testFlash() {
$result = $this->Flash->render();
$expected = 'This is a calling';
$this->assertEquals($expected, $result);
$expected = '<div class="message">This is a calling</div>';
$this->assertContains($expected, $result);

$expected = '<div id="classy-message">Recorded</div>';
$result = $this->Flash->render('classy');
Expand Down
1 change: 1 addition & 0 deletions tests/test_app/TestApp/Template/Element/Flash/default.ctp
@@ -0,0 +1 @@
<div class="message"><?= $message; ?></div>

0 comments on commit c50e730

Please sign in to comment.