├── .gitignore ├── Gemfile ├── Rakefile ├── test ├── sinatra_app.rb └── sinatra_redirect_with_flash_test.rb ├── lib └── sinatra │ └── redirect_with_flash.rb ├── sinatra-redirect-with-flash.gemspec ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | pkg/* 3 | Gemfile.lock 4 | *.gem 5 | .bundle 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gem 'rake', :group => :test 4 | 5 | gemspec 6 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake/testtask' 2 | require 'bundler' 3 | Bundler::GemHelper.install_tasks 4 | 5 | task :default => :test 6 | 7 | Rake::TestTask.new do |t| 8 | t.libs << "test" 9 | t.test_files = FileList['test/*_test.rb'] 10 | t.verbose = true 11 | end 12 | -------------------------------------------------------------------------------- /test/sinatra_app.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'sinatra' 3 | require 'sinatra/flash' 4 | require './lib/sinatra/redirect_with_flash' 5 | 6 | enable :sessions 7 | 8 | get '/' do 9 | redirect '/fff', :flash => {:info => 'sample info'} 10 | end 11 | 12 | Sinatra::RedirectWithFlash::COMMON_FLASH_NAMES.each do |k| 13 | get "/#{k.to_s}" do 14 | redirect '/fff', k => "sample #{k.to_s}" 15 | end 16 | end 17 | 18 | get '/notice-with-code' do 19 | redirect '/fff', 301, :notice => '301 and notice' 20 | end 21 | 22 | get '/old-school-redirect' do 23 | redirect '/aaa' 24 | end 25 | 26 | get '/old-school-redirect-with-code' do 27 | redirect '/aaa', 301 28 | end 29 | -------------------------------------------------------------------------------- /lib/sinatra/redirect_with_flash.rb: -------------------------------------------------------------------------------- 1 | require 'sinatra/base' 2 | 3 | module Sinatra 4 | module RedirectWithFlash 5 | COMMON_FLASH_NAMES = [:notice, :alert, :warning, :error, :info, :success] 6 | 7 | def redirect(uri, *args) 8 | flash_opts = args.last 9 | 10 | if flash_opts && flash_opts.is_a?(Hash) 11 | COMMON_FLASH_NAMES.each do |name| 12 | if val = flash_opts.delete(name) 13 | flash[name] = val 14 | end 15 | end 16 | 17 | if other_flashes = flash_opts.delete(:flash) 18 | other_flashes.each {|k, v| flash[k] = v} 19 | end 20 | end 21 | 22 | super(uri, *args) 23 | end 24 | 25 | end 26 | 27 | helpers RedirectWithFlash 28 | end 29 | -------------------------------------------------------------------------------- /sinatra-redirect-with-flash.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | Gem::Specification.new do |s| 4 | s.name = %q{sinatra-redirect-with-flash} 5 | s.version = "0.2.1" 6 | 7 | s.authors = ["Vasily Polovnyov"] 8 | s.license = 'MIT' 9 | s.summary = s.description = %q{redirect with flash helper for Sinatra} 10 | s.email = %q{vasily@polovnyov.ru} 11 | s.homepage = %q{http://github.com/vast/sinatra-redirect-with-flash} 12 | 13 | s.require_paths = ["lib"] 14 | 15 | s.extra_rdoc_files = [ 16 | "LICENSE", 17 | "README.md" 18 | ] 19 | 20 | s.files = [ 21 | "LICENSE", 22 | "README.md", 23 | "Rakefile", 24 | "lib/sinatra/redirect_with_flash.rb", 25 | "test/sinatra_app.rb", 26 | "test/sinatra_redirect_with_flash_test.rb" 27 | ] 28 | 29 | s.test_files = [ 30 | "test/sinatra_app.rb", 31 | "test/sinatra_redirect_with_flash_test.rb" 32 | ] 33 | 34 | s.add_runtime_dependency(%q, [">= 1.0.0"]) 35 | s.add_development_dependency(%q, [">= 0.3.0"]) 36 | s.add_development_dependency(%q, [">= 0.3.0"]) 37 | end 38 | 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2009 Vasily Polovnyov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /test/sinatra_redirect_with_flash_test.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'rack/test' 3 | require 'sinatra_app' 4 | require 'test/unit' 5 | 6 | set :environment, :test 7 | 8 | class SinatraRedirectWithFlashTest < Test::Unit::TestCase 9 | include Rack::Test::Methods 10 | 11 | def app 12 | Sinatra::Application 13 | end 14 | 15 | def flash 16 | last_request.env['x-rack.flash'] || last_request.env['rack.session']['flash'] 17 | end 18 | 19 | def test_redirect_with_custom_flash_opts 20 | get '/' 21 | assert_not_nil flash 22 | assert_equal "sample info", flash[:info] 23 | end 24 | 25 | def test_common_flash_names 26 | Sinatra::RedirectWithFlash::COMMON_FLASH_NAMES.each do |k| 27 | get "/#{k.to_s}" 28 | assert_not_nil flash 29 | assert_equal "sample #{k.to_s}", flash[k] 30 | end 31 | end 32 | 33 | def test_redirect_with_code_and_flash 34 | get '/notice-with-code' 35 | assert_not_nil flash 36 | assert_equal flash[:notice], '301 and notice' 37 | 38 | assert_equal last_response.status, 301 39 | assert_equal last_response.body, '' 40 | assert last_response.headers['Location'] =~ /\/fff$/ 41 | end 42 | 43 | def test_old_school_redirect 44 | get '/old-school-redirect' 45 | assert_equal last_response.status, 302 46 | assert_equal last_response.body, '' 47 | assert last_response.headers['Location'] =~ /\/aaa$/ 48 | end 49 | 50 | def test_old_school_redirect_with_code 51 | get '/old-school-redirect-with-code' 52 | assert_equal last_response.status, 301 53 | assert_equal last_response.body, '' 54 | assert last_response.headers['Location'] =~ /\/aaa$/ 55 | end 56 | 57 | end -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sinatra Extension: redirect with flash 2 | 3 | [![Build Status](https://semaphoreapp.com/api/v1/projects/73a347a9-574a-4bc0-804a-eb4211230ce6/235551/badge.png)](https://semaphoreapp.com/vast/sinatra-redirect-with-flash) 4 | 5 | sinatra-redirect-with-flash provides `redirect` helper that can 6 | set proper flash (rack-flash3 or sinatra-flash) before the redirection. 7 | 8 | In fact, every time you set a flash parameter the very next step is often to 9 | perform your redirect: 10 | 11 | ```ruby 12 | post '/posts/?' do 13 | @post = Post.create(params) 14 | flash[:notice] = 'The post was successfully created' 15 | redirect "/posts/#{@post.id}" 16 | end 17 | ``` 18 | 19 | With sinatra-redirect-with-flash you can do one-line redirects. For instance, 20 | to rewrite the above example: 21 | 22 | ```ruby 23 | post '/posts/?' do 24 | @post = Post.create(params) 25 | redirect "/posts/#{@post.id}", notice: 'The post was successfully created' 26 | end 27 | ``` 28 | 29 | 30 | ##Installation 31 | 32 | If you use [bundler](http://gembundler.com/), simply specify 33 | `sinatra-redirect-with-flash` as a dependency in a Gemfile 34 | in your project's root: 35 | 36 | ```ruby 37 | gem 'rack-flash3' 38 | gem 'sinatra-redirect-with-flash' 39 | ``` 40 | 41 | and run `bundle install`. 42 | 43 | 44 | Otherwise install the gem as usual: 45 | 46 | [sudo] gem install sinatra-redirect-with-flash 47 | 48 | 49 | 50 | ##Example 51 | 52 | ```ruby 53 | require 'rubygems' 54 | require 'sinatra' 55 | require 'rack-flash' 56 | require 'sinatra/redirect_with_flash' 57 | 58 | enable :sessions 59 | 60 | post '/sessions/new' do 61 | redirect '/secret', notice: 'Logged in' 62 | # predefined keys are: :notice, :error, :warning, :alert, :info, :success 63 | end 64 | 65 | get '/foo' do 66 | redirect '/bar', 301, notice: 'redirect with 301 code' 67 | end 68 | 69 | get '/archive' do 70 | redirect '/posts/', flash: { my_msg: 'Moving on!' } 71 | end 72 | ``` 73 | 74 | *Note* that if your application subclasses `Sinatra::Base` (modular app), 75 | you have to register the extension in your subclass: 76 | 77 | ```ruby 78 | helpers Sinatra::RedirectWithFlash 79 | ``` 80 | 81 | 82 | ##Requirements 83 | 84 | Either [rack-flash3](https://github.com/treeder/rack-flash) or 85 | [sinatra-flash](https://github.com/SFEley/sinatra-flash). 86 | --------------------------------------------------------------------------------