Created
March 20, 2012 15:08
-
-
Save jamiehodge/2136710 to your computer and use it in GitHub Desktop.
Sinatra flash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Sinatra | |
module Flash | |
module InstanceMethods | |
def flash | |
@flash ||= {} | |
end | |
def flash_now | |
@flash_now ||= {} | |
end | |
def redirect(uri, *args) | |
session[:flash] = flash | |
super(uri, *args) | |
end | |
end | |
def self.registered(app) | |
app.before { flash.merge!(session.delete('flash') || {}) } | |
app.helpers InstanceMethods | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'spec_helper' | |
describe Sinatra::Flash do | |
def app | |
Sinatra.new do | |
enable :sessions | |
register Sinatra::Flash | |
get '/' do | |
flash.inspect | |
end | |
get '/flash' do | |
flash[:notice] = 'foo' | |
redirect to '/' | |
end | |
get '/flash_now' do | |
flash_now[:notice] = 'foo' | |
flash_now.inspect | |
end | |
get '/flash_now_redirect' do | |
flash_now[:notice] = 'foo' | |
redirect to '/' | |
end | |
end | |
end | |
describe 'flash' do | |
it 'must be empty by default' do | |
get '/' | |
last_response.body.must_equal '{}' | |
end | |
it 'must persist across requests' do | |
get '/flash' | |
follow_redirect! | |
last_response.body.must_equal "{:notice=>\"foo\"}" | |
end | |
end | |
describe 'flash_now' do | |
it 'must set and return a hash' do | |
get '/flash_now' | |
last_response.body.must_equal "{:notice=>\"foo\"}" | |
end | |
it 'must not persist across requests' do | |
get '/flash_now_redirect' | |
follow_redirect! | |
last_response.body.must_equal '{}' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment