Skip to content

Commit

Permalink
Look for elements under Flash folder, add plugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
bcrowe committed Jun 14, 2014
1 parent 89a7211 commit 9bfc86d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
19 changes: 15 additions & 4 deletions src/Controller/Component/FlashComponent.php
Expand Up @@ -78,6 +78,16 @@ public function set($message, array $options = []) {
$message = $message->getMessage();
}

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

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

$this->_session->write('Flash.' . $opts['key'], [
'message' => $message,
'key' => $opts['key'],
Expand All @@ -90,15 +100,16 @@ public function set($message, array $options = []) {
* Magic method for verbose flash methods based on element names.
*
* For example: $this->Flash->success('My message') would use the
* flash_success.ctp element for rendering the flash message.
* success.ctp element under `Template/Element/Flash` for rendering the
* flash message.
*
* @param string $name Element name to use, omitting the "flash_" prefix.
* @param array $args Parameters to pass when calling `FlashComponent::set`.
* @param string $name Element name to use.
* @param array $args Parameters to pass when calling `FlashComponent::set()`.
* @return void
* @throws \Cake\Error\InternalErrorException If missing the flash message.
*/
public function __call($name, $args) {
$options = ['element' => 'flash_' . Inflector::underscore($name)];
$options = ['element' => Inflector::underscore($name)];

if (count($args) < 1) {
throw new InternalErrorException('Flash message missing.');
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Controller/Component/FlashComponentTest.php
Expand Up @@ -76,7 +76,7 @@ public function testSet() {
$expected = [
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'test',
'element' => 'Flash/test',
'params' => ['foo' => 'bar']
];
$result = $this->Session->read('Flash.flash');
Expand All @@ -86,7 +86,7 @@ public function testSet() {
$expected = [
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'MyPlugin.alert',
'element' => 'MyPlugin.Flash/alert',
'params' => []
];
$result = $this->Session->read('Flash.flash');
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/View/Helper/FlashHelperTest.php
Expand Up @@ -146,8 +146,8 @@ public function testFlashElementInAttrs() {
public function testFlashWithPluginElement() {
Plugin::load('TestPlugin');

$result = $this->Flash->render('flash', array('element' => 'TestPlugin.plugin_element'));
$expected = 'this is the plugin element using params[plugin]';
$result = $this->Flash->render('flash', array('element' => 'TestPlugin.Flash/plugin_element'));
$expected = 'this is the plugin element';
$this->assertEquals($expected, $result);
}
}

0 comments on commit 9bfc86d

Please sign in to comment.