Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 42005e1a92ea7c3557b28ad2cb7ac6789530d64d @tylerhunt committed Sep 30, 2008
Showing with 132 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +60 −0 README
  3. +22 −0 Rakefile
  4. +3 −0 init.rb
  5. +19 −0 lib/comeback.rb
  6. +8 −0 test/comeback_test.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Tyler Hunt
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
60 README
@@ -0,0 +1,60 @@
+Comeback
+========
+
+Provides a mechanism for storing URLs to return to at a later time. Allows the
+current URL, a custom URL, or the referer to be stored.
+
+When a referer is stored, it is checked against the current page URL, and if
+they're the same, then the referer won't be set and the default path will be
+used instead.
+
+The session key :return_to is used to store the URL.
+
+
+Example
+=======
+
+In your controller, you first call #store_referer to save the referer to the
+current page, and then to access it again, call #return_or_redirect_to and pass
+it the same arguments you would pass to #redirect_to as the default in case a
+referer has not been set:
+
+ class MyController < ApplicationController
+ def first_action
+ store_referer
+ end
+
+ def second_action
+ return_or_redirect_to root_path
+ end
+ end
+
+You can also store the referer using a before filter:
+
+ class MyController < ApplicationController
+ before_filter :store_referer, :only => :first_action
+
+ def first_action
+ end
+ end
+
+The current page URL can also be stored instead of the referer:
+
+ class MyController < ApplicationController
+ def first_action
+ store_location
+ redirect_to login_path
+ end
+ end
+
+Explicit URLs may also be stored:
+
+ class MyController < ApplicationController
+ def first_action
+ store_location dashbaord_path
+ redirect_to login_path
+ end
+ end
+
+
+Copyright (c) 2008 Tyler Hunt, released under the MIT license
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the comeback plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the comeback plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Comeback'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,3 @@
+require 'comeback'
+
+ActionController::Base.send(:include, Comeback)
@@ -0,0 +1,19 @@
+module Comeback
+ def clear_location
+ session[:return_to] = nil
+ end
+
+ def store_location(url=nil)
+ session[:return_to] ||= url ? url : request.url
+ end
+
+ def store_referer
+ referer = request.env['HTTP_REFERER']
+ session[:return_to] ||= referer unless referer == request.url
+ end
+
+ def return_or_redirect_to(*args)
+ session[:return_to] ? redirect_to(session[:return_to]) : redirect_to(*args)
+ session[:return_to] = nil
+ end
+end
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class ComebackTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit 42005e1

Please sign in to comment.