Class: WelcomeCycle::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/welcome_cycle/email.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Email

Returns a new instance of Email.



5
6
7
8
9
10
# File 'lib/welcome_cycle/email.rb', line 5

def initialize(name, &block)
  @name = name
  instance_eval &block
  raise "You must specify at least one day to send this email by specifying 'days_into_cycle' and/or 'days_offset_from_cycle_end'" if @days_into_cycle.nil? && @days_offset_from_cycle_end.nil?
  WelcomeCycle::EmailRegister.instance << self
end

Instance Method Details

#days_into_cycle(*days) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
# File 'lib/welcome_cycle/email.rb', line 12

def days_into_cycle(*days)
  raise ArgumentError, "'days_into_cycle' can only contain positive numbers" if days.detect { |d| d <= 0 }
  raise "When specifying 'days_into_cycle' you must set the 'welcome_cycle_start_date'. See README config section" if WelcomeCycle.config.welcome_cycle_start_date.nil?
  @days_into_cycle = days
end

#days_offset_from_cycle_end(*days) ⇒ Object



18
19
20
21
# File 'lib/welcome_cycle/email.rb', line 18

def days_offset_from_cycle_end(*days)
  raise "When specifying 'days_offset_from_cycle_end' you must set the 'welcome_cycle_end_date' config section" if WelcomeCycle.config.welcome_cycle_end_date.nil?
  @days_offset_from_cycle_end = days
end

#deliver(r) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/welcome_cycle/email.rb', line 37

def deliver(r)
  if defined?(Delayed) && (defined?(Delayed::Job) || defined?(Delayed::Worker))
    deliver_via_delayed_job(r)
  else
    deliver_directly(r)
  end
end

#recipientsObject



31
32
33
34
35
# File 'lib/welcome_cycle/email.rb', line 31

def recipients
  recipients = WelcomeCycle.config.base_class
  recipients = recipients.where(date_conditions)
  @scope_chain.nil? ? recipients.all : recipients.instance_eval(&@scope_chain)
end

#scope(&block) ⇒ Object



23
24
25
# File 'lib/welcome_cycle/email.rb', line 23

def scope(&block)
  @scope_chain = block
end

#send_to_recipients!Object



27
28
29
# File 'lib/welcome_cycle/email.rb', line 27

def send_to_recipients!
  recipients.each { |r| deliver(r) }
end