backtrail 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21168de9bcbf086287dbd7d4a3569f2fccad0cc5
4
+ data.tar.gz: 7eb6730f974c9c9ba7e8485bcd965fc5d4995e48
5
+ SHA512:
6
+ metadata.gz: e245f024438576b408bc587e271b9bfc37960c3cfeb14742eb07406b6efb1782d48e4c81174e187a0f460f77b4f86267314690137bb848fd735084dc89533e6d
7
+ data.tar.gz: b21c678aed7ff102f3700bc55683a61a1c06510b85b2c1d0285d843065a2830b0d29782d262fe440c2ffb254dc599d363a48c31adadb2188266269c8460414ce
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .ruby-version
2
+ .ruby-gemset
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ backtrail (0.1.0)
5
+ railties (>= 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (4.2.1)
11
+ actionview (= 4.2.1)
12
+ activesupport (= 4.2.1)
13
+ rack (~> 1.6)
14
+ rack-test (~> 0.6.2)
15
+ rails-dom-testing (~> 1.0, >= 1.0.5)
16
+ rails-html-sanitizer (~> 1.0, >= 1.0.1)
17
+ actionview (4.2.1)
18
+ activesupport (= 4.2.1)
19
+ builder (~> 3.1)
20
+ erubis (~> 2.7.0)
21
+ rails-dom-testing (~> 1.0, >= 1.0.5)
22
+ rails-html-sanitizer (~> 1.0, >= 1.0.1)
23
+ activesupport (4.2.1)
24
+ i18n (~> 0.7)
25
+ json (~> 1.7, >= 1.7.7)
26
+ minitest (~> 5.1)
27
+ thread_safe (~> 0.3, >= 0.3.4)
28
+ tzinfo (~> 1.1)
29
+ builder (3.2.2)
30
+ erubis (2.7.0)
31
+ i18n (0.7.0)
32
+ json (1.8.2)
33
+ loofah (2.0.1)
34
+ nokogiri (>= 1.5.9)
35
+ metaclass (0.0.4)
36
+ mini_portile (0.6.2)
37
+ minitest (5.6.0)
38
+ mocha (1.1.0)
39
+ metaclass (~> 0.0.1)
40
+ nokogiri (1.6.6.2)
41
+ mini_portile (~> 0.6.0)
42
+ rack (1.6.0)
43
+ rack-test (0.6.3)
44
+ rack (>= 1.0)
45
+ rails-deprecated_sanitizer (1.0.3)
46
+ activesupport (>= 4.2.0.alpha)
47
+ rails-dom-testing (1.0.6)
48
+ activesupport (>= 4.2.0.beta, < 5.0)
49
+ nokogiri (~> 1.6.0)
50
+ rails-deprecated_sanitizer (>= 1.0.1)
51
+ rails-html-sanitizer (1.0.2)
52
+ loofah (~> 2.0)
53
+ railties (4.2.1)
54
+ actionpack (= 4.2.1)
55
+ activesupport (= 4.2.1)
56
+ rake (>= 0.8.7)
57
+ thor (>= 0.18.1, < 2.0)
58
+ rake (10.4.2)
59
+ thor (0.19.1)
60
+ thread_safe (0.3.5)
61
+ tzinfo (1.2.2)
62
+ thread_safe (~> 0.1)
63
+
64
+ PLATFORMS
65
+ ruby
66
+
67
+ DEPENDENCIES
68
+ backtrail!
69
+ mocha
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Daniel Ferraz
2
+
3
+ MIT License
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Backtrail
2
+
3
+ Keep a trail of request paths for your Rails application
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'backtrail'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install backtrail
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ In your `ApplicationController` add:
28
+
29
+ ```ruby
30
+ class ApplicationController < ActionController::Base
31
+ include Backtrail::BaseController
32
+ end
33
+ ```
34
+
35
+ With that, all your `get` and `non xhr` request paths will start to be keep tracked on a trail stack. To generate links that go back on the trail of paths, use the view helper `<%= backtrail %>`. This will generate a link like:
36
+
37
+ ```html
38
+ <a class="backtrail" href="/[path]?trail=back">Back</a>
39
+ ```
40
+
41
+ The `[path]` is the top path from the trail stack. It can be returned through the `previous_path` method (that is also a view helper) included with `Backtrail::BaseController`.
42
+
43
+ ## Contributing
44
+
45
+ Questions or problems? Please post them on the [issue tracker](https://github.com/dferrazm/backtrail/issues).
46
+
47
+ You can contribute by doing the following:
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
54
+
55
+ To test the application run `bundle install` and then `rake test`.
56
+
57
+ ## License
58
+
59
+ MIT License.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ require 'rake/testtask'
10
+
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'lib'
13
+ t.libs << 'test'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = false
16
+ end
data/backtrail.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require "backtrail/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "backtrail"
7
+ s.version = Backtrail::VERSION
8
+ s.authors = ["Daniel Ferraz"]
9
+ s.email = ["d.ferrazm@gmail.com"]
10
+ s.homepage = "https://github.com/dferrazm/backtrail"
11
+ s.summary = "Keep a trail of request paths for your Rails application"
12
+ s.description = "Keep a trail of request paths for your Rails application"
13
+ s.license = "MIT"
14
+
15
+ s.files = `git ls-files`.split($/)
16
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'railties', '>= 4.0'
21
+ s.add_development_dependency 'mocha'
22
+ end
data/lib/backtrail.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'backtrail/controllers'
2
+ require 'backtrail/helpers'
3
+ require 'backtrail/railtie' if defined?(Rails)
4
+
5
+ module Backtrail;end
@@ -0,0 +1,46 @@
1
+ module Backtrail
2
+ module BaseController
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ before_action :update_previous_paths
7
+ helper_method :previous_path
8
+ end
9
+
10
+ def previous_path
11
+ get_from_stack_path || backtrail_starting_point
12
+ end
13
+
14
+ protected
15
+
16
+ def update_previous_paths
17
+ current = request.url.split('?')[0]
18
+ if request.get? and !request.xhr?
19
+ if session['_referer'].present? and !params[:trail]
20
+ previous = session['_referer']
21
+ if session['previous_paths'].nil?
22
+ session['previous_paths'] = []
23
+ end
24
+ if session['previous_paths'].length == 5
25
+ session['previous_paths'].delete_at 0
26
+ end
27
+ if previous != current
28
+ session['previous_paths'].push previous
29
+ end
30
+ end
31
+ session['_referer'] = current
32
+ end
33
+ end
34
+
35
+ def get_from_stack_path
36
+ if params[:trail] == 'back'
37
+ session['previous_paths'].try(:pop)
38
+ end
39
+ session['previous_paths'].try(:last)
40
+ end
41
+
42
+ def backtrail_starting_point
43
+ root_path
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ module Backtrail
2
+ module ViewHelpers
3
+ def backtrail
4
+ link_to 'Back', "#{previous_path}?trail=back", class: 'backtrail'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module MyGem
2
+ class Railtie < Rails::Railtie
3
+ initializer "backtrail.view_helpers" do
4
+ ActionView::Base.send :include, Backtrail::ViewHelpers
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Backtrail
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,135 @@
1
+ require 'test_helper'
2
+ require 'action_controller'
3
+ require 'backtrail/controllers'
4
+
5
+ class FooController < ActionController::Base
6
+ include Backtrail::BaseController
7
+ end
8
+
9
+ class ControllersTest < ActionController::TestCase
10
+
11
+ # returns the default path when the paths's stack is nil
12
+ def test_previous_path_when_stack_is_nil
13
+ controller = new_controller
14
+ assert_equal '/root_path', controller.previous_path
15
+ end
16
+
17
+ # returns the root path when the paths's stack is empty
18
+ def test_previous_path_when_stack_is_empty
19
+ controller = new_controller previous_paths: []
20
+ assert_equal '/root_path', controller.previous_path
21
+ end
22
+
23
+ # holds only the last 5 requests paths
24
+ def test_update_previous_paths_max_storage
25
+ controller = new_controller
26
+
27
+ paths = (1..7).map { |i| "/path#{i}" }
28
+ trail controller, paths
29
+
30
+ assert_equal 5, controller.session['previous_paths'].length
31
+ end
32
+
33
+ # does not save the previous path when referrer equals to the current
34
+ def test_when_referrer_equals_to_current
35
+ controller = new_controller
36
+ fake_request controller, "/same_path", "/same_path"
37
+ assert_equal 0, controller.session['previous_paths'].length
38
+ end
39
+
40
+ # returns the top path from the stack when going forward
41
+ def test_when_going_forward
42
+ controller = new_controller
43
+ trail controller, ['/path1', '/path2']
44
+
45
+ assert_equal '/path1', controller.previous_path
46
+ assert_equal 1, controller.session['previous_paths'].length
47
+ end
48
+
49
+ # pops the top path and returns the former when going back
50
+ def test_when_going_backwards
51
+ controller = new_controller
52
+ trail controller, ['/path1', '/path2', '/path3']
53
+ go_back controller, '/path2'
54
+
55
+ assert_equal '/path1', controller.previous_path
56
+ assert_equal 1, controller.session['previous_paths'].length
57
+ end
58
+
59
+ # does not save the path on non GET requests
60
+ def test_on_non_get_requests
61
+ controller = new_controller
62
+ fake_request controller, '/path1', '/referrer', get?: false
63
+ assert_equal nil, controller.session['previous_paths']
64
+ end
65
+
66
+ # does not save the path when on async request
67
+ def test_on_async_requests
68
+ controller = new_controller
69
+ fake_request controller, '/path1', '/referrer', xhr?: true
70
+ assert_equal nil, controller.session['previous_paths']
71
+ end
72
+
73
+ # does not save the path when there's no referer
74
+ def test_on_non_referrer_requests
75
+ controller = new_controller
76
+ fake_request controller, '/path1', nil
77
+ assert_equal nil, controller.session['previous_paths']
78
+ end
79
+
80
+ # does not save the path when going back
81
+ def test_on_back_requests
82
+ controller = new_controller
83
+ go_back controller, '/path1'
84
+ assert_equal nil, controller.session['previous_paths']
85
+ end
86
+
87
+ # does not save the referer on async request
88
+ def test_referrer_on_async_requests
89
+ controller = new_controller
90
+ fake_request controller, '/path1', nil, xhr?: true
91
+ assert_equal nil, controller.session['_referer']
92
+ end
93
+
94
+ # does not save the referer on non GET method
95
+ def test_referrer_on_non_get_requests
96
+ controller = new_controller
97
+ fake_request controller, '/path1', nil, get?: false
98
+ assert_equal nil, controller.session['_referer']
99
+ end
100
+
101
+ private
102
+
103
+ def new_controller(params = {})
104
+ controller = FooController.new
105
+ controller.stubs(:params).returns Hash.new
106
+ controller.stubs(:session).returns({'previous_paths' => params[:previous_paths]})
107
+ controller.stubs(:root_path).returns '/root_path'
108
+ request = mock 'object'
109
+ controller.stubs(:request).returns request
110
+
111
+ controller
112
+ end
113
+
114
+ def trail(controller, path_trail)
115
+ referrer = nil
116
+
117
+ path_trail.each do |path|
118
+ fake_request controller, path, referrer
119
+ referrer = path
120
+ end
121
+ end
122
+
123
+ def fake_request(controller, path, referrer, options = {})
124
+ controller.request.stubs(:get?).returns(options[:get?].nil? ? true : options[:get?])
125
+ controller.request.stubs(:xhr?).returns(options[:xhr?] || false)
126
+ controller.request.stubs(:url).returns path
127
+ controller.session['_referer'] = referrer
128
+ controller.send :update_previous_paths
129
+ end
130
+
131
+ def go_back(controller, path)
132
+ controller.params[:trail] = 'back'
133
+ trail controller, [path]
134
+ end
135
+ end
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+ require 'action_view/test_case'
3
+ require 'backtrail/helpers'
4
+
5
+ class HelpersTest < ActionView::TestCase
6
+ include Backtrail::ViewHelpers
7
+
8
+ def test_backtrail_link
9
+ self.stubs(:previous_path).returns '/previous'
10
+ assert_equal '<a class="backtrail" href="/previous?trail=back">Back</a>', backtrail
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require 'bundler/setup'
2
+ require 'active_support'
3
+ require 'minitest'
4
+ require 'minitest/autorun'
5
+ require 'minitest/unit'
6
+ require 'mocha/mini_test'
7
+
8
+ # Configure Rails Environment
9
+ ENV["RAILS_ENV"] = "test"
10
+
11
+ Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test)
12
+
13
+ # Filter out Minitest backtrace while allowing backtrace from other libraries
14
+ # to be shown.
15
+ Minitest.backtrace_filter = Minitest::BacktraceFilter.new
16
+
17
+ ActiveSupport::TestCase.test_order = :random
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backtrail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Ferraz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mocha
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Keep a trail of request paths for your Rails application
42
+ email:
43
+ - d.ferrazm@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - backtrail.gemspec
55
+ - lib/backtrail.rb
56
+ - lib/backtrail/controllers.rb
57
+ - lib/backtrail/helpers.rb
58
+ - lib/backtrail/railtie.rb
59
+ - lib/backtrail/version.rb
60
+ - test/controllers_test.rb
61
+ - test/helpers_test.rb
62
+ - test/test_helper.rb
63
+ homepage: https://github.com/dferrazm/backtrail
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.4.5
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Keep a trail of request paths for your Rails application
87
+ test_files:
88
+ - test/controllers_test.rb
89
+ - test/helpers_test.rb
90
+ - test/test_helper.rb