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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5c25bd37934df5bd36a00c7298d6daecb311a5d
4
- data.tar.gz: 260c81ec52405db3d5c7f3f8706042104a0ac180
3
+ metadata.gz: 78d114e95cd15b64aa87b68a36de33da6b1a9527
4
+ data.tar.gz: 0a757f3386a15098a7199673177ff82d2e684052
5
5
  SHA512:
6
- metadata.gz: d774bb9f4e0e6de408df40d3752487100d679d71cbc270fd6f1cdd51d59c136859ed0f10e198a7398268183bd3a57bce2742d42f1f9da7532c05463c4eb996e9
7
- data.tar.gz: f3470c7d4c1a021446d0335147a9a14f3e5f70167ed68ec862ebbfc8e30577168b13a599e8094e5ecf97031429521bcaa36ab09c93b37608f68159bf07f6d623
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
- def uri?(string)
33
- uri = URI.parse(string)
34
- %w( http https ).include?(uri.scheme)
35
- rescue URI::BadURIError
36
- false
37
- rescue URI::InvalidURIError
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
@@ -1,4 +1,5 @@
1
1
  module JumpBack
2
+
2
3
  def save_referer
3
4
  session[:jump_back_stored_referer] ||= request.referer
4
5
  end
@@ -1,3 +1,3 @@
1
1
  module JumpBack
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -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
   (1.4ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
8
8
   (0.2ms) SELECT version FROM "schema_migrations"
9
9
   (1.2ms) INSERT INTO "schema_migrations" (version) VALUES ('0')
10
+  (1.0ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
11
+  (0.1ms) select sqlite_version(*)
12
+  (1.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
13
+  (0.2ms) SELECT version FROM "schema_migrations"
14
+  (1.0ms) INSERT INTO "schema_migrations" (version) VALUES ('0')