Class: Mailing::Sequence

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/mailing/sequence.rb

Class Method Summary collapse

Class Method Details

.name_and_steps_countObject



10
11
12
# File 'app/models/mailing/sequence.rb', line 10

def self.name_and_steps_count
  left_joins(:steps).group(:name).count("mailing_sequence_steps.id")
end

.send_mails(dry_run: false) ⇒ Object

Mailing::Sequence.send_mails(dry_run: true)



15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/mailing/sequence.rb', line 15

def self.send_mails(dry_run: false)
  name_and_steps_count.each do |name, steps_count|
    logger.debug { "Sending mails for #{name} (#{steps_count} steps)" }
    Mailing::SequenceOwner.joins(:sequence).where(sequence: {name: name})
      .where("current_step < ? ", steps_count)
      .where(canceled_at: nil).each do |owner|
      logger.debug { "\tIf ready, will send mail to #{owner.owner_name} current_step is #{owner.current_step}" }
      owner.send_next_step_if_ready(dry_run: dry_run)
    end
  end
end