Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update documentation and minor code cleanups.

  • Loading branch information...
commit ebb5de24d9f148a8b9a2d18c4bfbb9ff1c12608e 1 parent 6196641
Hongli Lai authored
26  lib/auto_redirection.rb
@@ -178,8 +178,8 @@
178 178
 # depending on where the visitor came from. In other words, we want to be able
179 179
 # to *nest* redirection information.
180 180
 #
181  
-# Right now, CommentsController will always redirect to '/comments/id)' after
182  
-# having created a comments. So we change it a little:
  181
+# Luckily, auto_redirections takes care of this for you automatically. We modify
  182
+# CommentsController as we normally would:
183 183
 #
184 184
 #   class CommentsController < ApplicationController
185 185
 #      def create
@@ -198,24 +198,28 @@
198 198
 #   end
199 199
 #
200 200
 # Now, CommentsController will redirect using whatever auto-redirection information
201  
-# it has received. If no auto-redirection information is given (i.e.
202  
-# +attempt_auto_redirect+ returns false) then it returns the visitor to
203  
-# '/comments/(id)'.
  201
+# it has received. When LoginController is done, it will redirect the visitor
  202
+# back to CommentsController, and will pass to CommentsController whatever
  203
+# redirection information that CommentsController originally received. This
  204
+# allows CommentsController to redirect the visitor back to either '/books/(id)'
  205
+# or '/books/(review)'.
204 206
 #
205 207
 #
206 208
 # === Saving POST auto-redirection information without a session
207 209
 #
208  
-# The flash is not available if sessions are disabled. In that case, you have to pass
209  
-# auto-redirection information via a GET parameter, like this:
  210
+# The flash is not available if sessions are disabled. In that case, you cannot
  211
+# use +save_redirection_information+. Instead, you have to pass redirection
  212
+# information via a GET parameter, like this:
210 213
 #
211  
-#   redirect_to('/login/login_form', :auto_redirect_to => current_request)
  214
+#   redirect_to('/login/login_form',
  215
+#      :_redirection_information => redirection_parameter_for_current_request)
212 216
 #
213  
-# The +current_request+ method returns auto-redirection information for the
214  
-# current request.
  217
+# The +redirection_parameter_for_current_request+ method returns auto-redirection
  218
+# information for the current request.
215 219
 #
216 220
 # == Security
217 221
 #
218  
-# Auto-redirection information is encrypted, so it cannot be read or tampered with
  222
+# Redirection information is encrypted, so it cannot be read or tampered with
219 223
 # by third parties. Be sure to set a custom encryption key instead of leaving
220 224
 # the key at the default value. For example, put this in your environment.rb:
221 225
 #
66  lib/auto_redirection/controller_extensions.rb
@@ -22,50 +22,23 @@ module AutoRedirection
22 22
 
23 23
 module ControllerExtensions
24 24
 protected
25  
-	# Saves redirection information into the flash, so that the next
26  
-	# controller action may use this information.
  25
+	# Saves the current controller's name, action, HTTP method and parameters
  26
+	# as redirection information into the flash. This allows the next controller
  27
+	# action to use this information to redirect back to this controller action,
  28
+	# with the same HTTP method and parameters.
27 29
 	#
28  
-	# +location+ may either be +:here+, or a String containing an URL.
29  
-	def save_redirection_information(location = :here)
30  
-		case location
31  
-		when :here
32  
-			parameters = params
33  
-			current_redirection_info = get_redirection_information
34  
-			if current_redirection_info
35  
-				parameters = params.merge(:_redirection_information =>
36  
-					current_redirection_info.marshal)
37  
-			else
38  
-				parameters = params
39  
-			end
40  
-			
41  
-			redirection_info_to_pass = ControllerRedirectionInformation.new(
42  
-				controller_path, action_name, parameters, request.method)
43  
-			flash[:_redirection_information] = redirection_info_to_pass.marshal(true, false)
44  
-			logger.debug("Auto-Redirection: saving redirection information " <<
45  
-				"for: #{controller_path}/#{action_name} (#{request.method})")
46  
-		when String
47  
-			info = UrlRedirectionInformation.new(location)
48  
-			flash[:_redirection_information] = info.marshal(true, false)
49  
-			logger.debug("Auto-Redirection: saving redirection information " <<
50  
-				"for: #{location}")
51  
-		else
52  
-			raise ArgumentError, "Unknown location #{location.inspect}."
53  
-		end
  30
+	# The redirection information that the current controller action has
  31
+	# received is also saved, so that nested redirections is possible.
  32
+	def save_redirection_information
  33
+		flash[:_redirection_information] =
  34
+			redirection_information_for_current_request.marshal(true, false)
  35
+		logger.debug("Auto-Redirection: saving redirection information " <<
  36
+			"for: #{controller_path}/#{action_name} (#{request.method})")
54 37
 	end
55 38
 	
56  
-	# Returns auto-redirection information for the current request.
57  
-	def current_request
58  
-		@_current_request ||= begin
59  
-			info = {
60  
-				'controller' => controller_path,
61  
-				'action' => action_name,
62  
-				'method' => request.method,
63  
-				'params' => params
64  
-			}
65  
-			Encryption.encrypt(Marshal.dump(info))
66  
-		end
  39
+	def redirection_parameter_for_current_request
67 40
 	end
68  
-
  41
+	
69 42
 	# The current request may contain redirection information.
70 43
 	# If auto-redirection information is given, then this method will redirect
71 44
 	# the HTTP client to that location (by calling +redirect_to+) and return true.
@@ -148,6 +121,19 @@ def get_redirection_information
148 121
 		end
149 122
 		return @_redirection_information
150 123
 	end
  124
+	
  125
+	def redirection_information_for_current_request
  126
+		parameters = params
  127
+		current_redirection_info = get_redirection_information
  128
+		if current_redirection_info
  129
+			parameters = params.merge(:_redirection_information =>
  130
+				current_redirection_info.marshal)
  131
+		else
  132
+			parameters = params
  133
+		end
  134
+		return ControllerRedirectionInformation.new(
  135
+			controller_path, action_name, parameters, request.method)
  136
+	end
151 137
 end
152 138
 
153 139
 end # module AutoRedirections

0 notes on commit ebb5de2

Please sign in to comment.
Something went wrong with that request. Please try again.