Class: MList::EmailPost

Inherits:
Object
  • Object
show all
Includes:
Util::EmailHelpers
Defined in:
lib/mlist/email_post.rb

Overview

The simplest post that can be made to an MList::MailList. Every instance must have at least the text content and a subject. Html may also be added.

It is important to understand that this class is intended to be used by applications that have some kind of UI for creating a post. It assumes Rails form builder support is desired, and that there is no need for manipulating the final TMail::Mail object that will be delivered to the list outside of the methods provided herein.

Constant Summary collapse

ATTRIBUTE_NAMES =
%w(html text mailer subject subscriber)

Constants included from Util::EmailHelpers

Util::EmailHelpers::BRACKETS_RE, Util::EmailHelpers::HTML_ESCAPE, Util::EmailHelpers::REGARD_RE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::EmailHelpers

#bracket, #escape_once, #header_sanitizer, #html_to_text, #normalize_new_lines, #remove_brackets, #remove_regard, #sanitize_header, #text_to_html, #text_to_quoted

Constructor Details

#initialize(attributes) ⇒ EmailPost

Returns a new instance of EmailPost.



27
28
29
30
31
32
# File 'lib/mlist/email_post.rb', line 27

def initialize(attributes)
  @attributes = {}
  self.attributes = {
    :mailer => 'MList Client Application'
  }.merge(attributes)
end

Instance Attribute Details

#parent_identifierObject (readonly)

Returns the value of attribute parent_identifier.



25
26
27
# File 'lib/mlist/email_post.rb', line 25

def parent_identifier
  @parent_identifier
end

#reply_to_messageObject

Returns the value of attribute reply_to_message.



25
26
27
# File 'lib/mlist/email_post.rb', line 25

def reply_to_message
  @reply_to_message
end

Class Method Details

.human_attribute_name(attribute_key_name, options = {}) ⇒ Object

vvv ActiveRecord validations interface implementation vvv



97
98
99
# File 'lib/mlist/email_post.rb', line 97

def self.human_attribute_name(attribute_key_name, options = {})
  attribute_key_name.humanize
end

Instance Method Details

#attributesObject



34
35
36
# File 'lib/mlist/email_post.rb', line 34

def attributes
  @attributes.dup
end

#attributes=(new_attributes) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/mlist/email_post.rb', line 38

def attributes=(new_attributes)
  return if new_attributes.nil?
  attributes = new_attributes.dup
  attributes.stringify_keys!
  attributes.each do |attribute_name, value|
    send("#{attribute_name}=", value)
  end
end

#errorsObject



101
102
103
# File 'lib/mlist/email_post.rb', line 101

def errors
  @errors ||= ActiveRecord::Errors.new(self)
end

#subjectObject



56
57
58
# File 'lib/mlist/email_post.rb', line 56

def subject
  @attributes['subject'] || (reply_to_message ? "Re: #{reply_to_message.subject}" : nil)
end

#to_sObject



60
61
62
# File 'lib/mlist/email_post.rb', line 60

def to_s
  to_tmail.to_s
end

#to_tmailObject

Raises:

  • (ActiveRecord::RecordInvalid)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mlist/email_post.rb', line 64

def to_tmail
  raise ActiveRecord::RecordInvalid.new(self) unless valid?
  
  builder = MList::Util::TMailBuilder.new(TMail::Mail.new)
  
  builder.mime_version = "1.0"
  builder.mailer = mailer
  
  if parent_identifier
    builder.in_reply_to = parent_identifier
    builder.references = [bracket(parent_identifier)]
  end
  
  from = subscriber.email_address
  from = "#{subscriber.display_name} #{bracket(from)}" if subscriber.respond_to?(:display_name)
  builder.from = from
  
  builder.subject = subject
  
  if html
    builder.add_text_part(text)
    builder.add_html_part(html)
    builder.set_content_type('multipart/alternative')
  else
    builder.body = text
    builder.set_content_type('text/plain')
  end
  
  builder.tmail
end

#valid?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/mlist/email_post.rb', line 113

def valid?
  validate
end

#validateObject



105
106
107
108
109
110
111
# File 'lib/mlist/email_post.rb', line 105

def validate
  errors.clear
  errors.add(:subject, 'required') if subject.blank?
  errors.add(:text, 'required') if text.blank?
  errors.add(:text, 'needs to be a bit longer') if !text.blank? && text.strip.size < 25
  errors.empty?
end