Because Rails routes run sequentially, you can simulate a conditional login as follows:
# config/routes.rb match '/u' => 'controller#action', :constraints => BlacklistDomain.new match '/u' => 'user#signin'
The first line checks if the restriction conditions are fulfilled (i.e. if the request comes from a blacklisted domain). If the restriction is met, the request is sent to controller#action (respectively, replace accordingly).
If the restriction conditions are not met (i.e., the request is not blacklisted), the request will be redirected to user#signing .
Since this conditional logic is processed efficiently in your routes, your restriction code can be simplified:
# blacklist_domain.rb class BlacklistDomain BANNED_DOMAINS = ['domain1.com', 'domain2.com'] def matches?(request) if BANNED_DOMAINS.include?(request.host) return true end end end
source share