role_fu 0.3.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -1
- data/README.md +42 -1
- data/gemfiles/rails_7.2.gemfile.lock +117 -123
- data/gemfiles/rails_8.0.gemfile.lock +181 -187
- data/gemfiles/rails_8.1.gemfile.lock +183 -189
- data/lib/generators/role_fu/templates/role_fu.rb +7 -0
- data/lib/role_fu/configuration.rb +13 -0
- data/lib/role_fu/resourceable.rb +31 -0
- data/lib/role_fu/roleable.rb +19 -0
- data/lib/role_fu/version.rb +1 -1
- metadata +3 -3
data/lib/role_fu/resourceable.rb
CHANGED
|
@@ -9,6 +9,11 @@ module RoleFu
|
|
|
9
9
|
as: :resource,
|
|
10
10
|
dependent: :destroy,
|
|
11
11
|
class_name: RoleFu.configuration.role_class_name
|
|
12
|
+
|
|
13
|
+
has_many :users,
|
|
14
|
+
through: :roles,
|
|
15
|
+
class_name: RoleFu.configuration.user_class_name,
|
|
16
|
+
source: :users
|
|
12
17
|
end
|
|
13
18
|
|
|
14
19
|
module ClassMethods
|
|
@@ -48,12 +53,15 @@ module RoleFu
|
|
|
48
53
|
alias_method "users_with_any_#{singular}", :users_with_any_role
|
|
49
54
|
alias_method "users_with_all_#{plural}", :users_with_all_roles
|
|
50
55
|
alias_method "users_with_#{plural}", :users_with_roles
|
|
56
|
+
alias_method "users_grouped_by_#{singular}", :users_grouped_by_role
|
|
51
57
|
alias_method "available_#{plural}", :available_roles
|
|
58
|
+
alias_method "applied_#{plural}", :applied_roles
|
|
52
59
|
alias_method "has_#{singular}?", :has_role?
|
|
53
60
|
alias_method "count_users_with_#{singular}", :count_users_with_role
|
|
54
61
|
alias_method "user_has_#{singular}?", :user_has_role?
|
|
55
62
|
alias_method "add_#{singular}_to_user", :add_role_to_user
|
|
56
63
|
alias_method "remove_#{singular}_from_user", :remove_role_from_user
|
|
64
|
+
alias_method "destroy_#{singular}", :destroy_role
|
|
57
65
|
end
|
|
58
66
|
end
|
|
59
67
|
|
|
@@ -61,6 +69,29 @@ module RoleFu
|
|
|
61
69
|
roles
|
|
62
70
|
end
|
|
63
71
|
|
|
72
|
+
def destroy_role(role_name)
|
|
73
|
+
roles.where(name: role_name.to_s).destroy_all
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def users_grouped_by_role
|
|
77
|
+
RoleFu.role_class.table_name
|
|
78
|
+
|
|
79
|
+
# Efficiently load roles and their associated users
|
|
80
|
+
# 1. Get all roles for this resource
|
|
81
|
+
# 2. Preload users for these roles
|
|
82
|
+
|
|
83
|
+
role_scope = roles.includes(:users)
|
|
84
|
+
|
|
85
|
+
results = {}
|
|
86
|
+
role_scope.each do |role|
|
|
87
|
+
results[role.name] ||= []
|
|
88
|
+
results[role.name].concat(role.users)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Deduplicate users if a user has the same role multiple times (unlikely with distinct but safer)
|
|
92
|
+
results.transform_values(&:uniq)
|
|
93
|
+
end
|
|
94
|
+
|
|
64
95
|
def users_with_role(role_name)
|
|
65
96
|
role_table = RoleFu.role_class.table_name
|
|
66
97
|
RoleFu.user_class.joins(:roles)
|
data/lib/role_fu/roleable.rb
CHANGED
|
@@ -209,8 +209,27 @@ module RoleFu
|
|
|
209
209
|
query.distinct
|
|
210
210
|
end
|
|
211
211
|
|
|
212
|
+
def method_missing(method_name, *args, &block)
|
|
213
|
+
if (role_name = parse_dynamic_role_name(method_name))
|
|
214
|
+
has_role?(role_name, *args)
|
|
215
|
+
else
|
|
216
|
+
super
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def respond_to_missing?(method_name, include_private = false)
|
|
221
|
+
parse_dynamic_role_name(method_name) || super
|
|
222
|
+
end
|
|
223
|
+
|
|
212
224
|
private
|
|
213
225
|
|
|
226
|
+
def parse_dynamic_role_name(method_name)
|
|
227
|
+
return nil unless (regex = RoleFu.configuration.dynamic_shortcuts_regex)
|
|
228
|
+
|
|
229
|
+
match = method_name.to_s.match(regex)
|
|
230
|
+
match ? match[:role] : nil
|
|
231
|
+
end
|
|
232
|
+
|
|
214
233
|
def filter_expired(relation)
|
|
215
234
|
return relation unless RoleFu.role_assignment_class.column_names.include?("expires_at")
|
|
216
235
|
|
data/lib/role_fu/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: role_fu
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alexey Poimtsev
|
|
@@ -171,14 +171,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
171
171
|
requirements:
|
|
172
172
|
- - ">="
|
|
173
173
|
- !ruby/object:Gem::Version
|
|
174
|
-
version: 3.
|
|
174
|
+
version: 3.3.0
|
|
175
175
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
176
|
requirements:
|
|
177
177
|
- - ">="
|
|
178
178
|
- !ruby/object:Gem::Version
|
|
179
179
|
version: '0'
|
|
180
180
|
requirements: []
|
|
181
|
-
rubygems_version: 4.0.
|
|
181
|
+
rubygems_version: 4.0.16
|
|
182
182
|
specification_version: 4
|
|
183
183
|
summary: A modern role management gem for Rails, replacing rolify.
|
|
184
184
|
test_files: []
|