Module: ActsAsNewsletter::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/acts_as_newsletter/model.rb,
lib/acts_as_newsletter/model/config.rb

Defined Under Namespace

Modules: ClassMethods Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chunk_sentObject

Boolean allowing us to know if we sent the last emails chunk



109
110
111
# File 'lib/acts_as_newsletter/model.rb', line 109

def chunk_sent
  @chunk_sent
end

Instance Method Details

#available_emailsObject

Parses all available e-mails stored in recpients field



129
130
131
# File 'lib/acts_as_newsletter/model.rb', line 129

def available_emails
  @available_emails ||= recipients.split("|")
end

#emailsObject

Takes emails from the list and delete them from it



134
135
136
# File 'lib/acts_as_newsletter/model.rb', line 134

def emails
  @emails ||= available_emails.shift(emails_chunk_size)
end

#newsletter_configObject

Newsletter configuration passed to the block



112
113
114
115
# File 'lib/acts_as_newsletter/model.rb', line 112

def newsletter_config
  @newsletter_config ||=
    Model::Config.new(self, &self.class.config_proc).config
end

#prepare_emailsObject

Prepare model to handle e-mail sending collecting e-mails and initializing counters



121
122
123
124
125
126
# File 'lib/acts_as_newsletter/model.rb', line 121

def prepare_emails
  emails_list = newsletter_config[:emails]
  self.recipients_count = emails_list.length
  self.sent_count = 0
  self.recipients = emails_list.join("|")
end

#send_newsletter!Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/acts_as_newsletter/model.rb', line 138

def send_newsletter!
  prepare_sending! if state_name == :ready
  # Get config from newsletter config
  mail_config_keys = [:template_path, :template_name, :layout, :from]
  config = newsletter_config.select do |key, value|
    mail_config_keys.include?(key) and value
  end

  # Send e-mail to each recipient
  emails.each do |email|
    ActsAsNewsletter::Mailer.newsletter(self, email, config).deliver
  end

  self.chunk_sent = true
  save
end