Skip to content

Commit

Permalink
memoize in request
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vergés authored and Ivan Vergés committed Dec 2, 2024
1 parent e20805e commit 93b0e45
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/decidim/decidim_awesome/awesome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module DecidimAwesome
include ActiveSupport::Configurable

autoload :AwesomeHelpers, "decidim/decidim_awesome/awesome_helpers"
autoload :OrganizationMemoizer, "decidim/decidim_awesome/organization_memoizer"
autoload :RequestMemoizer, "decidim/decidim_awesome/request_memoizer"
autoload :Config, "decidim/decidim_awesome/config"
autoload :SystemChecker, "decidim/decidim_awesome/system_checker"
autoload :ContextAnalyzers, "decidim/decidim_awesome/context_analyzers"
Expand Down
19 changes: 8 additions & 11 deletions lib/decidim/decidim_awesome/awesome_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ module Decidim
# add a global helper with awesome configuration
module DecidimAwesome
module AwesomeHelpers
include OrganizationMemoizer
include RequestMemoizer

# Returns the normalized config for an Organization and the current url
def awesome_config_instance
return @awesome_config_instance if @awesome_config_instance

# if already created in the middleware, reuse it as it might have additional constraints
@awesome_config_instance = request.env["decidim_awesome.current_config"]
unless @awesome_config_instance.is_a? Config
@awesome_config_instance = Config.new request.env["decidim.current_organization"]
@awesome_config_instance.context_from_request request
memoize("current_config") do
config = Config.new(request.env["decidim.current_organization"])
config.context_from_request(request)
config
end
@awesome_config_instance
end

def awesome_config
Expand Down Expand Up @@ -100,8 +96,9 @@ def awesome_voting_manifest_for(component)
# Retrives all the "admins_available_authorizations" for the user along with other possible authorizations
# returns an instance of Decidim::DecidimAwesome::Authorizator
def awesome_authorizations_for(user)
@awesome_authorizations_for ||= {}
@awesome_authorizations_for[user.id] ||= Authorizator.new(user, awesome_config[:admins_available_authorizations])
memoize("awesome_authorizations_for_#{user.id}") do
Authorizator.new(user, awesome_config[:admins_available_authorizations])
end
end

def version_prefix
Expand Down
8 changes: 3 additions & 5 deletions lib/decidim/decidim_awesome/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ class Engine < ::Rails::Engine
# https://edgeguides.rubyonrails.org/engines.html#overriding-models-and-controllers
# overrides
config.to_prepare do
# activate Decidim LayoutHelper for the overriden views
ActiveSupport.on_load :action_controller do
helper Decidim::DecidimAwesome::AwesomeHelpers
end
# # Also for cells
# Include additional helpers globally
ActiveSupport.on_load(:action_view) { include Decidim::DecidimAwesome::AwesomeHelpers }
# Also for cells
Decidim::ViewModel.include(Decidim::DecidimAwesome::AwesomeHelpers)

# Override EtiquetteValidator
Expand Down
21 changes: 0 additions & 21 deletions lib/decidim/decidim_awesome/organization_memoizer.rb

This file was deleted.

14 changes: 14 additions & 0 deletions lib/decidim/decidim_awesome/request_memoizer.rb
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

0 comments on commit 93b0e45

Please sign in to comment.