-
-
Save robins35/f26cc76a9af0f9446e23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Message | |
extend ActiveModel::Naming | |
include ActiveModel::Conversion | |
include ActiveModel::Validations | |
include CheckedHelper | |
attr_accessor :bcc, :body, :cc, :link_to_timeline, :listing, :listing_id, | |
:message, :recipient, :recipient_id, :save_note, :sender, :sender_id, | |
:send_me_a_copy, :subject | |
validates_length_of :body, maximum: 2000, message: "must be under 2000 characters" | |
validates_length_of :subject, maximum: 1000, message: "must be under 1000 characters" | |
validates_presence_of :body, :recipient, :sender, :subject | |
def initialize options={} | |
options ||= {} | |
self.send_me_a_copy = if options[:send_me_a_copy].present? | |
checked?(options[:send_me_a_copy]) | |
else | |
true | |
end | |
self.bcc = options[:bcc] || "" | |
self.body = options[:body] | |
self.cc = options[:cc] || "" | |
self.link_to_timeline = options[:link_to_timeline] || true | |
self.listing = options[:listing] | |
self.listing_id = options[:listing_id] | |
self.message = options[:message] | |
self.recipient = options[:recipient] | |
self.recipient_id = options[:recipient_id] | |
self.sender = options[:sender] | |
self.sender_id = options[:sender_id] | |
self.subject = options[:subject] | |
self.listing ||= Listing.find_by_id listing_id | |
self.recipient ||= Person.find_by_id recipient_id | |
self.sender ||= Person.find_by_id sender_id | |
end | |
def mailer_params | |
params = { | |
to: recipient.email, | |
from: sender.email, | |
subject: subject | |
} | |
params[:cc] = cc? ? cc.split(';') : [] | |
binding.pry | |
params[:bcc] = bcc? ? bcc.split(';') : [] | |
params | |
end | |
def sender=(person) | |
raise | |
@sender = person | |
if send_me_a_copy.present? | |
if bcc.present? | |
self.bcc += ";#{sender.email}" | |
else | |
self.bcc = sender.email | |
end | |
end | |
end | |
def deliver | |
return false unless valid? | |
Note.transaction do | |
PersonMailer.delay.contact self | |
Note.from_message(self).save validate: false if save_note | |
end | |
true | |
end | |
def cc? | |
cc.present? | |
end | |
def bcc? | |
bcc.present? | |
end | |
def link_to_timeline? | |
link_to_timeline == '1' | |
end | |
def listing=(listing) | |
return unless listing.present? | |
@listing = listing | |
@listing_id = listing.id | |
end | |
def persisted? | |
false | |
end | |
def recipient=(person) | |
return unless person.present? | |
@recipient = person | |
@recipient_id = person.id | |
end | |
def sender=(person) | |
return unless person.present? | |
@sender = person | |
@sender_id = person.id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment