1
我有一個Rails應用程序,它需要在登錄後根據一些條件將用戶重定向到不同的頁面(使用Devise & OmniAuth)。這種邏輯可能是僞編碼是這樣的:在Rails中處理複雜的登錄後重定向邏輯
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
這個想法讓我感到畏縮。
什麼樣的方法可以將這種抽象方法抽象出來,並使這種更清潔,可測試和更簡單的管理(以防需要添加或更改更多邏輯)?
任何想法都會很棒 - 謝謝。