jump_back 0.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jump_back/redirect_back.rb +15 -32
- data/lib/jump_back/redirection_determiner.rb +13 -0
- data/lib/jump_back/referer_interpreter.rb +34 -0
- data/lib/jump_back/return_to_referer.rb +1 -0
- data/lib/jump_back/version.rb +1 -1
- data/spec/controller/tests_controller_spec.rb +32 -0
- data/spec/dummy/app/controllers/tests_controller.rb +3 -3
- data/spec/dummy/log/development.log +5 -0
- data/spec/dummy/log/test.log +4488 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78d114e95cd15b64aa87b68a36de33da6b1a9527
|
4
|
+
data.tar.gz: 0a757f3386a15098a7199673177ff82d2e684052
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fba8920bb3deaa32ccfc7570904da6952ce791f4da0e1fdc53f9cd698c06f89e38f72785068ca43273dae777301d915f7d6523241ba5dc5c581589da84c5184
|
7
|
+
data.tar.gz: 7a47e097f79ec5f89a8b4230b4b88425c0bedc1056224a95ee50123939250b75488f56e611f0b2bd9c57f87908a7b0334cf405a5339df3b1ab9d224572899bbe
|
@@ -1,41 +1,24 @@
|
|
1
|
+
require 'jump_back/referer_interpreter'
|
2
|
+
require 'jump_back/redirection_determiner'
|
3
|
+
|
1
4
|
module JumpBack
|
5
|
+
|
2
6
|
def redirect_back(path=root_path, options={})
|
7
|
+
parsed_args = parse_jump_back_arguments(path, options)
|
8
|
+
redirect_to RedirectionDeterminer.new(request, parsed_args[:path], parsed_args[:jump_back_options]).path, parsed_args[:redirect_options]
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse_jump_back_arguments(path, options)
|
3
12
|
if path.is_a? Hash
|
4
13
|
options = path
|
5
14
|
path = root_path
|
6
15
|
end
|
7
|
-
RefererInterpreter.new.back?(request, options) ? redirect_to(:back) : redirect_to(path)
|
8
|
-
end
|
9
|
-
|
10
|
-
class RefererInterpreter
|
11
|
-
|
12
|
-
def back?(request, options)
|
13
|
-
has_referer?(request) ? is_local?(request, options) ? true : false : false
|
14
|
-
end
|
15
|
-
|
16
|
-
def has_referer?(request)
|
17
|
-
!request.env["HTTP_REFERER"].blank? and request.env["HTTP_REFERER"] != request.env["REQUEST_URI"]
|
18
|
-
end
|
19
|
-
|
20
|
-
def is_local?(request, options)
|
21
|
-
return true if options[:offsite]
|
22
|
-
host = host(request.env["HTTP_REFERER"])
|
23
|
-
!(host && host != request.host)
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def host(string)
|
29
|
-
return URI.parse(string).host if uri? string
|
30
|
-
end
|
31
16
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
false
|
39
|
-
end
|
17
|
+
jump_back_options = { offsite: options.delete(:offsite) }
|
18
|
+
{
|
19
|
+
redirect_options: options,
|
20
|
+
jump_back_options: jump_back_options,
|
21
|
+
path: path
|
22
|
+
}
|
40
23
|
end
|
41
24
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module JumpBack
|
2
|
+
class RedirectionDeterminer
|
3
|
+
attr_reader :path
|
4
|
+
|
5
|
+
def initialize(request, path, options)
|
6
|
+
@path = path_determiner(request, path, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def path_determiner(request, path, options)
|
10
|
+
RefererInterpreter.new.back?(request, options) ? :back : path
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module JumpBack
|
2
|
+
|
3
|
+
class RefererInterpreter
|
4
|
+
|
5
|
+
def back?(request, options)
|
6
|
+
has_referer?(request) ? is_local?(request, options) ? true : false : false
|
7
|
+
end
|
8
|
+
|
9
|
+
def has_referer?(request)
|
10
|
+
!request.env["HTTP_REFERER"].blank? and request.env["HTTP_REFERER"] != request.env["REQUEST_URI"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def is_local?(request, options)
|
14
|
+
return true if options[:offsite]
|
15
|
+
host = host(request.env["HTTP_REFERER"])
|
16
|
+
!(host && host != request.host)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def host(string)
|
22
|
+
return URI.parse(string).host if uri? string
|
23
|
+
end
|
24
|
+
|
25
|
+
def uri?(string)
|
26
|
+
uri = URI.parse(string)
|
27
|
+
%w( http https ).include?(uri.scheme)
|
28
|
+
rescue URI::BadURIError
|
29
|
+
false
|
30
|
+
rescue URI::InvalidURIError
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/jump_back/version.rb
CHANGED
@@ -46,6 +46,38 @@ describe TestsController, type: :controller do
|
|
46
46
|
get :offsite_without_default
|
47
47
|
expect(response).to redirect_to(root_path)
|
48
48
|
end
|
49
|
+
|
50
|
+
it 'should pass additional options to redirect_to' do
|
51
|
+
post :create
|
52
|
+
expect(flash[:notice]).to eq('Created!')
|
53
|
+
expect(response.status).to eq(301)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should pass additional options with offsite set to true with default specified and a referer' do
|
57
|
+
@request.env['HTTP_REFERER'] = 'http://rubyonrails.org/'
|
58
|
+
get :offsite_with_default
|
59
|
+
expect(response).to redirect_to('http://rubyonrails.org/')
|
60
|
+
expect(flash[:alert]).to eq('Offsite with default!')
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should pass additional options with offsite set to true without default specified and a referer' do
|
64
|
+
@request.env['HTTP_REFERER'] = 'http://rubyonrails.org/'
|
65
|
+
get :offsite_without_default
|
66
|
+
expect(response).to redirect_to('http://rubyonrails.org/')
|
67
|
+
expect(flash[:alert]).to eq('Offsite without default!')
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should pass additional options with offsite set to true with default specified and no referer' do
|
71
|
+
get :offsite_with_default
|
72
|
+
expect(response).to redirect_to(new_test_path)
|
73
|
+
expect(flash[:alert]).to eq('Offsite with default!')
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should pass additional options with offsite set to true without default specified and no referer' do
|
77
|
+
get :offsite_without_default
|
78
|
+
expect(response).to redirect_to(root_path)
|
79
|
+
expect(flash[:alert]).to eq('Offsite without default!')
|
80
|
+
end
|
49
81
|
end
|
50
82
|
|
51
83
|
describe 'save_referer' do
|
@@ -11,7 +11,7 @@ class TestsController < ApplicationController
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def create
|
14
|
-
redirect_back
|
14
|
+
redirect_back notice: 'Created!', status: :moved_permanently
|
15
15
|
end
|
16
16
|
|
17
17
|
def edit
|
@@ -29,10 +29,10 @@ class TestsController < ApplicationController
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def offsite_with_default
|
32
|
-
redirect_back new_test_path, offsite: true
|
32
|
+
redirect_back new_test_path, offsite: true, alert: 'Offsite with default!'
|
33
33
|
end
|
34
34
|
|
35
35
|
def offsite_without_default
|
36
|
-
redirect_back offsite: true
|
36
|
+
redirect_back offsite: true, alert: 'Offsite without default!'
|
37
37
|
end
|
38
38
|
end
|
@@ -7,3 +7,8 @@
|
|
7
7
|
[1m[36m (1.4ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
8
8
|
[1m[35m (0.2ms)[0m SELECT version FROM "schema_migrations"
|
9
9
|
[1m[36m (1.2ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('0')[0m
|
10
|
+
[1m[36m (1.0ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
11
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
12
|
+
[1m[36m (1.7ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
13
|
+
[1m[35m (0.2ms)[0m SELECT version FROM "schema_migrations"
|
14
|
+
[1m[36m (1.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('0')[0m
|