Skip to content

Instantly share code, notes, and snippets.

@deepakkumarnd
Created January 30, 2014 13:11
Show Gist options
  • Save deepakkumarnd/8708022 to your computer and use it in GitHub Desktop.
Save deepakkumarnd/8708022 to your computer and use it in GitHub Desktop.
#lib/role_constraint.rb
class RoleConstraint
def initialize(*roles)
@roles = roles
end
def matches?(request)
@roles.include? request.env['warden'].user.try(:role)
end
end
#config/routes.rb
root :to => 'admin#index', :constraints => RoleConstraint.new(:admin) #matches this route when the current user is an admin
root :to => 'sites#index', :constraints => RoleConstraint.new(:user) #matches this route when the current user is an user
root :to => 'home#index' #matches this route when the above two matches don't pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment