module Mailboxer module Models module Messageable # :nodoc: included do class Mailboxer::Message < Mailboxer::Notification has_many :message_attachments attr_accessible :message_attachments end end def send_message_mult_attach(recipients, msg_body, subject, sanitize_text = true, attachments = nil, message_timestamp = Time.now) convo = build_convo(subject, message_timestamp) message = build_message(convo, recipients, msg_body, subject, message_timestamp) attach_files(attachments, message) message.deliver false, sanitize_text end def reply(conversation, recipients, reply_body, subject=nil, sanitize_text=true) subject = subject || "#{conversation.subject}" build_message(conversation, recipients, reply_body, subject, message_timestamp = Time.now) end def reply_to_sender_mult_attach(receipt, reply_body, subject = nil, sanitize_text = true, attachments = nil) message = reply(receipt.conversation, receipt.message.sender, reply_body, subject, sanitize_text) attach_files(attachments, message) message.deliver false, sanitize_text end def reply_to_convo_mult_attach(conversation, reply_body, subject=nil, should_untrash=true, sanitize_text=true, attachments=nil) #move conversation to inbox if it is currently in the trash and should_untrash parameter is true. if should_untrash && mailbox.is_trashed?(conversation) mailbox.receipts_for(conversation).untrash mailbox.receipts_for(conversation).mark_as_not_deleted end message = reply(conversation, conversation.last_message.recipients, reply_body, subject, sanitize_text) attach_files(attachments, message) message.deliver false, sanitize_text end def build_convo(subject, message_timestamp) ::Mailboxer::ConversationBuilder.new( subject: subject, created_at: message_timestamp, updated_at: message_timestamp ).build end def build_message(convo, recipients, msg_body, subject, message_timestamp) ::Mailboxer::MessageBuilder.new( sender: self, conversation: convo, recipients: recipients, body: msg_body, subject: subject, created_at: message_timestamp, updated_at: message_timestamp ).build end def attach_files(attachments, message) return if attachments.blank? attachments.each do |m| message.message_attachments << MessageAttachment.new(file: m[:file]) end end end end end