module Kibali::SubjectExtensions

Public Instance Methods

get_role( role_name=nil ) click to toggle source

get_role – returns a role obj for subject; else nil EXCEPTION: EmptyRolesException if role_objects collection is empty


# File lib/kibali/subject_extensions.rb, line 35
def get_role( role_name=nil )

  raise Kibali::EmptyRoles  if role_objects.empty?

  if role_name.nil?
     role_objects.first
  else
     role_objects.where( :name => role_name.to_s ).first
  end

end
has_role!(role_name) click to toggle source

has_role! – forces subject to have the given role


# File lib/kibali/subject_extensions.rb, line 17
def has_role!(role_name)
  role = _auth_role_class.where( :name => role_name.to_s ).   # acts as the find part
         first_or_create( :name => role_name.to_s )           # acts as the create part
  role_objects << role unless self.role_objects.member?(role)
  role
end
has_role?(role_name) click to toggle source

has_role? – returns true if subject has the given role


# File lib/kibali/subject_extensions.rb, line 10
def has_role?(role_name)
  !get_role( role_name ).nil?
end
remove_role!(role_name) click to toggle source

remove_role! – foreces subject to NOT have the given role


# File lib/kibali/subject_extensions.rb, line 27
def remove_role!(role_name)
  role_objects.delete( get_role( role_name ) )
end

Protected Instance Methods

_auth_role_assoc() click to toggle source

_auth_role_assoc – returns the habtm symbol for the array of subject.roles


# File lib/kibali/subject_extensions.rb, line 67
def _auth_role_assoc
  self.class._auth_role_assoc_name
end
_auth_role_class() click to toggle source

_auth_role_class – retuns the Klass for the Role model


# File lib/kibali/subject_extensions.rb, line 60
def _auth_role_class
  self.class._auth_role_class_name.constantize
end
role_objects() click to toggle source

role_objects – returns the habtm array of roles for the subject


# File lib/kibali/subject_extensions.rb, line 74
def role_objects
  send(self._auth_role_assoc)
end