Skip to content

Commit 95282ba

Browse files
authored
Merge pull request #2734 from nicolasfranck/preload_perms
refactor Perm like TokenPermissionType
2 parents 67944c1 + e356805 commit 95282ba

File tree

2 files changed

+136
-202
lines changed

2 files changed

+136
-202
lines changed

app/models/perm.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212

1313
class Perm < ApplicationRecord
1414

15+
class << self
16+
17+
private
18+
19+
def lazy_load(name)
20+
Rails.cache
21+
.fetch("Perm.find_by_name(#{name})", expires_in: 5.seconds, cache_nils: false) do
22+
Perm.find_by_name(name)
23+
end
24+
.freeze
25+
end
26+
27+
end
28+
1529
# ================
1630
# = Associations =
1731
# ================
@@ -29,41 +43,40 @@ class Perm < ApplicationRecord
2943
# = Class methods =
3044
# =================
3145

32-
# TODO: see the constants setup in token_permission_type.rb and refactor
3346
def self.add_orgs
34-
Perm.find_by(name: "add_organisations")
47+
lazy_load("add_organisations")
3548
end
3649

3750
def self.change_affiliation
38-
Perm.find_by(name: "change_org_affiliation")
51+
lazy_load("change_org_affiliation")
3952
end
4053

4154
def self.grant_permissions
42-
Perm.find_by(name: "grant_permissions")
55+
lazy_load("grant_permissions")
4356
end
4457

4558
def self.modify_templates
46-
Perm.find_by(name: "modify_templates")
59+
lazy_load("modify_templates")
4760
end
4861

4962
def self.modify_guidance
50-
Perm.find_by(name: "modify_guidance")
63+
lazy_load("modify_guidance")
5164
end
5265

5366
def self.use_api
54-
Perm.find_by(name: "use_api")
67+
lazy_load("use_api")
5568
end
5669

5770
def self.change_org_details
58-
Perm.find_by(name: "change_org_details")
71+
lazy_load("change_org_details")
5972
end
6073

6174
def self.grant_api
62-
Perm.find_by(name: "grant_api_to_orgs")
75+
lazy_load("grant_api_to_orgs")
6376
end
6477

6578
def self.review_plans
66-
Perm.find_by(name: "review_org_plans")
79+
lazy_load("review_org_plans")
6780
end
6881

6982
end

0 commit comments

Comments
 (0)