From 0def5a0161cbee8b204cef8b85bdeb4f292ead7e Mon Sep 17 00:00:00 2001 From: PokeCode Bot <109160875+pokecodebot@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:43:38 +0100 Subject: [PATCH 1/5] New translations en.yml (French) (#354) --- config/locales/fr.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 82da352ae..e7f15ea06 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -249,6 +249,7 @@ fr: %{warning} Ce texte peut être personnalisé avec une traduction personnalisée en utilisant la clé: %{key} + scoped_admin_styles: Soyez conscient que cacher des éléments pourrait rendre l'interface administrateur inutilisable. Agissez avec prudence. scoped_admin_styles_variables: 'Vous pouvez utiliser les variables CSS suivantes pour les couleurs personnalisées de l''organisation :' scoped_admins: Promouvoir les utilisateurs réguliers en administrateurs qui ne peuvent administrer que certaines parties de la plateforme (utilisez l'éditeur de restrictions pour cela). Notez que les utilisateurs qui ont déjà les permissions d'administration habituelles seront ignorés. scoped_styles: Créer un CSS personnalisé qui s'applique uniquement à certaines parties du site web public (front-end) (utilisez l'éditeur de restrictions pour cela) @@ -409,6 +410,7 @@ fr: proposals: Hacks pour les propositions styles: Styles personnalisés surveys: Enquêtes et formulaires + verifications: Vérifications menu_hacks: create: error: Erreur lors de la création de l'élément de menu ! %{error} @@ -447,6 +449,11 @@ fr: error: Erreur lors de la mise à jour de l'élément de menu ! %{error} success: Élément de menu mis à jour avec succès url_exists: La même URL est déjà configurée, veuillez la modifier au lieu de la créer à nouveau. + officializations: + authorizations: Autorisations + verification_modal: + authorizations_for: Autorisations pour %{user} + close: Fermer proposal_custom_fields: menu: title: Champs publiques @@ -528,6 +535,13 @@ fr: view_meeting: Voir la rencontre view_proposal: Voir la proposition proposal_private_custom_fields_disclosure: Cette information ne sera pas publiée + required_authorizations: + index: + cancel: Je vais le faire ultérieurement, %{link}. + granted_title: Vérifications accordées + missing_title: Pas encore vérifié + resume_title: Vérifications en attente + title: Vérifiez votre identité validators: too_much_caps: Utilise trop de majuscule (plus de %{percent} du texte) voting: From c7866f1eff3ad081395a67c61cafcdcca2a1a355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Verg=C3=A9s?= Date: Mon, 2 Dec 2024 14:57:04 +0100 Subject: [PATCH 2/5] add an organization memoizer for helpers --- Gemfile.lock | 2 +- .../proposal_l_cell_override.rb | 5 +-- .../decidim_awesome/needs_awesome_config.rb | 2 - lib/decidim/decidim_awesome/awesome.rb | 2 + .../decidim_awesome/awesome_helpers.rb | 44 ++++++++++++++----- lib/decidim/decidim_awesome/engine.rb | 6 +-- .../decidim_awesome/organization_memoizer.rb | 21 +++++++++ 7 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 lib/decidim/decidim_awesome/organization_memoizer.rb diff --git a/Gemfile.lock b/Gemfile.lock index 2fbf84c6d..548d72b9c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -749,7 +749,7 @@ GEM uber (0.1.0) unicode-display_width (2.6.0) uniform_notifier (1.16.0) - uri (0.13.1) + uri (1.0.2) valid_email2 (4.0.6) activemodel (>= 3.2) mail (~> 2.5) diff --git a/app/cells/concerns/decidim/decidim_awesome/proposal_l_cell_override.rb b/app/cells/concerns/decidim/decidim_awesome/proposal_l_cell_override.rb index 6566a4c1c..3dfcd5291 100644 --- a/app/cells/concerns/decidim/decidim_awesome/proposal_l_cell_override.rb +++ b/app/cells/concerns/decidim/decidim_awesome/proposal_l_cell_override.rb @@ -11,12 +11,11 @@ module ProposalLCellOverride alias_method :decidim_original_cache_hash, :cache_hash def metadata_cell - awesome_voting_manifest_for(resource&.component)&.proposal_metadata_cell.presence || "decidim/proposals/proposal_metadata" + @metadata_cell ||= awesome_voting_manifest_for(resource&.component)&.proposal_metadata_cell.presence || "decidim/proposals/proposal_metadata" end def cache_hash - extra_hash = model.extra_fields&.reload&.vote_weight_totals - "#{decidim_original_cache_hash}#{Decidim.cache_key_separator}#{extra_hash}" + @cache_hash ||= "#{decidim_original_cache_hash}#{Decidim.cache_key_separator}#{model.extra_fields&.reload&.vote_weight_totals}" end end end diff --git a/app/controllers/concerns/decidim/decidim_awesome/needs_awesome_config.rb b/app/controllers/concerns/decidim/decidim_awesome/needs_awesome_config.rb index d61f2177b..59bd0df2e 100644 --- a/app/controllers/concerns/decidim/decidim_awesome/needs_awesome_config.rb +++ b/app/controllers/concerns/decidim/decidim_awesome/needs_awesome_config.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "decidim/decidim_awesome/awesome_helpers" - module Decidim module DecidimAwesome module NeedsAwesomeConfig diff --git a/lib/decidim/decidim_awesome/awesome.rb b/lib/decidim/decidim_awesome/awesome.rb index 224df298f..5d66fdacd 100644 --- a/lib/decidim/decidim_awesome/awesome.rb +++ b/lib/decidim/decidim_awesome/awesome.rb @@ -4,6 +4,8 @@ module Decidim module DecidimAwesome include ActiveSupport::Configurable + autoload :AwesomeHelpers, "decidim/decidim_awesome/awesome_helpers" + autoload :OrganizationMemoizer, "decidim/decidim_awesome/organization_memoizer" autoload :Config, "decidim/decidim_awesome/config" autoload :SystemChecker, "decidim/decidim_awesome/system_checker" autoload :ContextAnalyzers, "decidim/decidim_awesome/context_analyzers" diff --git a/lib/decidim/decidim_awesome/awesome_helpers.rb b/lib/decidim/decidim_awesome/awesome_helpers.rb index 74936e3fd..bb885244b 100644 --- a/lib/decidim/decidim_awesome/awesome_helpers.rb +++ b/lib/decidim/decidim_awesome/awesome_helpers.rb @@ -6,6 +6,8 @@ module Decidim # add a global helper with awesome configuration module DecidimAwesome module AwesomeHelpers + include OrganizationMemoizer + # Returns the normalized config for an Organization and the current url def awesome_config_instance return @awesome_config_instance if @awesome_config_instance @@ -20,11 +22,15 @@ def awesome_config_instance end def awesome_config - @awesome_config ||= awesome_config_instance.config + memoize("awesome_config") do + awesome_config_instance.config + end end def javascript_config_vars - awesome_config.slice(:allow_images_in_proposals, :allow_images_in_editors, :allow_videos_in_editors, :auto_save_forms).to_json.html_safe + memoize("javascript_config_vars") do + awesome_config.slice(:allow_images_in_proposals, :allow_images_in_editors, :allow_videos_in_editors, :auto_save_forms).to_json.html_safe + end end def show_public_intergram? @@ -35,11 +41,15 @@ def show_public_intergram? end def unfiltered_awesome_config - @unfiltered_awesome_config ||= awesome_config_instance.unfiltered_config + memoize("unfiltered_awesome_config") do + awesome_config_instance.unfiltered_config + end end def organization_awesome_config - @organization_awesome_config ||= awesome_config_instance.organization_config + memoize("organization_awesome_config") do + awesome_config_instance.organization_config + end end def awesome_version @@ -48,33 +58,43 @@ def awesome_version # Collects all CSS that is applied in the current URL context def awesome_scoped_styles - @awesome_scoped_styles ||= awesome_config_instance.collect_sub_configs_values("scoped_style") + memoize("awesome_scoped_styles") do + awesome_config_instance.collect_sub_configs_values("scoped_style") + end end # Collects all CSS that is applied in the current URL context def awesome_scoped_admin_styles - @awesome_scoped_admin_styles ||= awesome_config_instance.collect_sub_configs_values("scoped_admin_style") + memoize("awesome_scoped_admin_styles") do + awesome_config_instance.collect_sub_configs_values("scoped_admin_style") + end end # Collects all proposal custom fields that is applied in the current URL context def awesome_scoped_admins - @awesome_scoped_admins ||= awesome_config_instance.collect_sub_configs_values("scoped_admin") + memoize("awesome_scoped_admins") do + awesome_config_instance.collect_sub_configs_values("scoped_admin") + end end # Collects all proposal custom fields that is applied in the current URL context def awesome_proposal_custom_fields - @awesome_proposal_custom_fields ||= awesome_config_instance.collect_sub_configs_values("proposal_custom_field") + memoize("awesome_proposal_custom_fields") do + awesome_config_instance.collect_sub_configs_values("proposal_custom_field") + end end def awesome_proposal_private_custom_fields - @awesome_proposal_private_custom_fields ||= awesome_config_instance.collect_sub_configs_values("proposal_private_custom_field") + memoize("awesome_proposal_private_custom_fields") do + awesome_config_instance.collect_sub_configs_values("proposal_private_custom_field") + end end # this will check if the current component has been configured to use a custom voting manifest def awesome_voting_manifest_for(component) - return nil unless component.settings.respond_to? :awesome_voting_manifest - - DecidimAwesome.voting_registry.find(component.settings.awesome_voting_manifest) + memoize("awesome_voting_manifest_for_#{component.id}") do + DecidimAwesome.voting_registry.find(component.settings.try(:awesome_voting_manifest)) + end end # Retrives all the "admins_available_authorizations" for the user along with other possible authorizations diff --git a/lib/decidim/decidim_awesome/engine.rb b/lib/decidim/decidim_awesome/engine.rb index d5346bee2..d7339a1be 100644 --- a/lib/decidim/decidim_awesome/engine.rb +++ b/lib/decidim/decidim_awesome/engine.rb @@ -25,11 +25,9 @@ class Engine < ::Rails::Engine config.to_prepare do # activate Decidim LayoutHelper for the overriden views ActiveSupport.on_load :action_controller do - helper Decidim::LayoutHelper if respond_to?(:helper) + helper Decidim::DecidimAwesome::AwesomeHelpers end - # Include additional helpers globally - ActiveSupport.on_load(:action_view) { include Decidim::DecidimAwesome::AwesomeHelpers } - # Also for cells + # # Also for cells Decidim::ViewModel.include(Decidim::DecidimAwesome::AwesomeHelpers) # Override EtiquetteValidator diff --git a/lib/decidim/decidim_awesome/organization_memoizer.rb b/lib/decidim/decidim_awesome/organization_memoizer.rb new file mode 100644 index 000000000..c238df106 --- /dev/null +++ b/lib/decidim/decidim_awesome/organization_memoizer.rb @@ -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 From e20805e0964a1ba4ed65dacfc80f371df72175f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Verg=C3=A9s?= Date: Mon, 2 Dec 2024 20:39:05 +0100 Subject: [PATCH 3/5] ensure request exits --- lib/decidim/decidim_awesome/organization_memoizer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/decidim/decidim_awesome/organization_memoizer.rb b/lib/decidim/decidim_awesome/organization_memoizer.rb index c238df106..f823c5a6c 100644 --- a/lib/decidim/decidim_awesome/organization_memoizer.rb +++ b/lib/decidim/decidim_awesome/organization_memoizer.rb @@ -12,7 +12,7 @@ def self.memoize(key) # 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 + return yield unless defined?(request) && request.env["decidim.current_organization"]&.id OrganizationMemoizer.memoize("#{request.env["decidim.current_organization"].id}-#{key}", &) end From 93b0e455934caa9b4a717c66ddb530f99656c844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Verg=C3=A9s?= Date: Mon, 2 Dec 2024 23:28:46 +0100 Subject: [PATCH 4/5] memoize in request --- lib/decidim/decidim_awesome/awesome.rb | 2 +- .../decidim_awesome/awesome_helpers.rb | 19 +++++++---------- lib/decidim/decidim_awesome/engine.rb | 8 +++---- .../decidim_awesome/organization_memoizer.rb | 21 ------------------- .../decidim_awesome/request_memoizer.rb | 14 +++++++++++++ 5 files changed, 26 insertions(+), 38 deletions(-) delete mode 100644 lib/decidim/decidim_awesome/organization_memoizer.rb create mode 100644 lib/decidim/decidim_awesome/request_memoizer.rb diff --git a/lib/decidim/decidim_awesome/awesome.rb b/lib/decidim/decidim_awesome/awesome.rb index 5d66fdacd..8f9860849 100644 --- a/lib/decidim/decidim_awesome/awesome.rb +++ b/lib/decidim/decidim_awesome/awesome.rb @@ -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" diff --git a/lib/decidim/decidim_awesome/awesome_helpers.rb b/lib/decidim/decidim_awesome/awesome_helpers.rb index bb885244b..3ff1ca7db 100644 --- a/lib/decidim/decidim_awesome/awesome_helpers.rb +++ b/lib/decidim/decidim_awesome/awesome_helpers.rb @@ -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 @@ -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 diff --git a/lib/decidim/decidim_awesome/engine.rb b/lib/decidim/decidim_awesome/engine.rb index d7339a1be..3da43646c 100644 --- a/lib/decidim/decidim_awesome/engine.rb +++ b/lib/decidim/decidim_awesome/engine.rb @@ -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 diff --git a/lib/decidim/decidim_awesome/organization_memoizer.rb b/lib/decidim/decidim_awesome/organization_memoizer.rb deleted file mode 100644 index f823c5a6c..000000000 --- a/lib/decidim/decidim_awesome/organization_memoizer.rb +++ /dev/null @@ -1,21 +0,0 @@ -# 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 defined?(request) && request.env["decidim.current_organization"]&.id - - OrganizationMemoizer.memoize("#{request.env["decidim.current_organization"].id}-#{key}", &) - end - end - end -end diff --git a/lib/decidim/decidim_awesome/request_memoizer.rb b/lib/decidim/decidim_awesome/request_memoizer.rb new file mode 100644 index 000000000..01dde0075 --- /dev/null +++ b/lib/decidim/decidim_awesome/request_memoizer.rb @@ -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 From f66fabcc1bacb3c695204f42908a90f5968c3b23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Verg=C3=A9s?= Date: Mon, 2 Dec 2024 23:53:12 +0100 Subject: [PATCH 5/5] Fix cell spec --- .../decidim/decidim_awesome/proposal_l_cell_override.rb | 2 +- spec/cells/proposal_l_cell_spec.rb | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/cells/concerns/decidim/decidim_awesome/proposal_l_cell_override.rb b/app/cells/concerns/decidim/decidim_awesome/proposal_l_cell_override.rb index 3dfcd5291..7d85bd460 100644 --- a/app/cells/concerns/decidim/decidim_awesome/proposal_l_cell_override.rb +++ b/app/cells/concerns/decidim/decidim_awesome/proposal_l_cell_override.rb @@ -15,7 +15,7 @@ def metadata_cell end def cache_hash - @cache_hash ||= "#{decidim_original_cache_hash}#{Decidim.cache_key_separator}#{model.extra_fields&.reload&.vote_weight_totals}" + @cache_hash ||= "#{decidim_original_cache_hash}#{Decidim.cache_key_separator}#{model.extra_fields&.vote_weight_totals}" end end end diff --git a/spec/cells/proposal_l_cell_spec.rb b/spec/cells/proposal_l_cell_spec.rb index d827e161f..5099b8cd3 100644 --- a/spec/cells/proposal_l_cell_spec.rb +++ b/spec/cells/proposal_l_cell_spec.rb @@ -12,8 +12,10 @@ module Proposals let!(:component) { create(:proposal_component, :with_votes_enabled, organization:, settings: { awesome_voting_manifest: manifest }) } let!(:extra_fields) { create(:awesome_proposal_extra_fields, proposal:) } let(:proposal) { create(:proposal, component:) } + let(:request) { double(host: "example.org", env: {}) } before do + allow(subject).to receive(:request).and_return(request) Decidim::DecidimAwesome.voting_registry.register(:another_voting_system) do |voting| voting.show_vote_button_view = "" voting.show_votes_count_view = "" @@ -41,6 +43,7 @@ module Proposals extra_fields.update_column(:vote_weight_totals, 100) # rubocop:enable Rails/SkipsModelValidations proposal.reload + subject.instance_variable_set(:@cache_hash, nil) expect(cache1).not_to eq(subject.send(:cache_hash)) end end