Module: CoreModels::Models::Concerns::UserConcerns

Included in:
User
Defined in:
lib/core_models/models/concerns/user_concerns.rb

Instance Method Summary collapse

Instance Method Details

#add_group=(group_name) ⇒ Object



46
47
48
# File 'lib/core_models/models/concerns/user_concerns.rb', line 46

def add_group=(group_name)
  self.groups << self.groups.klass.find_by(group_name: group_name)
end

#add_group_membership(group, role) ⇒ Object



33
34
35
# File 'lib/core_models/models/concerns/user_concerns.rb', line 33

def add_group_membership(group, role)
  self.group_memberships.create!(group_id: group.id, role_id: role.id)
end

#get_role_from_group_membership(group_id) ⇒ Object



37
38
39
# File 'lib/core_models/models/concerns/user_concerns.rb', line 37

def get_role_from_group_membership(group_id)
  self.group_memberships.find_by!(group_id: group_id).role
end

#groupObject



54
55
56
# File 'lib/core_models/models/concerns/user_concerns.rb', line 54

def group
  self.groups.first.try(:group_name)
end

#group_namesObject



58
59
60
61
62
63
64
65
# File 'lib/core_models/models/concerns/user_concerns.rb', line 58

def group_names
  associated_groups = Array.new
  self.groups.each do |group|
    associated_groups.push(group.group_name)
  end

  associated_groups
end

#has_group?(name) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
# File 'lib/core_models/models/concerns/user_concerns.rb', line 5

def has_group?(name)
  self.groups.each do |g|
    if g.group_name == name
      return true
    end
  end

  return false
end

#has_role?(name) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
# File 'lib/core_models/models/concerns/user_concerns.rb', line 15

def has_role?(name)
  self.roles.each do |r|
    if r.role_name == name
      return true
    end
  end

  return false
end

#has_role_for_group?(name, group_name) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/core_models/models/concerns/user_concerns.rb', line 25

def has_role_for_group?(name, group_name)
  if self.groups.where(group_name: group_name).any?
    if self.groups.find(group_name.parameterize).roles.where(role_name: name).any?
      self.has_role?(name)
    end
  end
end

#permissionsObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/core_models/models/concerns/user_concerns.rb', line 88

def permissions
  permission_array = Array.new

  self.roles.each do |role|
    role.permissions.each do |permission|
      permission_array.push(permission.permission_name)
    end
  end

  permission_array
end

#remove_group_membership(group_name) ⇒ Object



41
42
43
44
# File 'lib/core_models/models/concerns/user_concerns.rb', line 41

def remove_group_membership(group_name)
  group = self.groups.klass.find_by!(group_name: group_name)
  self.group_memberships.where(group_id: group.id).delete_all
end

#roleObject



50
51
52
# File 'lib/core_models/models/concerns/user_concerns.rb', line 50

def role
  self.roles.first.try(:role_name)
end

#role_namesObject



67
68
69
70
71
72
73
74
# File 'lib/core_models/models/concerns/user_concerns.rb', line 67

def role_names
  associated_roles = Array.new
  self.roles.each do |role|
    associated_roles.push(role.role_name)
  end

  associated_roles
end

#uniq_permissionsObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/core_models/models/concerns/user_concerns.rb', line 76

def uniq_permissions
  permission_array = Array.new

  self.roles.each do |role|
    role.permissions.each do |permission|
      permission_array.push(permission.permission_name)
    end
  end

  permission_array.uniq
end