redirectr 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +50 -0
- data/init.rb +0 -0
- data/lib/redirectr.rb +135 -0
- metadata +69 -0
data/README
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Redirectr
|
2
|
+
=========
|
3
|
+
|
4
|
+
In many web applications, the user triggers actions that result in simple or complex workflows that should, after that workflow is finished, result in the user being redirected to the page where he initially started it. Another example would be a "back"-Link on any page.
|
5
|
+
A simple but completely Un-RESTful way would be to store the "current" page in a cookie each time the user calls an action and redirect to the url stored there if needed.
|
6
|
+
|
7
|
+
A much better (and potentially even cacheable) way is to encode the "backlink" URL in an URL parameter or form field and pass it along with every workflow step until the last (or only) action uses it to redirect the user back to where he initially came from.
|
8
|
+
|
9
|
+
Redirectr really does nothing more than provide a simple API for exactly that.
|
10
|
+
|
11
|
+
Redirectr provides a few Controller and Helper methods that will be included in your ApplicationController and ApplicationHelper, respectively.
|
12
|
+
|
13
|
+
Installation
|
14
|
+
============
|
15
|
+
|
16
|
+
With Rails 3, just at the following to your Gemfile
|
17
|
+
|
18
|
+
gem 'redirectr', :git => 'https://github.com/wvk/redirectr.git'
|
19
|
+
|
20
|
+
and then call
|
21
|
+
|
22
|
+
bundle install
|
23
|
+
|
24
|
+
Example
|
25
|
+
=======
|
26
|
+
|
27
|
+
Suppose you have an application with a contact form that can be reached via a footer link on every page. After submitting the form, the user should be redirected to the page he was before clicking on the "contact form" link.
|
28
|
+
|
29
|
+
for the footer link to the contact form:
|
30
|
+
|
31
|
+
<%= link_to 'Contact us!', new_contact_path(referrer_param => current_path) %>
|
32
|
+
|
33
|
+
In the 'new contact' view:
|
34
|
+
|
35
|
+
<%= form for ... do |f| %>
|
36
|
+
<%= hidden_referrer_input_tag %>
|
37
|
+
<!-- ... -->
|
38
|
+
<% end %>
|
39
|
+
|
40
|
+
and finally, in the 'create' action of your ContactsController:
|
41
|
+
|
42
|
+
def create
|
43
|
+
# ...
|
44
|
+
redirect_to back_or_default
|
45
|
+
end
|
46
|
+
|
47
|
+
for more detailled examples, see the Rdoc documentation.
|
48
|
+
|
49
|
+
|
50
|
+
Copyright (c) 2010 Willem van Kerkhof <wvk@consolving.de>, released under the MIT license
|
data/init.rb
ADDED
File without changes
|
data/lib/redirectr.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
module Redirectr
|
2
|
+
REFERRER_PARAM_NAME = :referrer
|
3
|
+
|
4
|
+
module ControllerMethods
|
5
|
+
def self.included(base)
|
6
|
+
base.send :include, InstanceMethods
|
7
|
+
base.send :extend, ClassMethods
|
8
|
+
|
9
|
+
base.send :helper_method,
|
10
|
+
:current_path,
|
11
|
+
:referrer_or_current_path,
|
12
|
+
:back_or_default,
|
13
|
+
:referrer_path,
|
14
|
+
:referrer_param
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
# Used to set a different param name where the referrer path should be stored.
|
19
|
+
# Default is :referrer, a sensible alternative would be :r or so.
|
20
|
+
# Example:
|
21
|
+
#
|
22
|
+
# class MyController
|
23
|
+
# use_referrer_param_name :r
|
24
|
+
# # ...
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
def use_referrer_param_name(new_name)
|
28
|
+
const_set Redirectr::REFERRER_PARAM_NAME, new_name.to_sym
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module InstanceMethods
|
33
|
+
|
34
|
+
# Return the name of the parameter used to pass the referrer path.
|
35
|
+
# Use this instead of the real param name in creating your own links
|
36
|
+
# to allow easily changing the name later
|
37
|
+
# Example:
|
38
|
+
#
|
39
|
+
# <%= link_to my_messages_path :filter_by => 'date', referrer_param => current_path %>
|
40
|
+
#
|
41
|
+
def referrer_param
|
42
|
+
Redirectr::REFERRER_PARAM_NAME
|
43
|
+
end
|
44
|
+
|
45
|
+
# Return the path of the current request.
|
46
|
+
# note that this path does NOT include any query parameters nor the hostname,
|
47
|
+
# thus allowing you to navigate within the application only. This may be changed
|
48
|
+
# in the future. If you need a different behaviour now, just overwrite this method
|
49
|
+
# in your controller.
|
50
|
+
# Example:
|
51
|
+
#
|
52
|
+
# <%= link_to my_messages_path referrer_param => current_path %>
|
53
|
+
#
|
54
|
+
def current_path
|
55
|
+
# maybe we want to use request.env['REQUEST_URI'] in the future...?
|
56
|
+
request.env['PATH_INFO']
|
57
|
+
end
|
58
|
+
|
59
|
+
# Return the referrer or the current path, it the former is not set.
|
60
|
+
# Useful in cases where there might be a redirect path that has to be
|
61
|
+
# taken note of but in case it is not present, the current path will be
|
62
|
+
# redirected back to.
|
63
|
+
# Example:
|
64
|
+
#
|
65
|
+
# <%= link_to my_messages_path referrer_param => referrer_or_current_path %>
|
66
|
+
#
|
67
|
+
def referrer_or_current_path
|
68
|
+
referrer_path.blank? ? current_path : referrer_path
|
69
|
+
end
|
70
|
+
|
71
|
+
# Used in back links, referrer based redirection after actions etc.
|
72
|
+
# Accepts a default redirect path in case no param[referrer_param]
|
73
|
+
# is set, default being root_path.
|
74
|
+
# Can and should be overwritten in namespace specific controllers
|
75
|
+
# to set a sensible default if no referrer is given.
|
76
|
+
# Example:
|
77
|
+
#
|
78
|
+
# class MyController
|
79
|
+
# def create
|
80
|
+
# @my = My.create(...)
|
81
|
+
# redirect_to back_or_default(my_path)
|
82
|
+
# end
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# The above example will redirect to the referrer_path if it is defined, otherwise
|
86
|
+
# it will redirect to the my_path
|
87
|
+
#
|
88
|
+
# Example:
|
89
|
+
#
|
90
|
+
# class MyController
|
91
|
+
# def create
|
92
|
+
# @my = My.create(...)
|
93
|
+
# redirect_to back_or_default
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# The above example will redirect to the referrer_path if it is defined, otherwise
|
98
|
+
# it will redirect to the root_path of the application.
|
99
|
+
def back_or_default(default = nil)
|
100
|
+
unless referrer_path.blank?
|
101
|
+
referrer_path
|
102
|
+
else
|
103
|
+
root_path
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Convenience method for params[referrer_param]
|
108
|
+
def referrer_path
|
109
|
+
params[referrer_param]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
module Helpers
|
116
|
+
# Create a link back to the path specified in the referrer-param.
|
117
|
+
# title can be either a text string or anything else like an image.
|
118
|
+
# Remember to call #html_safe on the title argument if it contains
|
119
|
+
# HTML and you are using Rails 3.
|
120
|
+
def link_to_back(title, options = {})
|
121
|
+
link_to title, back_or_default, options
|
122
|
+
end
|
123
|
+
|
124
|
+
# Create a hidden input field containing the referrer or current path.
|
125
|
+
# Handy for use in forms that are called with a referrer param which
|
126
|
+
# has to be passed on and respected by the form processing action.
|
127
|
+
def hidden_referrer_input_tag
|
128
|
+
hidden_field_tag :referrer, referrer_or_current_path
|
129
|
+
end
|
130
|
+
|
131
|
+
end # module Helpers
|
132
|
+
end # module Redirectr
|
133
|
+
|
134
|
+
ActionController::Base.send :include, Redirectr::ControllerMethods
|
135
|
+
ActionView::Helpers.send :include, Redirectr::Helpers
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redirectr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Willem van Kerkhof
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-16 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Provides Rails-helper methods for referrer-style backlinks and setting redirect URLs after form submussion
|
23
|
+
email: wvk@consolving.de
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- README
|
32
|
+
- init.rb
|
33
|
+
- lib/redirectr.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/wvk/redirectr
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
hash: 3
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.7
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Provides Rails-helper methods for referrer-style backlinks
|
68
|
+
test_files: []
|
69
|
+
|