Skip to content

Instantly share code, notes, and snippets.

@tofumatt
Created July 15, 2010 14:11
Show Gist options
  • Save tofumatt/476984 to your computer and use it in GitHub Desktop.
Save tofumatt/476984 to your computer and use it in GitHub Desktop.
# Meta-method to setup before_filters on controller
# actions requiring various authorization levels.
def self.must_be(role, options={})
define_method "#{role}_required" do
unless authorized?(role)
if logged_in?
redirect_to request.referer || root_url, :alert => "You must be an #{role} to access that page."
else
redirect_to login_path, :alert => "You must be logged in to access that page."
end
end
end
protected "#{role}_required"
before_filter "#{role}_required".to_sym, options
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment