Permalink
Browse files

Update documentation and minor code cleanups.

  • Loading branch information...
1 parent 6196641 commit ebb5de24d9f148a8b9a2d18c4bfbb9ff1c12608e @FooBarWidget committed Sep 3, 2008
Showing with 41 additions and 51 deletions.
  1. +15 −11 lib/auto_redirection.rb
  2. +26 −40 lib/auto_redirection/controller_extensions.rb
View
@@ -178,8 +178,8 @@
# depending on where the visitor came from. In other words, we want to be able
# to *nest* redirection information.
#
-# Right now, CommentsController will always redirect to '/comments/id)' after
-# having created a comments. So we change it a little:
+# Luckily, auto_redirections takes care of this for you automatically. We modify
+# CommentsController as we normally would:
#
# class CommentsController < ApplicationController
# def create
@@ -198,24 +198,28 @@
# end
#
# Now, CommentsController will redirect using whatever auto-redirection information
-# it has received. If no auto-redirection information is given (i.e.
-# +attempt_auto_redirect+ returns false) then it returns the visitor to
-# '/comments/(id)'.
+# it has received. When LoginController is done, it will redirect the visitor
+# back to CommentsController, and will pass to CommentsController whatever
+# redirection information that CommentsController originally received. This
+# allows CommentsController to redirect the visitor back to either '/books/(id)'
+# or '/books/(review)'.
#
#
# === Saving POST auto-redirection information without a session
#
-# The flash is not available if sessions are disabled. In that case, you have to pass
-# auto-redirection information via a GET parameter, like this:
+# The flash is not available if sessions are disabled. In that case, you cannot
+# use +save_redirection_information+. Instead, you have to pass redirection
+# information via a GET parameter, like this:
#
-# redirect_to('/login/login_form', :auto_redirect_to => current_request)
+# redirect_to('/login/login_form',
+# :_redirection_information => redirection_parameter_for_current_request)
#
-# The +current_request+ method returns auto-redirection information for the
-# current request.
+# The +redirection_parameter_for_current_request+ method returns auto-redirection
+# information for the current request.
#
# == Security
#
-# Auto-redirection information is encrypted, so it cannot be read or tampered with
+# Redirection information is encrypted, so it cannot be read or tampered with
# by third parties. Be sure to set a custom encryption key instead of leaving
# the key at the default value. For example, put this in your environment.rb:
#
@@ -22,50 +22,23 @@ module AutoRedirection
module ControllerExtensions
protected
- # Saves redirection information into the flash, so that the next
- # controller action may use this information.
+ # Saves the current controller's name, action, HTTP method and parameters
+ # as redirection information into the flash. This allows the next controller
+ # action to use this information to redirect back to this controller action,
+ # with the same HTTP method and parameters.
#
- # +location+ may either be +:here+, or a String containing an URL.
- def save_redirection_information(location = :here)
- case location
- when :here
- parameters = params
- current_redirection_info = get_redirection_information
- if current_redirection_info
- parameters = params.merge(:_redirection_information =>
- current_redirection_info.marshal)
- else
- parameters = params
- end
-
- redirection_info_to_pass = ControllerRedirectionInformation.new(
- controller_path, action_name, parameters, request.method)
- flash[:_redirection_information] = redirection_info_to_pass.marshal(true, false)
- logger.debug("Auto-Redirection: saving redirection information " <<
- "for: #{controller_path}/#{action_name} (#{request.method})")
- when String
- info = UrlRedirectionInformation.new(location)
- flash[:_redirection_information] = info.marshal(true, false)
- logger.debug("Auto-Redirection: saving redirection information " <<
- "for: #{location}")
- else
- raise ArgumentError, "Unknown location #{location.inspect}."
- end
+ # The redirection information that the current controller action has
+ # received is also saved, so that nested redirections is possible.
+ def save_redirection_information
+ flash[:_redirection_information] =
+ redirection_information_for_current_request.marshal(true, false)
+ logger.debug("Auto-Redirection: saving redirection information " <<
+ "for: #{controller_path}/#{action_name} (#{request.method})")
end
- # Returns auto-redirection information for the current request.
- def current_request
- @_current_request ||= begin
- info = {
- 'controller' => controller_path,
- 'action' => action_name,
- 'method' => request.method,
- 'params' => params
- }
- Encryption.encrypt(Marshal.dump(info))
- end
+ def redirection_parameter_for_current_request
end
-
+
# The current request may contain redirection information.
# If auto-redirection information is given, then this method will redirect
# the HTTP client to that location (by calling +redirect_to+) and return true.
@@ -148,6 +121,19 @@ def get_redirection_information
end
return @_redirection_information
end
+
+ def redirection_information_for_current_request
+ parameters = params
+ current_redirection_info = get_redirection_information
+ if current_redirection_info
+ parameters = params.merge(:_redirection_information =>
+ current_redirection_info.marshal)
+ else
+ parameters = params
+ end
+ return ControllerRedirectionInformation.new(
+ controller_path, action_name, parameters, request.method)
+ end
end
end # module AutoRedirections

0 comments on commit ebb5de2

Please sign in to comment.