Explicit set login/signup return_to path

This commit is contained in:
Rei
2014-03-04 14:13:17 +08:00
parent afdfd56c6e
commit f9dcc4edd1
7 changed files with 31 additions and 16 deletions

View File

@@ -13,8 +13,7 @@ class ApplicationController < ActionController::Base
def login_required
unless login?
store_location
redirect_to login_url
redirect_to login_url(return_to: (request.fullpath if request.get?))
end
end
@@ -76,17 +75,12 @@ class ApplicationController < ActionController::Base
@current_user ||= User.find_by_access_token(params[:access_token]) if params[:access_token]
end
def store_location(path = nil)
session[:return_to] = path || request.fullpath
def store_location(path)
session[:return_to] = path
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
def redirect_referrer_or_default(default)
redirect_to(request.referrer || default)
redirect_to(session.delete(:return_to) || default)
end
def forget_me

View File

@@ -1,6 +1,6 @@
class SessionsController < ApplicationController
def new
store_location params[:return_to] if params[:return_to].present?
store_location params[:return_to]
end
def create

View File

@@ -2,7 +2,7 @@ class UsersController < ApplicationController
before_filter :no_login_required, only: [:new, :create]
def new
store_location params[:return_to] if params[:return_to].present?
store_location params[:return_to]
@user = User.new
end

View File

@@ -0,0 +1,10 @@
module ApplicationHelper
def return_to_path(path)
case path
when '/', /^\/login/, /^\/signup/
nil
else
path
end
end
end

View File

@@ -54,10 +54,10 @@ html
= t '.logout'
- else
li
a.item href=signup_path(return_to: request.original_url)
a.item href=signup_path(return_to: params[:return_to] || return_to_path(request.fullpath))
= t '.sign_up'
li
a.item href=login_path(return_to: request.original_url)
a.item href=login_path(return_to: params[:return_to] || return_to_path(request.fullpath))
= t '.sign_in'
#navbar.collapse.navbar-collapse
ul.nav.navbar-nav

View File

@@ -75,12 +75,12 @@
- else
= t '.your_account_had_been_locked'
- else
a href=signup_path(return_to: request.original_url)
a href=signup_path(return_to: request.fullpath)
= t '.sign_up'
'
= t '.or'
'
a href=login_path(return_to: request.original_url)
a href=login_path(return_to: request.fullpath)
= t '.login'
'
= t '.to_comment'

View File

@@ -0,0 +1,11 @@
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
test "return_to should return right path" do
assert_equal nil, return_to_path('/')
assert_equal '/?page=2', return_to_path('/?page=2')
assert_equal nil, return_to_path('/login')
assert_equal nil, return_to_path('/signup')
assert_equal '/topics', return_to_path('/topics')
end
end