Class: Rolify::Adapter::ResourceAdapter

Inherits:
ResourceAdapterBase show all
Defined in:
lib/rolify/adapters/mongoid/resource_adapter.rb,
lib/rolify/adapters/active_record/resource_adapter.rb

Instance Method Summary collapse

Methods inherited from Base

create, #initialize, #relation_types_for, #role_class, #role_table, #user_class

Constructor Details

This class inherits a constructor from Rolify::Adapter::Base

Instance Method Details

#all_except(resource, excluded_obj) ⇒ Object



42
43
44
# File 'lib/rolify/adapters/mongoid/resource_adapter.rb', line 42

def all_except(resource, excluded_obj)
  resource.not_in(_id: excluded_obj.to_a)
end

#applied_roles(relation, children) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/rolify/adapters/mongoid/resource_adapter.rb', line 34

def applied_roles(relation, children)
  if children
    relation.role_class.where(:resource_type.in => self.relation_types_for(relation), :resource_id => nil)
  else
    relation.role_class.where(:resource_type => relation.to_s, :resource_id => nil)
  end
end

#find_roles(role_name, relation, user) ⇒ Object



7
8
9
10
11
12
# File 'lib/rolify/adapters/mongoid/resource_adapter.rb', line 7

def find_roles(role_name, relation, user)
  roles = user && (user != :any) ? user.roles : self.role_class
  roles = roles.where(:resource_type.in => self.relation_types_for(relation))
  roles = roles.where(:name => role_name.to_s) if role_name && (role_name != :any)
  roles
end

#in(relation, user, role_names) ⇒ Object



27
28
29
30
31
32
# File 'lib/rolify/adapters/mongoid/resource_adapter.rb', line 27

def in(resources, user, role_names)
  roles = user.roles.where(:name.in => Array(role_names))
  return [] if resources.empty? || roles.empty?
  resources.delete_if { |resource| (resource.applied_roles & roles).empty? }
  resources
end

#resources_find(roles_table, relation, role_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rolify/adapters/mongoid/resource_adapter.rb', line 14

def resources_find(roles_table, relation, role_name)
  roles = roles_table.classify.constantize.where(:name.in => Array(role_name), :resource_type.in => self.relation_types_for(relation))
  resources = []
  roles.each do |role|
    if role.resource_id.nil?
      resources += relation.all
    else
      resources << role.resource
    end
  end
  resources.compact.uniq
end