jump_back 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +15 -0
  4. data/lib/jump_back.rb +6 -0
  5. data/lib/jump_back/engine.rb +12 -0
  6. data/lib/jump_back/redirect_back.rb +41 -0
  7. data/lib/jump_back/return_to_referer.rb +13 -0
  8. data/lib/jump_back/version.rb +3 -0
  9. data/lib/tasks/jump_back_tasks.rake +4 -0
  10. data/spec/controller/application_controller_spec.rb +18 -0
  11. data/spec/controller/tests_controller_spec.rb +122 -0
  12. data/spec/dummy/README.rdoc +28 -0
  13. data/spec/dummy/Rakefile +6 -0
  14. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  15. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  16. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  17. data/spec/dummy/app/controllers/tests_controller.rb +38 -0
  18. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  19. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  20. data/spec/dummy/app/views/tests/edit.html.erb +3 -0
  21. data/spec/dummy/app/views/tests/index.html.erb +6 -0
  22. data/spec/dummy/app/views/tests/new.html.erb +6 -0
  23. data/spec/dummy/bin/bundle +3 -0
  24. data/spec/dummy/bin/rails +4 -0
  25. data/spec/dummy/bin/rake +4 -0
  26. data/spec/dummy/config.ru +4 -0
  27. data/spec/dummy/config/application.rb +29 -0
  28. data/spec/dummy/config/boot.rb +5 -0
  29. data/spec/dummy/config/database.yml +25 -0
  30. data/spec/dummy/config/environment.rb +5 -0
  31. data/spec/dummy/config/environments/development.rb +37 -0
  32. data/spec/dummy/config/environments/production.rb +78 -0
  33. data/spec/dummy/config/environments/test.rb +39 -0
  34. data/spec/dummy/config/initializers/assets.rb +8 -0
  35. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  37. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  38. data/spec/dummy/config/initializers/inflections.rb +16 -0
  39. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  40. data/spec/dummy/config/initializers/session_store.rb +3 -0
  41. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  42. data/spec/dummy/config/locales/en.yml +23 -0
  43. data/spec/dummy/config/routes.rb +61 -0
  44. data/spec/dummy/config/secrets.yml +22 -0
  45. data/spec/dummy/db/development.sqlite3 +0 -0
  46. data/spec/dummy/db/schema.rb +16 -0
  47. data/spec/dummy/db/test.sqlite3 +0 -0
  48. data/spec/dummy/log/development.log +9 -0
  49. data/spec/dummy/log/test.log +7229 -0
  50. data/spec/dummy/public/404.html +67 -0
  51. data/spec/dummy/public/422.html +67 -0
  52. data/spec/dummy/public/500.html +66 -0
  53. data/spec/dummy/public/favicon.ico +0 -0
  54. data/spec/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  55. data/spec/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  56. data/spec/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  57. data/spec/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  58. data/spec/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  59. data/spec/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  60. data/spec/features/jump_back_feature_spec.rb +38 -0
  61. data/spec/spec_helper.rb +13 -0
  62. metadata +214 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c5c25bd37934df5bd36a00c7298d6daecb311a5d
4
+ data.tar.gz: 260c81ec52405db3d5c7f3f8706042104a0ac180
5
+ SHA512:
6
+ metadata.gz: d774bb9f4e0e6de408df40d3752487100d679d71cbc270fd6f1cdd51d59c136859ed0f10e198a7398268183bd3a57bce2742d42f1f9da7532c05463c4eb996e9
7
+ data.tar.gz: f3470c7d4c1a021446d0335147a9a14f3e5f70167ed68ec862ebbfc8e30577168b13a599e8094e5ecf97031429521bcaa36ab09c93b37608f68159bf07f6d623
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Peter Debelak
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
8
+ load 'rails/tasks/engine.rake'
9
+ Bundler::GemHelper.install_tasks
10
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
11
+ require 'rspec/core'
12
+ require 'rspec/core/rake_task'
13
+ desc "Run all specs in spec directory (excluding plugin specs)"
14
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
15
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ require 'jump_back/engine'
2
+ require 'jump_back/redirect_back'
3
+ require 'jump_back/return_to_referer'
4
+
5
+ module JumpBack
6
+ end
@@ -0,0 +1,12 @@
1
+ module JumpBack
2
+ class Engine < ::Rails::Engine
3
+ config.generators do |g|
4
+ g.test_framework :rspec, :fixture => false
5
+ g.assets false
6
+ g.helper false
7
+ end
8
+ initializer "jump_back.methods" do |app|
9
+ ApplicationController.send :include, JumpBack
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,41 @@
1
+ module JumpBack
2
+ def redirect_back(path=root_path, options={})
3
+ if path.is_a? Hash
4
+ options = path
5
+ path = root_path
6
+ 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
+
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
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ module JumpBack
2
+ def save_referer
3
+ session[:jump_back_stored_referer] ||= request.referer
4
+ end
5
+
6
+ def return_to_referer(path=root_path)
7
+ session[:jump_back_stored_referer] ? redirect_to(clear_referer) : redirect_to(path)
8
+ end
9
+
10
+ def clear_referer
11
+ session.delete(:jump_back_stored_referer)
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module JumpBack
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :jump_back do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,18 @@
1
+ describe ApplicationController do
2
+
3
+ it 'should have the redirect_back method' do
4
+ expect(ApplicationController.new).to respond_to :redirect_back
5
+ end
6
+
7
+ it 'should have the save_referer method' do
8
+ expect(ApplicationController.new).to respond_to :save_referer
9
+ end
10
+
11
+ it 'should have the return_to_referer method' do
12
+ expect(ApplicationController.new).to respond_to :return_to_referer
13
+ end
14
+
15
+ it 'should have the clear_referer method' do
16
+ expect(ApplicationController.new).to respond_to :clear_referer
17
+ end
18
+ end
@@ -0,0 +1,122 @@
1
+ describe TestsController, type: :controller do
2
+
3
+ describe 'redirect_back' do
4
+
5
+ it 'should redirect to default with no referer and no arguments' do
6
+ post :create
7
+ expect(response).to redirect_to(root_path)
8
+ end
9
+
10
+ it 'should redirect to the supplied path with no referer' do
11
+ delete :destroy, id: 1
12
+ expect(response).to redirect_to(new_test_path)
13
+ end
14
+
15
+ it 'should redirect back with a referer' do
16
+ @request.env['HTTP_REFERER'] = test_path(1)
17
+ post :create
18
+ expect(response).to redirect_to(test_path(1))
19
+ delete :destroy, id: 2
20
+ expect(response).to redirect_to(test_path(1))
21
+ end
22
+
23
+ it 'should not redirect back when the referer is offsite' do
24
+ @request.env['HTTP_REFERER'] = 'http://rubyonrails.org/'
25
+ post :create
26
+ expect(response).to redirect_to(root_path)
27
+ delete :destroy, id: 1
28
+ expect(response).to redirect_to(new_test_path)
29
+ end
30
+
31
+ it 'should redirect back when referer is offsite with offsite set to true' do
32
+ @request.env['HTTP_REFERER'] = 'http://rubyonrails.org/'
33
+ get :offsite_with_default
34
+ expect(response).to redirect_to('http://rubyonrails.org/')
35
+ end
36
+
37
+ it 'should redirect back when referer is offsite with offsite set to true with no path argument' do
38
+ @request.env['HTTP_REFERER'] = 'http://rubyonrails.org/'
39
+ get :offsite_without_default
40
+ expect(response).to redirect_to('http://rubyonrails.org/')
41
+ end
42
+
43
+ it 'should work normally with no referer if offsite is true' do
44
+ get :offsite_with_default
45
+ expect(response).to redirect_to(new_test_path)
46
+ get :offsite_without_default
47
+ expect(response).to redirect_to(root_path)
48
+ end
49
+ end
50
+
51
+ describe 'save_referer' do
52
+
53
+ it 'should save the referer in the session' do
54
+ @request.env['HTTP_REFERER'] = test_path(1)
55
+ get :new
56
+ expect(session[:jump_back_stored_referer]).to eq(test_path(1))
57
+ end
58
+
59
+ it 'should not overwrite the referer once saved' do
60
+ @request.env['HTTP_REFERER'] = test_path(1)
61
+ get :new
62
+ @request.env['HTTP_REFERER'] = new_test_path
63
+ get :new
64
+ expect(session[:jump_back_stored_referer]).to eq(test_path(1))
65
+ end
66
+ end
67
+
68
+ describe 'clear_referer' do
69
+
70
+ it 'should remove the stored referer' do
71
+ @request.env['HTTP_REFERER'] = test_path(1)
72
+ get :new
73
+ expect(session[:jump_back_stored_referer]).to eq(test_path(1))
74
+ delete :destroy, id: 1
75
+ expect(session[:jump_back_stored_referer]).to be_nil
76
+ end
77
+
78
+ it 'should remove the stored referer so it can be written' do
79
+ @request.env['HTTP_REFERER'] = test_path(1)
80
+ get :new
81
+ expect(session[:jump_back_stored_referer]).to eq(test_path(1))
82
+ @request.env['HTTP_REFERER'] = new_test_path
83
+ get :edit, id: 1
84
+ expect(session[:jump_back_stored_referer]).to eq(new_test_path)
85
+ end
86
+ end
87
+
88
+ describe 'return_to_referer' do
89
+ it 'should redirect to saved referer' do
90
+ @request.env['HTTP_REFERER'] = test_path(1)
91
+ get :new
92
+ put :update, id: 1
93
+ expect(response).to redirect_to(test_path(1))
94
+ end
95
+
96
+ it 'should redirect to saved referer even with actions in between' do
97
+ @request.env['HTTP_REFERER'] = test_path(1)
98
+ get :new
99
+ post :create
100
+ put :update, id: 1
101
+ expect(response).to redirect_to(test_path(1))
102
+ end
103
+
104
+ it 'should delete the stored referer' do
105
+ @request.env['HTTP_REFERER'] = test_path(1)
106
+ get :new
107
+ expect(session[:jump_back_stored_referer]).to eq(test_path(1))
108
+ put :update, id: 1
109
+ expect(session[:jump_back_stored_referer]).to be_nil
110
+ end
111
+
112
+ it 'should redirect to default with no referer and no arguments' do
113
+ put :update, id: 1
114
+ expect(response).to redirect_to(root_path)
115
+ end
116
+
117
+ it 'should redirect to argument with no referer' do
118
+ get :show, id: 1
119
+ expect(response).to redirect_to(new_test_path)
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,38 @@
1
+ class TestsController < ApplicationController
2
+ def index
3
+ end
4
+
5
+ def show
6
+ return_to_referer new_test_path
7
+ end
8
+
9
+ def new
10
+ save_referer
11
+ end
12
+
13
+ def create
14
+ redirect_back
15
+ end
16
+
17
+ def edit
18
+ clear_referer
19
+ save_referer
20
+ end
21
+
22
+ def update
23
+ return_to_referer
24
+ end
25
+
26
+ def destroy
27
+ clear_referer
28
+ redirect_back new_test_path
29
+ end
30
+
31
+ def offsite_with_default
32
+ redirect_back new_test_path, offsite: true
33
+ end
34
+
35
+ def offsite_without_default
36
+ redirect_back offsite: true
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ Edit Page
2
+
3
+ <%= link_to 'Go to new page', new_test_path %>
@@ -0,0 +1,6 @@
1
+ Index Page
2
+ <%= form_tag test_path(1), method: :put do %>
3
+ <%= submit_tag 'Update test' %>
4
+ <% end %>
5
+
6
+ <%= link_to 'Go to new page', new_test_path %>
@@ -0,0 +1,6 @@
1
+ New Page
2
+ <%= form_tag tests_path do %>
3
+ <%= submit_tag 'New test' %>
4
+ <% end %>
5
+
6
+ <%= link_to 'Go to edit page', edit_test_path(1) %>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run