Skip to content

Instantly share code, notes, and snippets.

@mrowl
Created January 24, 2011 18:00
Show Gist options
  • Save mrowl/793616 to your computer and use it in GitHub Desktop.
Save mrowl/793616 to your computer and use it in GitHub Desktop.
Add functions to a rails activerecord model dynamically for testing role membership
class User < ActiveRecord::Base
#available roles for users
ROLES = ['uber', 'admin', 'mod']
#generates methods like user.admin? for testing whether the user belongs to
#the proper role
ROLES.each do |role_name|
self.send(:define_method, "#{role_name}?".to_sym) { self.role == role_name }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment