Module: Roleable::UserRole

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



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

#create_if_unique!(attributes) ⇒ Object

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

#with_resource(resource) ⇒ Object



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

#with_resource_class(resource_class) ⇒ Object



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

#with_role(role) ⇒ Object



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

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

#with_role_name(role_name) ⇒ Object



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

#with_user(user) ⇒ Object



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

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