Skip to content

Instantly share code, notes, and snippets.

@shekibobo
Created July 12, 2012 03:33
Show Gist options
  • Save shekibobo/3095514 to your computer and use it in GitHub Desktop.
Save shekibobo/3095514 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
rolify :after_add => :event_added_mew_role #, :role_cname => 'Role'
accepts_nested_attributes_for :roles
attr_accessible :roles_attributes
# Include default devise modules. Others available are:
# :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :token_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
before_save :ensure_authentication_token
# attr_accessible :title, :body
# Connect this model with the user group
has_and_belongs_to_many :business_groups
attr_accessible :business_group_ids
def managed_applications
if self.has_role? :sys_admin
Application.all
else
self.roles.where(:name => :app_manager).map { |role| role.resource if role.resource_type == "Application" }
end
end
def roles_attributes=(roles_attrs)
roles_attrs.each do |key, role_attrs|
if (rid = role_attrs[:resource_id].presence) && (rtype = role_attrs[:resource_type].presence)
resource = Kernel.const_get(rtype).find(rid)
end
self.add_role role_attrs[:name], resource
end
end
def event_added_mew_role(role)
puts '======================================================================='
logger.info role
puts role
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment