Class: MailyHerald::Dispatch

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/maily_herald/dispatch.rb

Overview

Main dispatch class.

Inherited by all Mailing classes. Each Dispatch instance need to have associated List. Dispatch can be in one of three states:

  • enabled

  • disabled

  • archived

Direct Known Subclasses

Mailing, Sequence

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#absolute_delayString

Email delivery delay from beginning of sequence. Valid only for SequenceMailing.

Returns:

  • (String)

    the current value of absolute_delay



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def absolute_delay
  @absolute_delay
end

#conditionsString

Delivery conditions as Liquid expression.

Returns:

  • (String)

    the current value of conditions



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def conditions
  @conditions
end

#fromString

Sender email address. If not provided, action_mailer.default_options[:from} is used. Valid only for Mailing.

Returns:

  • (String)

    the current value of from



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def from
  @from
end

#list_idFixnum

List association id.

Returns:

  • (Fixnum)

    the current value of list_id



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def list_id
  @list_id
end

#mailer_nameString

Mailer class name. This refers to Mailer used by Dispatch while sending emails.

Returns:

  • (String)

    the current value of mailer_name



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def mailer_name
  @mailer_name
end

#nameString

Dispatch name.

Returns:

  • (String)

    the current value of name



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def name
  @name
end

#override_subscriptionString

Defines whether email should be sent regardless of entity subscription state.

Returns:

  • (String)

    the current value of override_subscription



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def override_subscription
  @override_subscription
end

#periodString

Email delivery period. Valid only for PeriodicalMailing.

Returns:

  • (String)

    the current value of period



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def period
  @period
end

#sequence_idFixnum

Sequence association id.

Returns:

  • (Fixnum)

    the current value of sequence_id



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def sequence_id
  @sequence_id
end

#stateObject

Returns dispatch state as symbol



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def state
  @state
end

#subjectString

Email subject as Liquid template. Valid only for Mailing.

Returns:

  • (String)

    the current value of subject



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def subject
  @subject
end

#templateString

Email body template as Liquid template. Valid only for Mailing.

Returns:

  • (String)

    the current value of template



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def template
  @template
end

#titleString

Dispatch title.

Returns:

  • (String)

    the current value of title



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def title
  @title
end

#typeString

Polymorphic type.

Returns:

  • (String)

    the current value of type



33
34
35
# File 'app/models/maily_herald/dispatch.rb', line 33

def type
  @type
end

Instance Method Details

#archiveObject



131
132
133
# File 'app/models/maily_herald/dispatch.rb', line 131

def archive
  write_attribute(:state, "archived")
end

#archive!Object



121
122
123
# File 'app/models/maily_herald/dispatch.rb', line 121

def archive!
  update_attribute(:state, "archived")
end

#archived?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/maily_herald/dispatch.rb', line 111

def archived?
  self.state == :archived
end

#disableObject



128
129
130
# File 'app/models/maily_herald/dispatch.rb', line 128

def disable
  write_attribute(:state, "disabled")
end

#disable!Object



118
119
120
# File 'app/models/maily_herald/dispatch.rb', line 118

def disable!
  update_attribute(:state, "disabled")
end

#disabled?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'app/models/maily_herald/dispatch.rb', line 108

def disabled?
  self.state == :disabled
end

#enableObject



125
126
127
# File 'app/models/maily_herald/dispatch.rb', line 125

def enable
  write_attribute(:state, "enabled")
end

#enable!Object



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

def enable!
  update_attribute(:state, "enabled")
end

#enabled?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'app/models/maily_herald/dispatch.rb', line 105

def enabled?
  self.state == :enabled
end

#has_start_at_proc?Boolean

Returns:

  • (Boolean)


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

def has_start_at_proc?
  !!(@start_at_proc || MailyHerald.start_at_procs[self.id])
end

#in_scope?(entity) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'app/models/maily_herald/dispatch.rb', line 147

def in_scope? entity
  self.list.context.scope.exists?(entity)
end

#list=(l) ⇒ Object

Sets List associated with this dispatch

Parameters:



138
139
140
141
# File 'app/models/maily_herald/dispatch.rb', line 138

def list= l
  l = MailyHerald::List.find_by_name(l.to_s) if l.is_a?(String) || l.is_a?(Symbol)
  super(l)
end

#locked?Boolean

Check if dispatch is locked.

Returns:

  • (Boolean)

See Also:



165
166
167
# File 'app/models/maily_herald/dispatch.rb', line 165

def locked?
  MailyHerald.dispatch_locked?(self.name)
end

#processable?(entity) ⇒ Boolean

Checks if dispatch can be sent to given entity.

Following checks are performed:

  • dispatch is enabled,

  • subscription is overriden or user is subscribed to dispatch list,

  • entity belongs to list Context scope.

Parameters:

  • entity (ActiveRecord::Base)

    Recipient

Returns:

  • (Boolean)


159
160
161
# File 'app/models/maily_herald/dispatch.rb', line 159

def processable? entity
  self.enabled? && subscription_valid?(entity) && in_scope?(entity)
end

#start_atObject

Returns time as string with Liquid expression or Proc.



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

def start_at
  @start_at_proc || MailyHerald.start_at_procs[self.id] || read_attribute(:start_at)
end

#start_at=(v) ⇒ Object

Sets start_at value of OneTimeMailing, PeriodicalMailing and SequenceMailing.

Parameters:

  • v

    String with Liquid expression or ‘Proc` that evaluates to `Time`.



75
76
77
78
79
80
81
# File 'app/models/maily_herald/dispatch.rb', line 75

def start_at= v
  if v.respond_to? :call
    @start_at_proc = v
  else
    super(v.is_a?(Time) ? v.to_s : v)
  end
end

#start_at_changed?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'app/models/maily_herald/dispatch.rb', line 92

def start_at_changed?
  if has_start_at_proc?
    @start_at_proc != MailyHerald.start_at_procs[self.id]
  else
    super
  end
end

#subscription_valid?(entity) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'app/models/maily_herald/dispatch.rb', line 143

def subscription_valid? entity
  self.override_subscription? || self.list.subscribed?(entity)
end