Module: Roleable::AppliedRole

Included in:
AppliedRole
Defined in:
lib/roleable/applied_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/applied_role.rb', line 3

def self.extended(base)
  base.belongs_to :subject, :class_name => Roleable.configuration.subject_class_name
  base.belongs_to :role
  base.belongs_to :resource, :polymorphic => true

  base.attr_accessible :role, :subject_id, :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.



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

def create_if_unique!(attributes)
  applied_role = new(attributes)

  record_attributes = applied_role.attributes.reject do |k, v|
    %w(id updated_at created_at).include?(k)
  end

  if !exists?(record_attributes) && applied_role.save
    applied_role
  else
    nil
  end
end

#with_resource(resource) ⇒ Object



15
16
17
# File 'lib/roleable/applied_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



29
30
31
# File 'lib/roleable/applied_role.rb', line 29

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

#with_role_name(role_name) ⇒ Object



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

def with_role_name(role_name)
  roles = ::Role.find_all_by_name(role_name)
  with_roles(roles)
end

#with_roles(roles) ⇒ Object



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

def with_roles(roles)
  role_ids = roles.map { |r| r.id }
  where(:role_id => role_ids)
end

#with_subject(subject) ⇒ Object



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

def with_subject(subject)
  where(:subject_id => subject && subject.id)
end