Class: EmailAddress
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EmailAddress
- Defined in:
- app/models/email_address.rb
Overview
Represents a valid RFC822 email address. See www.w3.org/Protocols/rfc822/ for more information about the entire specification.
Email addresses are directly associated with emails and, therefore, should be used for building and delivering new e-mails.
Associations
Email addresses have the following associations defined as a result of using the has_emails macro:
-
emails- Emails that were composed and are visible to the owner. Emails may have been sent or unsent. -
received_emails- Emails that have been received from others and are visible. Emails may have been read or unread. -
unsent_emails- Emails that have not yet been delivered -
sent_emails- Emails that have already been delivered
Class Method Summary collapse
-
.find_or_create_by_address(address) ⇒ Object
Finds or create an email address based on the given value.
-
.split_address(address) ⇒ Object
Splits the given address into a name and spec.
Instance Method Summary collapse
-
#address=(address) ⇒ Object
Sets the value to be used for this email address.
-
#with_name ⇒ Object
Generates the value for the email address, including the name associated with it (if provided).
Class Method Details
.find_or_create_by_address(address) ⇒ Object
Finds or create an email address based on the given value
26 27 28 29 |
# File 'app/models/email_address.rb', line 26 def find_or_create_by_address(address) name, spec = split_address(address) find_or_create_by_name_and_spec(name, spec) end |
.split_address(address) ⇒ Object
Splits the given address into a name and spec. For example,
EmailAddress.split_address("John Smith <[email protected]") # => ["John Smith", "[email protected]"]
EmailAddress.split_address("[email protected]") # => [nil, "[email protected]"]
35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/email_address.rb', line 35 def split_address(address) if match = /^(\S.*)\s+<(.*)>$/.match(address) name = match[1] spec = match[2] else spec = address end return name, spec end |
Instance Method Details
#address=(address) ⇒ Object
Sets the value to be used for this email address. This can come in two formats:
-
With name - John Doe <[email protected]>
-
Without name - [email protected]
50 51 52 |
# File 'app/models/email_address.rb', line 50 def address=(address) self.name, self.spec = self.class.split_address(address) end |
#with_name ⇒ Object
Generates the value for the email address, including the name associated with it (if provided). For example,
e = EmailAddress.new(:name => 'John Doe', :spec => '[email protected]')
e.with_name # => "John Doe <[email protected]>"
59 60 61 |
# File 'app/models/email_address.rb', line 59 def with_name name.blank? ? spec : "#{name} <#{spec}>" end |