module ActionDispatch::Routing
  class Mapper
    def multi_site_scope(&block)
      scope ':multi_site', :constraints => MultiSiteConstraints.new do
        block.call
      end
    end
  end
end

class MultiSiteConstraints
  def matches?(request)
    Site.exists?(:id => request.path_parameters[:multi_site])
  end
end

module ActionDispatch::Routing
  class Mapper
    def multi_site_scope(&block)
      scope ':multi_site', :constraints => MultiSiteConstraints.new do
        block.call
      end
    end
  end
end

class MultiSiteConstraints
  def matches?(request)
    #TODO: This is not a definitive solution. Devise principal class is configurable,
    #so we can't be sure if the the routes will start with users/...
    #Devise settings must be read to know the principal's name
    return true if defined?(Devise) and [ 'devise/sessions',
                                          'users/registrations',
                                          'users/passwords' ].include?(request.path_parameters[:controller])

    Site.exists?(:url => request.path_parameters[:multi_site])
  end
end

Revert previous change. Handling Devise special case
Revert previous change.
Handling Devise special case
