Class: AdminMessage

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

Overview

Admin messages are the messages that are sent out when people are joining or leaving a mailing list.

Constant Summary collapse

TemplateRegex =

The regular expression defining what templates look like. The templates are the things like address that are replaced with an actual address.

/\{(\w+)\}/

Instance Method Summary collapse

Instance Method Details

#render(template_values) ⇒ Object

Renders the current administration message, filling in the template with the values with the values in template_values. For example, if the message is:

Thank you for subscribing to the "{mldesc}" mailing list.

You might call #render as such:

adminmsg.render :mldesc => "quick example"

This would return:

Thank you for subscribing to the "quick example" mailing list.
template_values

A Hash containing as keys, template keys, and as values, the values to replace the template keys with.



28
29
30
31
32
33
34
35
# File 'app/models/admin_message.rb', line 28

def render template_values
    template_values.each_key do |k|
        template_values[k.to_s]=template_values[k]
    end
    self.message.gsub(TemplateRegex) do |text|
        template_values[ text.match(TemplateRegex)[1] ]
    end
end

#send_mail(from, to, template_values) ⇒ Object

Sends out an admin message with with the from address in the envelope “From” string, the to address in the envelope “To” string, and with with template_values expanded as in render.1



40
41
42
43
44
# File 'app/models/admin_message.rb', line 40

def send_mail from, to, template_values
    Net::SMTP.start("localhost", 25) do |smtp|
        smtp.send_message render(template_values), from, to
    end
end