Installation as gem
gem 'cacheable_flash' # added to your Gemfile $ bundle install
Without asset pipeline, or pre-Rails 3.1:
First copy the JS assets into your app:
rails generate cacheable_flash:install
So if you have config.assets.enabled = false in application.rb then in your layout:
Otherwise, in your layout, just source them like normal:
With asset pipeline (requires Rails 3.1)
//= require flash //= require jquery.cookie
Please help document!
Usage as an around filter
Note that the cookie holding the flash messages is removed as the page is displayed, so a refresh will clear the flash message (just as happens normally).
class MyController < ActionController::Base include # ... end
Usage as a Rack middleware (requires Rails 3)
To use as a Rack Middleware, swap the Rails flash middleware with the Cacheable flash middleware. Use this method if you set flash messages inside a rescue_from block:
rescue_from CanCan::AccessDenied do |exception| redirect_to root_url, :alert => exception. end
In your application.rb:
# Swap the ActionDispatch::Flash middleware with the CacheableFlash one config.middleware.swap ActionDispatch::Flash, ::
Example Template Markup
You can test your flash cookies by making assertions on the json of the “flash” cookie. Cacheable Flash provides test helpers which includes the flash_cookie method.
require "cacheable_flash/test_helpers" class TestController < ActionController::Base def index flash["notice"] = "In index" end end class ControllerTest < Test::Unit::TestCase include :: def setup @controller = TestController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end def test_cacheable_flash_action get :index assert_equal "In index", ["notice"] end end
require "cacheable_flash/test_helpers" class TestController < ActionController::Base def index flash["notice"] = "In index" end end describe TestController, "#index" do include :: it "writes to the flash cookie" do get :index ["notice"].should == "In index" end end
Contributing to cacheable-flash
Create your feature branch (`git checkout -b my-new-feature`)
Commit your changes (`git commit -am 'Added some feature'`)
Push to the branch (`git push origin my-new-feature`)
Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
Create new Pull Request
This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.
As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.
spec.add_dependency 'cacheable_flash', '~> 4.0'
Licensed under the MIT License. See LICENSE for further details.