我有一个rails应用程序,需要在用户登录后根据某些条件将用户重定向到不同的页面(使用Devise& OmniAuth)。这个逻辑可以像这样伪编码:
if the user is an admin
if no url was specified before login (original_uri)
- redirect to admin panel
else
- redirect to original_uri
else
if the user filled up his profile data
if no url was specified before login
- redirect to user's home page
else
if original_uri is allowed (not restricted to that user)
- redirect to original_uri
else
- redirect to user's home page
else
- redirect to profile page
或作为rspec集成示例:
describe "complex routing" do
context "user is an admin" do
let(:user) { create(:admin) }
context "an original URL was specified before login" do
it "redirects to the original URL"
end
context "no original URL was specified" do
it "redirects to the admin panel"
end
end
context "user is not an admin" do
let(:user) { create(:user, :completed_profile => false) }
context "with complete profile info" do
before(:each) { user.completed_profile = true }
context "an original URL was specified before login" do
it "redirects to original URL if not restricted"
it "redirects to home page if URL is restricted"
end
context "no original URL was specified" do
it "redirects to home page"
end
end
context "with incomplete profile" do
it "redirects to profile page"
end
end
end
可以看出,这变得相当复杂并且不是很明显(或易于测试)。另外,这种坐在before_filter :decide_routing
作为方法调用的想法让我感到畏缩。
什么是一个很好的方法来抽象这个并使这个更清洁,可测试和更简单的管理(如果需要添加或更改更多的逻辑)?
任何想法都会很棒 - 谢谢。
答案 0 :(得分:1)
这样做吗?
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!
protected
def after_sign_in_path_for(resource)
# if user is not active, redirect him so he completes registration
if resource.active?
super
else
# complete_the_effing_registration_path
end
end
end