tylerhunt-comeback 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +61 -0
- data/Rakefile +29 -0
- data/VERSION.yml +4 -0
- data/lib/comeback.rb +28 -0
- data/rails/init.rb +5 -0
- data/spec/comeback_spec.rb +47 -0
- data/spec/spec_helper.rb +10 -0
- metadata +89 -0
data/README.markdown
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
Comeback
|
2
|
+
========
|
3
|
+
|
4
|
+
Provides a mechanism for storing URLs to return to at a later time. Allows the
|
5
|
+
current URL, a custom URL, or the referer to be stored.
|
6
|
+
|
7
|
+
When a referer is stored, it is checked against the current page URL, and if
|
8
|
+
they're the same, then the referer won't be set and the default path will be
|
9
|
+
used instead.
|
10
|
+
|
11
|
+
By default, the session key :comeback_to is used to store the URL. The class
|
12
|
+
inheritable accessor comeback_session_key can be set to override this.
|
13
|
+
|
14
|
+
|
15
|
+
Example
|
16
|
+
=======
|
17
|
+
|
18
|
+
In your controller, you first call #store_referer to save the referer to the
|
19
|
+
current page, and then to access it again, call #return_or_redirect_to and pass
|
20
|
+
it the same arguments you would pass to #redirect_to as the default in case a
|
21
|
+
referer has not been set:
|
22
|
+
|
23
|
+
class MyController < ApplicationController
|
24
|
+
def first_action
|
25
|
+
store_referer
|
26
|
+
end
|
27
|
+
|
28
|
+
def second_action
|
29
|
+
return_or_redirect_to root_path
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
You can also store the referer using a before filter:
|
34
|
+
|
35
|
+
class MyController < ApplicationController
|
36
|
+
before_filter :store_referer, :only => :first_action
|
37
|
+
|
38
|
+
def first_action
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
The current page URL can also be stored instead of the referer:
|
43
|
+
|
44
|
+
class MyController < ApplicationController
|
45
|
+
def first_action
|
46
|
+
store_location
|
47
|
+
redirect_to login_path
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
Explicit URLs may also be stored:
|
52
|
+
|
53
|
+
class MyController < ApplicationController
|
54
|
+
def first_action
|
55
|
+
store_location dashbaord_path
|
56
|
+
redirect_to login_path
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
Copyright (c) 2008 Tyler Hunt, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = 'comeback'
|
9
|
+
gem.summary = 'Provides a mechanism for storing and accessing return URLs.'
|
10
|
+
gem.email = 'tyler@tylerhunt.com'
|
11
|
+
gem.homepage = 'http://github.com/tylerhunt/comeback'
|
12
|
+
gem.authors = ['Tyler Hunt']
|
13
|
+
|
14
|
+
gem.add_dependency('actionpack')
|
15
|
+
|
16
|
+
gem.add_development_dependency('jeweler', '~> 0.11.0')
|
17
|
+
gem.add_development_dependency('rspec', '~> 1.2.2')
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
puts 'Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com'
|
21
|
+
end
|
22
|
+
|
23
|
+
task :default => :spec
|
24
|
+
|
25
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
26
|
+
t.libs << 'rails'
|
27
|
+
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
|
28
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
end
|
data/VERSION.yml
ADDED
data/lib/comeback.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Comeback
|
2
|
+
def self.included(base)
|
3
|
+
base.class_inheritable_accessor :comeback_session_key
|
4
|
+
base.comeback_session_key = :comeback_to
|
5
|
+
end
|
6
|
+
|
7
|
+
def store_location(url=nil)
|
8
|
+
session[comeback_session_key] ||= url ? url : request.url
|
9
|
+
end
|
10
|
+
|
11
|
+
def store_referer
|
12
|
+
referer = request.env['HTTP_REFERER']
|
13
|
+
store_location(referer) unless referer == request.url
|
14
|
+
end
|
15
|
+
|
16
|
+
def clear_location
|
17
|
+
session[comeback_session_key] = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def return_or_redirect_to(*args)
|
21
|
+
if session[comeback_session_key]
|
22
|
+
redirect_to(session[comeback_session_key])
|
23
|
+
clear_location
|
24
|
+
else
|
25
|
+
redirect_to(*args)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "Comeback" do
|
4
|
+
before(:each) do
|
5
|
+
@comeback_controller = Class.new(ActionController::Base).class_eval do
|
6
|
+
include ActionController::TestProcess
|
7
|
+
end
|
8
|
+
|
9
|
+
@request = mock("request")
|
10
|
+
@request.stub!(:env).and_return({ "HTTP_REFERER" => "/referer" })
|
11
|
+
@request.stub!(:url).and_return("/private")
|
12
|
+
|
13
|
+
@controller = @comeback_controller.new
|
14
|
+
@controller.stub!(:request).and_return(@request)
|
15
|
+
@controller.stub!(:session).and_return({})
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "session key" do
|
19
|
+
it "should default to :comeback_to" do
|
20
|
+
@comeback_controller.comeback_session_key.should == :comeback_to
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be allowed to be overridden" do
|
24
|
+
@comeback_controller.comeback_session_key = :return_to
|
25
|
+
@comeback_controller.comeback_session_key.should == :return_to
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#store_location" do
|
30
|
+
it "should store the current location" do
|
31
|
+
@controller.store_location
|
32
|
+
@controller.session[:comeback_to].should == "/private"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should store the specified location" do
|
36
|
+
@controller.store_location("/other")
|
37
|
+
@controller.session[:comeback_to].should == "/other"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#store_referer" do
|
42
|
+
it "should store the referer" do
|
43
|
+
@controller.store_referer
|
44
|
+
@controller.session[:comeback_to].should == "/referer"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tylerhunt-comeback
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler Hunt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-03 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: jeweler
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.11.0
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.2.2
|
44
|
+
version:
|
45
|
+
description:
|
46
|
+
email: tyler@tylerhunt.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README.markdown
|
53
|
+
files:
|
54
|
+
- README.markdown
|
55
|
+
- Rakefile
|
56
|
+
- VERSION.yml
|
57
|
+
- lib/comeback.rb
|
58
|
+
- rails/init.rb
|
59
|
+
- spec/comeback_spec.rb
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
has_rdoc: false
|
62
|
+
homepage: http://github.com/tylerhunt/comeback
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --charset=UTF-8
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.2.0
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Provides a mechanism for storing and accessing return URLs.
|
87
|
+
test_files:
|
88
|
+
- spec/comeback_spec.rb
|
89
|
+
- spec/spec_helper.rb
|