Skip to content

Instantly share code, notes, and snippets.

@rvanlieshout
Created March 8, 2011 13:27
Show Gist options
  • Save rvanlieshout/860254 to your computer and use it in GitHub Desktop.
Save rvanlieshout/860254 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
composed_of :roles, :class_name => 'Array',
:mapping => %w(roles to_json),
:constructor => Proc.new { |roles| roles.nil? ? [] : JSON.parse(roles) },
:converter => Proc.new { |roles| roles.class == Array ? roles : [] }
ROLES = [
'admin',
'manager',
'branch_manager',
'planner',
'employee',
'customer'
].freeze
# Define a method for each role: user.admin?, user.manager?, ...
User.class_eval do
User::ROLES.each do |role|
define_method "#{role}?" do
self.roles.include?(role)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment