Sha256: 41bd91686e169a9b43c080f0955e735cfd985956a6c361232bb7bb70d98f6af0

Contents?: true

Size: 1.98 KB

Versions: 18

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module AcidicJob
  module Extensions
    module Noticed
      extend ActiveSupport::Concern

      class_methods do
        def deliver_acidicly(recipients)
          new.deliver_acidicly(recipients)
        end
      end

      def deliver_acidicly(recipients)
        # THIS IS A HACK THAT COPIES AND PASTES KEY PARTS OF THE `Noticed::Base` CODE
        # IN ORDER TO ALLOW US TO TRANSACTIONALLY DELIVER NOTIFICATIONS
        # THIS IS THUS LIABLE TO BREAK WHENEVER THAT GEM IS UPDATED
        delivery_methods = self.class.delivery_methods.dup

        Array.wrap(recipients).uniq.each do |recipient|
          if (index = delivery_methods.find_index { |m| m[:name] == :database })
            database_delivery_method = delivery_methods.delete_at(index)
            self.record = run_delivery_method(database_delivery_method,
                                              recipient: recipient,
                                              enqueue: false,
                                              record: nil)
          end

          delivery_methods.map do |delivery_method|
            job_class = delivery_method_for(delivery_method[:name], delivery_method[:options])
            args = {
              notification_class: self.class.name,
              options: delivery_method[:options],
              params: params,
              recipient: recipient,
              record: record
            }
            serialized_job = job_class.send(:job_or_instantiate, args).serialize
            acidic_identifier = job_class.respond_to?(:acidic_identifier) ? job_class.acidic_identifier : :job_id
            key = IdempotencyKey.new(acidic_identifier).value_for(serialized_job)

            AcidicJob::Run.create!(
              staged: true,
              job_class: job_class.name,
              serialized_job: serialized_job,
              idempotency_key: key
            )
          end
        end
      end
      alias deliver_transactionally deliver_acidicly
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
acidic_job-1.0.0.pre29 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre28 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre27 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre26 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre25 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre24 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre23 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre22 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre21 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre20 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre19 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre18 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre17 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre16 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre15 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre14 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre13 lib/acidic_job/extensions/noticed.rb
acidic_job-1.0.0.pre12 lib/acidic_job/extensions/noticed.rb