我有一个 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
作为方法调用的想法让我感到畏缩。
什么是一种抽象的好方法,使这个更清晰、可测试并且在未来更易于管理(以防需要添加或更改更多逻辑)?
任何想法都会很棒-谢谢。