Class: Mailing::SequenceOwner

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

Instance Method Summary collapse

Instance Method Details

#cancel!Object



31
32
33
# File 'app/models/mailing/sequence_owner.rb', line 31

def cancel!
  update!(canceled_at: Time.zone.now)
end

#canceled?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/mailing/sequence_owner.rb', line 35

def canceled?
  canceled_at.present?
end

#descriptionObject



19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/mailing/sequence_owner.rb', line 19

def description
  desc = ["Sequence: #{sequence_name}", "Owner: #{owner_name}"]
  desc << "Canceled at: #{canceled_at}" if canceled?
  desc << if last_step?
    "Finished"
  else
    "Next Step : #{next_step_name}, ready to send at: #{I18n.l(next_step_ready_at)}"
  end

  desc.join(" - ")
end

#last_activity_atObject



70
71
72
# File 'app/models/mailing/sequence_owner.rb', line 70

def last_activity_at
  last_email_sent_at.presence || created_at
end

#last_step?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/mailing/sequence_owner.rb', line 62

def last_step?
  next_step.blank?
end

#next_stepObject



66
67
68
# File 'app/models/mailing/sequence_owner.rb', line 66

def next_step
  @next_step ||= steps.where("position > ?", current_step).order(position: :asc).first
end

#next_step_ready_atObject



58
59
60
# File 'app/models/mailing/sequence_owner.rb', line 58

def next_step_ready_at
  next_step.ready_at(last_activity_at)
end

#owner_nameObject



11
12
13
14
15
16
17
# File 'app/models/mailing/sequence_owner.rb', line 11

def owner_name
  if owner.respond_to?(:mailing_sequence_owner_description)
    owner.mailing_sequence_owner_description
  else
    owner.name
  end
end

#ready_to_send_next_step?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'app/models/mailing/sequence_owner.rb', line 52

def ready_to_send_next_step?
  return false if canceled?
  return false if last_step?
  next_step.ready_to_send?(last_activity_at)
end

#send_next_step_if_ready(dry_run: false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/mailing/sequence_owner.rb', line 39

def send_next_step_if_ready(dry_run: false)
  # First email is sent at creation if no delay is set
  # This method is then called every day of the week

  if ready_to_send_next_step?
    logger.debug { "Will send email to owner" }
    next_step.send_to_owner(self) unless dry_run
  else
    logger.debug { "Not ready to send next step" }
    nil
  end
end