Class: MailyHerald::Mailing

Inherits:
Dispatch
  • Object
show all
Includes:
Autonaming, TemplateRenderer
Defined in:
app/models/maily_herald/mailing.rb

Instance Attribute Summary

Attributes inherited from Dispatch

#absolute_delay, #from, #list_id, #name, #override_subscription, #period, #sequence_id, #state, #subject, #template, #title, #type

Instance Method Summary collapse

Methods included from Autonaming

included

Methods included from TemplateRenderer

included

Methods inherited from Dispatch

#archive, #archive!, #archived?, #disable, #disable!, #disabled?, #enable, #enable!, #enabled?, #has_start_at_proc?, #in_scope?, #list=, #locked?, #processable?, #start_at, #start_at=, #start_at_changed?, #subscription_valid?

Instance Method Details

#ad_hoc?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/models/maily_herald/mailing.rb', line 80

def ad_hoc?
  self.class == AdHocMailing
end

#build_mail(schedule) ⇒ Object

Builds `Mail::Message` object for given entity.

Depending on #mailer_name value it uses either generic mailer (from MailyHerald::Mailer class) or custom mailer.



178
179
180
181
182
183
184
# File 'app/models/maily_herald/mailing.rb', line 178

def build_mail schedule
  if generic_mailer?
    Mailer.generic(schedule, self)
  else
    self.mailer.send(self.name, schedule)
  end
end

#conditionsObject

Returns time as string with Liquid expression or Proc.



54
55
56
# File 'app/models/maily_herald/mailing.rb', line 54

def conditions
  @conditions_proc || MailyHerald.conditions_procs[self.id] || read_attribute(:conditions)
end

#conditions=(v) ⇒ Object

Sets mailing conditions.

Parameters:

  • v

    String with Liquid expression or `Proc` that evaluates to `true` or `false`.



45
46
47
48
49
50
51
# File 'app/models/maily_herald/mailing.rb', line 45

def conditions= v
  if v.respond_to? :call
    @conditions_proc = v
  else
    write_attribute(:conditions, v)
  end
end

#conditions_changed?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'app/models/maily_herald/mailing.rb', line 62

def conditions_changed?
  if has_conditions_proc?
    @conditions_proc != MailyHerald.conditions_procs[self.id]
  else
    super
  end
end

#conditions_met?(entity) ⇒ Boolean

Checks whether entity meets conditions of this Mailing.

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

    if the conditions do not evaluate to boolean.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/maily_herald/mailing.rb', line 122

def conditions_met? entity
  subscription = Subscription.get_from(entity) || self.list.subscription_for(entity)

  if has_conditions_proc?
    !!conditions.call(entity, subscription)
  else
    if self.list.context.attributes
      evaluator = Utils::MarkupEvaluator.new(self.list.context.drop_for(entity, subscription))
      evaluator.evaluate_conditions(self.conditions)
    else
      true
    end
  end
end

#destination(entity) ⇒ Object

Returns destination email address for given entity.



146
147
148
# File 'app/models/maily_herald/mailing.rb', line 146

def destination entity
  self.list.context.destination_for(entity)
end

#general_scheduling?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'app/models/maily_herald/mailing.rb', line 70

def general_scheduling?
  self.start_at.is_a?(String) && Time.parse(self.start_at).is_a?(Time)
rescue
  false
end

#generic_mailer?Boolean

Checks whether Mailing uses generic mailer.

Returns:

  • (Boolean)


110
111
112
# File 'app/models/maily_herald/mailing.rb', line 110

def generic_mailer?
  self.mailer_name == :generic
end

#has_conditions?Boolean

Checks whether Mailig has conditions defined.

Returns:

  • (Boolean)


115
116
117
# File 'app/models/maily_herald/mailing.rb', line 115

def has_conditions?
  self.conditions && (has_conditions_proc? || !self.conditions.empty?)
end

#has_conditions_proc?Boolean

Returns:

  • (Boolean)


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

def has_conditions_proc?
  @conditions_proc || MailyHerald.conditions_procs[self.id]
end

#individual_scheduling?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/maily_herald/mailing.rb', line 76

def individual_scheduling?
  !general_scheduling?
end

#mailerObject

Returns MailyHerald::Mailer class used by this Mailing.



101
102
103
104
105
106
107
# File 'app/models/maily_herald/mailing.rb', line 101

def mailer
  if generic_mailer?
    MailyHerald::Mailer
  else
    self.mailer_name.to_s.constantize
  end
end

#mailer_nameObject



96
97
98
# File 'app/models/maily_herald/mailing.rb', line 96

def mailer_name
  read_attribute(:mailer_name).to_sym
end

#one_time?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/models/maily_herald/mailing.rb', line 88

def one_time?
  self.class == OneTimeMailing
end

#periodical?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/models/maily_herald/mailing.rb', line 84

def periodical?
  self.class == PeriodicalMailing
end

#render_subject(entity) ⇒ Object

Renders email subject line for given entity.

Reads Dispatch#subject attribute and renders it using Liquid within the context for provided entity.



166
167
168
169
170
171
172
# File 'app/models/maily_herald/mailing.rb', line 166

def render_subject entity
  subscription = self.list.subscription_for(entity)
  return unless subscription

  drop = self.list.context.drop_for entity, subscription
  perform_template_rendering drop, self.subject
end

#render_template(entity) ⇒ Object

Renders email body for given entity.

Reads Dispatch#template attribute and renders it using Liquid within the context for provided entity.



154
155
156
157
158
159
160
# File 'app/models/maily_herald/mailing.rb', line 154

def render_template entity
  subscription = self.list.subscription_for(entity)
  return unless subscription

  drop = self.list.context.drop_for entity, subscription
  perform_template_rendering drop, self.template
end

#sequence?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'app/models/maily_herald/mailing.rb', line 92

def sequence?
  self.class == SequenceMailing
end

#test_conditions(entity) ⇒ Object

Checks whether conditions evaluate properly for given entity.



138
139
140
141
142
143
# File 'app/models/maily_herald/mailing.rb', line 138

def test_conditions entity
  conditions_met?(entity)
  true
rescue StandardError => e
  false
end