Skip to content

Commit 18e6ddc

Browse files
committed
Added the ability to return to the last visited page after signing in. This required a monkey patch to Warden as Warden wipes out the entire session after signing out. Closes fredwu#5.
1 parent 5bcf674 commit 18e6ddc

File tree

6 files changed

+70
-2
lines changed

6 files changed

+70
-2
lines changed

app/controllers/application_controller.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ class ApplicationController < ActionController::Base
55

66
layout proc { |controller| controller.request.xhr? ? nil : 'application' }
77

8-
before_filter :require_login, :unless => :devise_controller?
9-
before_filter :ensure_ownership, :unless => :devise_controller?, :except => [:index, :show]
8+
before_filter :require_login, :unless => :devise_controller?
9+
before_filter :ensure_ownership, :unless => :devise_controller?, :except => [:index, :show]
10+
after_filter :record_last_visted_page, :unless => :devise_controller?, :if => Proc.new { request.get? && !request.xhr? }
1011

1112
protected
1213

@@ -24,6 +25,10 @@ def ensure_ownership
2425
end
2526
end
2627

28+
def record_last_visted_page
29+
session[:user_return_to] = request.path
30+
end
31+
2732
def deny_access
2833
render :text => t('system.access_denied'), :status => 403
2934
end

app/views/layouts/application.html.slim

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ body
2525
h1= link_to t('site.name'), root_path
2626
nav
2727
ul
28+
li= link_to_current t('label.home'), root_path
2829
li= link_to_current t('label.startups'), startups_path
2930
li= link_to_current t('label.entrepreneurs'), entrepreneurs_path
3031
li= link_to_current t('label.investors'), investors_path

config/application.rb

+1
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ class Application < Rails::Application
5858
end
5959

6060
require Rails.root + 'lib/monkey_patches/array'
61+
require Rails.root + 'lib/monkey_patches/warden'

config/locales/en.yml

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ en:
5555
time_ago: '%{time} ago'
5656
time_ago_by: '%{time} ago by'
5757
label:
58+
home: Home
5859
loading: Loading
5960
create: Create
6061
update: Update

lib/monkey_patches/warden.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Warden::Mixins::Common
2+
# overwrites session reset so that we could keep things like 'last_visited_page'
3+
def reset_session!
4+
nil
5+
end
6+
end

spec/requests/session_spec.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'spec_helper'
2+
3+
describe "session" do
4+
let(:user) do
5+
u = User.make!
6+
u.confirm!
7+
u
8+
end
9+
10+
let(:startup) { Startup.make! }
11+
12+
before do
13+
startup.attach_user(user, :member, 'Founder')
14+
15+
post user_session_path, :user => {
16+
:login => user.email,
17+
:password => 'password'
18+
}
19+
20+
get startups_path
21+
end
22+
23+
it "records the last visited page for a normal GET request" do
24+
get investors_path
25+
26+
session[:user_return_to].should == investors_path
27+
end
28+
29+
it "doesn't record the last visited page for a normal POST request" do
30+
post my_private_messages_path, :users => 1, :message => { :content => 'hello' }, :format => :json
31+
32+
session[:user_return_to].should == startups_path
33+
end
34+
35+
it "doesn't record the last visited page for an AJAX GET request" do
36+
get startup_path(startup)
37+
xhr :get, startup_profile_details_path(startup)
38+
39+
session[:user_return_to].should == startup_path(startup)
40+
end
41+
42+
it "doesn't record the last visited page for an AJAX POST request" do
43+
get startup_path(startup)
44+
xhr :post, "#{startup_path(startup)}/update_user?role_identifier=member&uid=1", :member_title => 'CEO'
45+
46+
session[:user_return_to].should == startup_path(startup)
47+
end
48+
49+
it "doesn't record the last visited page for a user session request" do
50+
get new_user_session_path
51+
52+
session[:user_return_to].should == startups_path
53+
end
54+
end

0 commit comments

Comments
 (0)