Module: AcidicJob::Extensions::Noticed

Extended by:
ActiveSupport::Concern
Defined in:
lib/acidic_job/extensions/noticed.rb

Instance Method Summary collapse

Instance Method Details

#deliver_acidicly(recipients) ⇒ Object Also known as: deliver_transactionally



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/acidic_job/extensions/noticed.rb', line 14

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

      AcidicJob::Run.create!(
        staged: true,
        job_class: job_class.name,
        serialized_job: serialized_job,
        idempotency_key: IdempotencyKey.value_for(serialized_job)
      )
    end
  end
end