Skip to content

Commit

Permalink
Modify decorators to updated version of zeitwek
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-mr committed Sep 25, 2024
1 parent 3f68bf6 commit 340e332
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
46 changes: 20 additions & 26 deletions app/decorators/decidim/admin/import/importer_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 340e332

Please sign in to comment.