-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add an organization memoizer for helpers
- Loading branch information
Ivan Vergés
authored and
Ivan Vergés
committed
Dec 2, 2024
1 parent
0def5a0
commit c7866f1
Showing
7 changed files
with
60 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
app/controllers/concerns/decidim/decidim_awesome/needs_awesome_config.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
# add a global helper with awesome configuration | ||
module DecidimAwesome | ||
module OrganizationMemoizer | ||
def self.memoize(key) | ||
@memoized ||= {} | ||
@memoized[key] ||= yield | ||
end | ||
|
||
# memoize a piece of code in the class instead of the instance (helper are initialized for each view) | ||
# only works if request.env["decidim.current_organization"] is defined | ||
def memoize(key, &) | ||
return yield unless request.env["decidim.current_organization"]&.id | ||
|
||
OrganizationMemoizer.memoize("#{request.env["decidim.current_organization"].id}-#{key}", &) | ||
end | ||
end | ||
end | ||
end |