sinatra-redirect-with-flash 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
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.
@@ -0,0 +1,44 @@
1
+ #Sinatra Extension: redirect with flash
2
+
3
+ Shortly: sinatra-redirect-with-flash provides `redirect` helper that can set proper flash (rack-flash) before the
4
+ redirection
5
+
6
+ In fact, every time you set a flash parameter the very next step is often to perform your redirect:
7
+
8
+ post '/posts/?' do
9
+ @post = Post.new(params)
10
+ flash[:notice] = 'The post was successfully created'
11
+ redirect "/posts/#{@post.id}"
12
+ end
13
+
14
+ With sinatra-redirect-with-flash you can do one-line redirects. For instance, to rewrite the above example:
15
+
16
+ post '/posts/?' do
17
+ @post = Post.new(params)
18
+ redirect "/posts/#{@post.id}", :notice => 'The post was successfully created'
19
+ end
20
+
21
+ ##Example
22
+ require 'rubygems'
23
+ require 'sinatra'
24
+ require 'rack-flash'
25
+ gem 'sinatra-redirect-with-flash'
26
+ require 'sinatra/redirect_with_flash'
27
+
28
+ use Rack::Flash
29
+ enable :sessions
30
+
31
+ post '/sessions/new' do
32
+ redirect '/secret', :notice => 'Logged in' #predefined keys are: :notice, :error, :warning, :alert, :info
33
+ end
34
+
35
+ get '/foo' do
36
+ redirect '/bar', 301, :notice => 'redirect with 301 code'
37
+ end
38
+
39
+ get '/archive' do
40
+ redirect '/posts/', :flash => {:my_msg => 'Moving on!'}
41
+ end
42
+
43
+ ##Requirements
44
+ * [rack-flash](http://nakajima.github.com/rack-flash/)
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rake/testtask'
3
+ require 'rake/gempackagetask'
4
+
5
+ task :default => :test
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gemspec|
10
+ gemspec.name = "sinatra-redirect-with-flash"
11
+ gemspec.version = "0.1.1"
12
+ gemspec.summary = gemspec.description = "redirect with flash helper for Sinatra"
13
+ gemspec.email = "vasily@polovnyov.ru"
14
+ gemspec.homepage = "http://github.com/vast/sinatra-redirect-with-flash"
15
+ gemspec.authors = ["Vasily Polovnyov"]
16
+
17
+ gemspec.add_dependency 'sinatra', '>=0.9.1'
18
+ gemspec.add_dependency 'rack', '>=0.9.1'
19
+ gemspec.add_dependency 'rack-flash', '>=0.1.1'
20
+
21
+ gemspec.add_development_dependency 'rack-test', '>=0.3.0'
22
+
23
+ gemspec.test_files = Dir.glob('test/*')
24
+ gemspec.files = ["LICENSE", "README.md", "Rakefile"] + Dir.glob('lib/**/*') + gemspec.test_files
25
+
26
+ end
27
+ rescue LoadError
28
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
29
+ end
30
+
31
+ Rake::TestTask.new do |t|
32
+ t.libs << "test"
33
+ t.test_files = FileList['test/*_test.rb']
34
+ t.verbose = true
35
+ end
@@ -0,0 +1,28 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module RedirectWithFlash
5
+ COMMON_FLASH_NAMES = [:notice, :alert, :warning, :error, :info]
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
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'rack-flash'
4
+ require 'lib/sinatra/redirect_with_flash'
5
+
6
+ use Rack::Flash
7
+
8
+ get '/' do
9
+ redirect '/fff', :flash => {:info => 'sample info'}
10
+ end
11
+
12
+ [:notice, :error, :warning, :alert].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
@@ -0,0 +1,57 @@
1
+ require 'sinatra_app'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require 'rack/flash/test'
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']
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
+ [:notice, :error, :warning, :alert].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_equal 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_equal 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_equal last_response.headers['Location'], '/aaa'
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-redirect-with-flash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Vasily Polovnyov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-28 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rack
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.1
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rack-flash
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.1.1
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: rack-test
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.3.0
54
+ version:
55
+ description: redirect with flash helper for Sinatra
56
+ email: vasily@polovnyov.ru
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.md
64
+ files:
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/sinatra/redirect_with_flash.rb
69
+ - test/sinatra_app.rb
70
+ - test/sinatra_redirect_with_flash_test.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/vast/sinatra-redirect-with-flash
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --charset=UTF-8
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.5
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: redirect with flash helper for Sinatra
99
+ test_files:
100
+ - test/sinatra_redirect_with_flash_test.rb
101
+ - test/sinatra_app.rb