-
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.
- Loading branch information
Ivan Vergés
authored and
Ivan Vergés
committed
Dec 2, 2024
1 parent
0a5852b
commit bfc6474
Showing
5 changed files
with
26 additions
and
37 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
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 |
---|---|---|
@@ -1,21 +1 @@ | ||
# 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, &block) | ||
return yield unless defined?(request) && request.env["decidim.current_organization"]&.id | ||
|
||
OrganizationMemoizer.memoize("#{request.env["decidim.current_organization"].id}-#{key}", &block) | ||
end | ||
end | ||
end | ||
end |
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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module DecidimAwesome | ||
module RequestMemoizer | ||
# memoize a piece of code in the global request instead of the helper instance (helpers are initialized for each view) | ||
def memoize(key) | ||
return yield unless defined?(request) && request.env["decidim.current_organization"]&.id | ||
|
||
request.env["decidim_awesome.#{key}"] ||= yield | ||
end | ||
end | ||
end | ||
end |