Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Created March 20, 2012 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiehodge/2136710 to your computer and use it in GitHub Desktop.
Save jamiehodge/2136710 to your computer and use it in GitHub Desktop.
Sinatra flash
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
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