Skip to content

Commit

Permalink
Request#flash() without args now returns all flashes
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 17, 2010
1 parent f2637c6 commit 50e0593
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/express/plugins/flash.js
Expand Up @@ -18,6 +18,9 @@ exports.Flash = Plugin.extend({
* it will persist in the session until outputted.
* The _val_ pushed is returned.
*
* When no arguments are given, all flashes are
* returned keyed by their type.
*
* Example:
*
* this.flash('info', 'email sent')
Expand All @@ -30,12 +33,17 @@ exports.Flash = Plugin.extend({
*
* @param {string} key
* @param {string} val
* @return {string}
* @return {mixed}
* @api public
*/

flash: function(key, val) {
if (!this.session.flash) this.session.flash = {}
if (!key) {
var flashes = this.session.flash
this.session.flash = {}
return flashes
}
if (!(key in this.session.flash)) this.session.flash[key] = []
if (val)
return this.session.flash[key].push(val), val
Expand Down
29 changes: 29 additions & 0 deletions spec/spec.plugins.flash.js
Expand Up @@ -41,6 +41,35 @@ describe 'Express'
get('/', headers).body.should.eql 'nope!'
end
end

describe 'given no arguments'
it 'should provide access to all types'
var flash, headers = { headers: { cookie: 'sid=123' }}
post('/', function(){ return this.flash('info', 'email sent') })
post('/error', function(){ return this.flash('error', 'email failed to send') })
get('/', function(){ return flash = this.flash(), '' })
post('/', headers)
post('/', headers)
post('/error', headers)
get('/')
flash.should.eql { info: ['email sent', 'email sent'], error: ['email failed to send'] }
end

it 'should persist only until flushed'
var flash, headers = { headers: { cookie: 'sid=123' }}
post('/', function(){ return this.flash('info', 'email sent') })
post('/error', function(){ return this.flash('error', 'email failed to send') })
get('/', function(){ return flash = this.flash(), '' })
post('/', headers)
post('/', headers)
post('/error', headers)
get('/', headers)
flash.should.eql { info: ['email sent', 'email sent'], error: ['email failed to send'] }
get('/', headers)
flash.should.eql {}
end
end

end
end
end
Expand Down

0 comments on commit 50e0593

Please sign in to comment.