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(copy_sender html text mailer subject subscriber)

Constants included from Util::EmailHelpers

Util::EmailHelpers::AUTO_LINK_RE, Util::EmailHelpers::BRACKETS, 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

#auto_link_urls, #bracket, #escape_once, #header_sanitizer, #html_to_text, #normalize_new_lines, #remove_brackets, #remove_regard, #sanitize_header, #subscriber_name_and_address, #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



106
107
108
# File 'lib/mlist/email_post.rb', line 106

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

.human_name(options = {}) ⇒ Object

vvv ActiveRecord validations interface implementation vvv



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

def self.human_name(options = {})
  self.name.humanize
end

.self_and_descendants_from_active_recordObject

nodoc:



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

def self.self_and_descendants_from_active_record #nodoc:
  [self]
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

#copy_sender=(value) ⇒ Object



47
48
49
# File 'lib/mlist/email_post.rb', line 47

def copy_sender=(value)
  @attributes['copy_sender'] = %w(true 1).include?(value.to_s)
end

#errorsObject



110
111
112
# File 'lib/mlist/email_post.rb', line 110

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

#subjectObject



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

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

#to_sObject



64
65
66
# File 'lib/mlist/email_post.rb', line 64

def to_s
  to_tmail.to_s
end

#to_tmailObject

Raises:

  • (ActiveRecord::RecordInvalid)


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
94
# File 'lib/mlist/email_post.rb', line 68

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
  
  builder.from = subscriber_name_and_address(subscriber)
  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)


122
123
124
# File 'lib/mlist/email_post.rb', line 122

def valid?
  validate
end

#validateObject



114
115
116
117
118
119
120
# File 'lib/mlist/email_post.rb', line 114

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