From 340e33202364df37c72599eca2d5df35ac603831 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 25 Sep 2024 13:22:36 +0200 Subject: [PATCH] Modify decorators to updated version of zeitwek --- .../admin/import/importer_decorator.rb | 46 ++++++++----------- .../proposal_answer_creator_decorator.rb | 24 ++++++---- .../import/proposal_creator_decorator.rb | 20 +++++--- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/app/decorators/decidim/admin/import/importer_decorator.rb b/app/decorators/decidim/admin/import/importer_decorator.rb index db2216c5..c7801230 100644 --- a/app/decorators/decidim/admin/import/importer_decorator.rb +++ b/app/decorators/decidim/admin/import/importer_decorator.rb @@ -1,35 +1,29 @@ # frozen_string_literal: true -Decidim::Admin::Import::Importer.class_eval do - def import! - finished_collection = collection.map { |elem| object_to_manage_without_notify_uniquely?(elem) ? elem.finish_without_notif! : elem.finish! } - if collection.any? { |elem| elem.is_a? Decidim::Proposals::Import::ProposalCreator } - notify_collection(finished_collection, :proposals) - elsif collection.any? { |elem| elem.is_a? Decidim::Proposals::Import::ProposalAnswerCreator } - notify_collection(finished_collection, :answers) - end - end - - def notify_collection(collection, type) - recipients = collection.map { |elem| elem.participatory_space.followers.uniq }.flatten.uniq - case type - when :proposals - recipients.each do |recipient| - next unless ["all", "followed-only"].include?(recipient.notification_types) +module Decidim::Admin::Import::ImporterDecorator + def self.decorate + Decidim::Admin::Import::Importer.class_eval do + def import! + finished_collection = collection.map { |elem| object_to_manage_without_notify_uniquely?(elem) ? elem.finish_without_notif! : elem.finish! } + notify_collection(finished_collection, collection.first) + end - ProposalsMailer.notify_massive_import(collection, recipient).deliver_later + def notify_collection(collection, klass) + recipients = collection.flat_map { |elem| elem.participatory_space.followers.where(notification_types: %w(all followed-only)).uniq }.uniq + case klass + when Decidim::Proposals::Import::ProposalCreator + recipients.each { |recipient| ProposalsMailer.notify_massive_import(collection, recipient).deliver_later } + when Decidim::Proposals::Import::ProposalAnswerCreator + recipients.each { |recipient| ProposalsAnswersMailer.notify_massive_import(collection, recipient).deliver_later } + end end - when :answers - recipients.each do |recipient| - next unless ["all", "followed-only"].include?(recipient.notification_types) - ProposalsAnswersMailer.notify_massive_import(collection, recipient).deliver_later + def object_to_manage_without_notify_uniquely?(obj) + # These types of object must be notified in group, not uniquely + [Decidim::Proposals::Import::ProposalCreator, Decidim::Proposals::Import::ProposalAnswerCreator].include? obj.class end end end - - def object_to_manage_without_notify_uniquely?(obj) - # These types of object must be notified in group, not uniquely - [Decidim::Proposals::Import::ProposalCreator, Decidim::Proposals::Import::ProposalAnswerCreator].include? obj.class - end end + +::Decidim::Admin::Import::ImporterDecorator.decorate diff --git a/app/decorators/decidim/proposals/import/proposal_answer_creator_decorator.rb b/app/decorators/decidim/proposals/import/proposal_answer_creator_decorator.rb index fb9564e7..37a6f0aa 100644 --- a/app/decorators/decidim/proposals/import/proposal_answer_creator_decorator.rb +++ b/app/decorators/decidim/proposals/import/proposal_answer_creator_decorator.rb @@ -1,14 +1,20 @@ # frozen_string_literal: true -Decidim::Proposals::Import::ProposalAnswerCreator.class_eval do - def finish_without_notif! - Decidim.traceability.perform_action!( - "answer", - resource, - current_user - ) do - resource.try(:save!) +module Decidim::Proposals::Import::ProposalAnswerCreatorDecorator + def self.decorate + Decidim::Proposals::Import::ProposalAnswerCreator.class_eval do + def finish_without_notif! + Decidim.traceability.perform_action!( + "answer", + resource, + current_user + ) do + resource.try(:save!) + end + resource + end end - resource end end + +Decidim::Proposals::Import::ProposalAnswerCreatorDecorator.decorate diff --git a/app/decorators/decidim/proposals/import/proposal_creator_decorator.rb b/app/decorators/decidim/proposals/import/proposal_creator_decorator.rb index 347b24f7..f47c3d2e 100644 --- a/app/decorators/decidim/proposals/import/proposal_creator_decorator.rb +++ b/app/decorators/decidim/proposals/import/proposal_creator_decorator.rb @@ -1,12 +1,18 @@ # frozen_string_literal: true -Decidim::Proposals::Import::ProposalCreator.class_eval do - def finish_without_notif! - Decidim.traceability.perform_action!(:create, self.class.resource_klass, context[:current_user], visibility: "admin-only") do - resource.save! - resource +module Decidim::Proposals::Import::ProposalCreatorDecorator + def self.decorate + Decidim::Proposals::Import::ProposalCreator.class_eval do + def finish_without_notif! + Decidim.traceability.perform_action!(:create, self.class.resource_klass, context[:current_user], visibility: "admin-only") do + resource.save! + resource + end + publish(resource) + resource + end end - publish(resource) - resource end end + +::Decidim::Proposals::Import::ProposalCreatorDecorator.decorate