effective_roles 2.2.0 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77423c072ab8659b9e632cebcd9fe939743abac4d0b374f12b3e144a70bcdfe1
4
- data.tar.gz: 929bbf885819e6303f7a09ffe7c58988f4607403903b3dfdc3c141f175714eee
3
+ metadata.gz: 663715e96a52fd2472bcc606272284e1292c6b33b95914bfb9f8349cd367fef4
4
+ data.tar.gz: 9fc246e383b8b416cf755043f4f88bc5a764bfc7964a5c4736bef09b385516e3
5
5
  SHA512:
6
- metadata.gz: a6917b11179747ff215fd926a595594de592b6881a3ca74c7b699fb52a1a0b482259ff2edddd0cd02d2eaebf8d265ebea506128641d32fb7f296567bad2466bf
7
- data.tar.gz: 90a7d053382163c74cf8df9dee3d03b84239f4f39bd199d1b0ada04f124fd88b8ff4d7222dd8e3fce9f0eb37544c2bf386ab0a0d8c3a38ee9384b6b04b861907
6
+ metadata.gz: 2a07058e19d7e5b088cac749bfb2512625ea505c052559902940da20aa40a53ba47febdf64897fa5400cb883a929ac90b8d16d94a7b25ee2c0c874536a89c835
7
+ data.tar.gz: c48b105bf52531ee64cad811eebab4bdb078f8559435d89bc32fa74e27a5fdf7392c108710b9f220fa1e7030750564a5a770ea30286aea264a8429edd67ea04f
@@ -53,6 +53,8 @@ module ActsAsRoleRestricted
53
53
  end
54
54
 
55
55
  module ClassMethods
56
+ def acts_as_role_restricted?; true; end
57
+
56
58
  # Call with for_role(:admin) or for_role(@user.roles) or for_role([:admin, :member]) or for_role(:admin, :member, ...)
57
59
 
58
60
  # Returns all records which have been assigned any of the the given roles
@@ -87,17 +89,42 @@ module ActsAsRoleRestricted
87
89
  end
88
90
  end
89
91
 
92
+ def roles
93
+ EffectiveRoles.roles_for(roles_mask)
94
+ end
95
+
90
96
  def roles=(roles)
91
97
  self.roles_mask = EffectiveRoles.roles_mask_for(roles)
92
98
  end
93
99
 
94
- def roles
95
- EffectiveRoles.roles_for(roles_mask)
100
+ def add_role(role)
101
+ raise('expected role to be a symbol') unless role.kind_of?(Symbol)
102
+ raise('unknown role') unless EffectiveRoles.roles_mask_for(role) > 0
103
+
104
+ assign_attributes(roles: roles | [role])
105
+ end
106
+
107
+ def add_role!(role)
108
+ add_role(role); save!
109
+ end
110
+
111
+ def remove_role(role)
112
+ raise('expected role to be a symbol') unless role.kind_of?(Symbol)
113
+ raise('unknown role') unless EffectiveRoles.roles_mask_for(role) > 0
114
+
115
+ assign_attributes(roles: roles - [role])
116
+ end
117
+
118
+ def remove_role!(role)
119
+ remove_role(role); save!
96
120
  end
97
121
 
98
122
  # if user.is? :admin
99
123
  def is?(role)
100
- roles.include?(role.try(:to_sym))
124
+ raise('expected role to be a symbol') unless role.kind_of?(Symbol)
125
+ raise('unknown role') unless EffectiveRoles.roles_mask_for(role) > 0
126
+
127
+ roles.include?(role)
101
128
  end
102
129
 
103
130
  # if user.is_any?(:admin, :editor)
@@ -4,7 +4,7 @@ module EffectiveRoles
4
4
 
5
5
  # Include acts_as_addressable concern and allow any ActiveRecord object to call it
6
6
  initializer 'effective_roles.active_record' do |app|
7
- ActiveSupport.on_load :active_record do
7
+ app.config.to_prepare do
8
8
  ActiveRecord::Base.extend(ActsAsRoleRestricted::Base)
9
9
  end
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveRoles
2
- VERSION = '2.2.0'.freeze
2
+ VERSION = '2.3.1'.freeze
3
3
  end
@@ -39,7 +39,7 @@ module EffectiveRoles
39
39
  roles_for(roles).map { |r| 2 ** config.roles.index(r) }.sum
40
40
  end
41
41
 
42
- def self.roles_collection(resource, current_user = nil, only: nil, except: nil, multiple: nil)
42
+ def self.roles_collection(resource, current_user = nil, only: nil, except: nil, multiple: nil, skip_disabled: nil)
43
43
  if assignable_roles.present?
44
44
  raise('expected object to respond to is_role_restricted?') unless resource.respond_to?(:is_role_restricted?)
45
45
  raise('expected current_user to respond to is_role_restricted?') if current_user && !current_user.respond_to?(:is_role_restricted?)
@@ -49,15 +49,19 @@ module EffectiveRoles
49
49
  except = Array(except).compact
50
50
  multiple = resource.acts_as_role_restricted_options[:multiple] if multiple.nil?
51
51
  assignable = assignable_roles_collection(resource, current_user, multiple: multiple)
52
+ skip_disabled = assignable_roles.kind_of?(Hash) if skip_disabled.nil?
52
53
 
53
54
  roles.map do |role|
54
55
  next if only.present? && !only.include?(role)
55
56
  next if except.present? && except.include?(role)
56
57
 
58
+ disabled = !assignable.include?(role)
59
+ next if disabled && skip_disabled
60
+
57
61
  [
58
62
  "#{role}<p class='help-block text-muted'>#{role_description(role, resource)}</p>".html_safe,
59
63
  role,
60
- ({:disabled => :disabled} unless assignable.include?(role))
64
+ ({:disabled => :disabled} if disabled)
61
65
  ]
62
66
  end.compact
63
67
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_roles
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-21 00:00:00.000000000 Z
11
+ date: 2022-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails