Code Monkey home page Code Monkey logo

rack-flash's Introduction

rack-flash4 - don't symbolize keys

Original rack-flash as well as rack-flash3 forcibly changes the key names to symbols. Unfortunately this does not work if the session storage is backed by something that does not support symbols (i.e. Redis). rack-flash4 addresses this problem by converting the keys to strings, so you can use things like flash['error'] and it will behave as expected.

Patch the Call to rack_builder

Treeder forked the original Rack::Flash and patched its issues, see https://github.com/treeder/rack-flash

However, when not using Sinatra, the code still won't work. This patches that in the Patch_rack_builder_call branch.

View the RDoc. Below is the original README.md

Rack Flash

flash[:notice] = "You can stop rolling your own now."

Simple flash hash implementation for Rack apps.

View the RDoc.

Usage

Here's how to use it.

Install / add to Gemfile

gem 'rack-flash3'

Vanilla Rack apps

You can access flash entries via env['x-rack.flash']. You can treat it either like a regular flash hash:

env['x-rack.flash'][:notice] = 'You have logged out.'

Or you can pass the :accessorize option to declare your flash types. Each of these will have accessors defined on the flash object:

use Rack::Flash, :accessorize => [:notice, :error]

# Set a flash entry
env['x-rack.flash'].notice = 'You have logged out.'

# Get a flash entry
env['x-rack.flash'].notice # => 'You have logged out.'

# Set a a flash entry for only the current request
env['x-rack.flash'].notice! 'You have logged out.'

Sample rack app:

get = proc { |env|
  [200, {},
    env['x-rack.flash'].notice || 'No flash set. Try going to /set'
  ]
}

set = proc { |env|
  env['x-rack.flash'].notice = 'Hey, the flash was set!'
  [302, {'Location' => '/'},
    'You are being redirected.'
  ]
}

builder = Rack::Builder.new do
  use Rack::Session::Cookie
  use Rack::Flash, :accessorize => true

  map('/set') { run set }
  map('/')    { run get }
end

Rack::Handler::Mongrel.run builder, :Port => 9292

Sinatra

If you're using Sinatra, you can use the flash hash just like in Rails:

require 'sinatra/base'
require 'rack-flash'

class MyApp < Sinatra::Base
  enable :sessions
  use Rack::Flash

  post '/set-flash' do
    # Set a flash entry
    flash[:notice] = "Thanks for signing up!"
    
    # Get a flash entry
    flash[:notice] # => "Thanks for signing up!"
    
    # Set a flash entry for only the current request
    flash.now[:notice] = "Thanks for signing up!"
  end
end

If you've got any ideas on how to simplify access to the flash hash for vanilla Rack apps, let me know. It still feels a bit off to me.

Sweeping stale entries

By default Rack::Flash has slightly different behavior than Rails in that it doesn't delete entries until they are used. If you want entries to be cleared even if they are not ever accessed, you can use the :sweep option:

use Rack::Flash, :sweep => true

This will sweep stale flash entries, whether or not you actually use them.

rack-flash's People

Contributors

nakajima avatar treeder avatar skrobul avatar knzconnor avatar jeremyevans avatar akahn avatar gbognar avatar neaf avatar

Stargazers

Julien Desrosiers avatar Aldis Berjoza avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.