Sha256: 0d77478276f01a4164e6d0872ab76e1113ef67edcb9eb53b9cbf94f34b76b535

Contents?: true

Size: 936 Bytes

Versions: 4

Compression:

Stored size: 936 Bytes

Contents

module Rolify
  def has_role(role, resource = nil)
    role = Role.find_or_create_by_name_and_resource_type_and_resource_id( :name => role, 
                                                                          :resource_type => (resource.class.name if resource), 
                                                                          :resource_id => (resource.id if resource))
    self.roles << role if !roles.include?(role)
  end
  
  def has_role?(role, resource = nil)
    global_role_query = "((name = ?) AND (resource_type IS NULL) AND (resource_id IS NULL))"
    if resource
      self.roles.where("#{global_role_query} OR ((name = ?) AND (resource_type = ?) AND (resource_id = ?))", 
                        role, role, resource.class.name, resource.id).size > 0
    else
      self.roles.where(global_role_query, role).size > 0
    end
  end
  
  def roles_name
    self.roles.select(:name).map { |r| r.name }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rolify-0.2.1 lib/rolify/role.rb
rolify-0.2.0 lib/rolify/role.rb
rolify-0.1.1 lib/rolify/role.rb
rolify-0.1.0 lib/rolify/role.rb