Module: Roleable::UserRole

Included in:
UserRole
Defined in:
lib/roleable/user_role.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) extended(base)



3
4
5
6
7
8
9
# File 'lib/roleable/user_role.rb', line 3

def self.extended(base)
  base.belongs_to :user
  base.belongs_to :role
  base.belongs_to :resource, :polymorphic => true
  
  base.attr_accessible :role, :user, :resource
end

Instance Method Details

- (Object) create_if_unique!(attributes)

Create a record with the given attributes if there are no records that already have those attributes.

Returns the record if it was saved, otherwise nil.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/roleable/user_role.rb', line 36

def create_if_unique!(attributes)
  user_role = new(attributes)
  
  record_attributes = user_role.attributes.reject do |k, v| 
    %w(id updated_at created_at).include?(k)
  end
  
  if !exists?(record_attributes) && user_role.save
    user_role
  else
    nil
  end
end

- (Object) with_resource(resource)



15
16
17
# File 'lib/roleable/user_role.rb', line 15

def with_resource(resource)
  where(:resource_id => resource && resource.id, :resource_type => resource && resource_type(resource))
end

- (Object) with_resource_class(resource_class)



28
29
30
# File 'lib/roleable/user_role.rb', line 28

def with_resource_class(resource_class)
  where(:resource_type => resource_type_from_class(resource_class))
end

- (Object) with_role(role)



24
25
26
# File 'lib/roleable/user_role.rb', line 24

def with_role(role)
  where(:role_id => role && role.id)
end

- (Object) with_role_name(role_name)



19
20
21
22
# File 'lib/roleable/user_role.rb', line 19

def with_role_name(role_name)
  role = ::Role.find_by_name(role_name)
  with_role(role)
end

- (Object) with_user(user)



11
12
13
# File 'lib/roleable/user_role.rb', line 11

def with_user(user)
  where(:user_id => user && user.id)
end