Skip to content

Instantly share code, notes, and snippets.

@damien
Created July 12, 2012 23:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damien/3101717 to your computer and use it in GitHub Desktop.
Save damien/3101717 to your computer and use it in GitHub Desktop.
Extending Mailboxer through Concerns
module Concerns
module MessagableWithData
extend ActiveSupport::Concern
included do
Message.class_eval do
has_many :message_data,
class_name: MessageData,
extend: Concerns::MessagableWithData::MessageDataAssociationMethods
end
end
module MessageDataAssociationMethods
# When working with the message_data association method,
# calling message_data#attatched will return all of the objects
# referenced by each MessageData instance
#
# @return [Array<Object>] an array of objects
def attatched
self.collect(&:data)
end
end
module InstanceMethods
# Send a message with a data attatchment.
#
# @param [Hash] opts An options hash used to configure and send a message
# @option [User, Array<User>] :to Users to send a message to
# @option [String] :subject An optional subject for this message (nil)
# @option [#serializable_hash] :data A serializable object to be associated with the outgoing message
# @option [File] :attatchment An optional attatchment to include with this message (nil)
# @option [Boolean] :sanitize Weather or not to sanetize input (true)
#
# @return [Receipt] a Receipt
def send_message_with_data(opts = {})
data = opts.delete(:data)
receipt = send_message(opts)
unless receipt.errors.any?
message_data = MessageData.create(user: self, message: receipt.message, data_type: data.class.name, data_id: data.id)
end
receipt
end
end
end
end
module Concerns
module MessagableWithoutSubject
extend ActiveSupport::Concern
module InstanceMethods
# Override's Mailboxer's #send_message such that subjects are not required.
# @see https://github.com/ging/mailboxer/blob/56987403939d6077ddb72bc708df90ab6e672ab6/lib/mailboxer/models/messageable.rb#L48-57
# @param [Hash] opts An options hash used to configure and send a message
# @option [User, Array<User>] :to Users to send a message to
# @option [String] :subject An optional subject for this message (nil)
# @option [File] :attatchment An optional attatchment to include with this message (nil)
# @option [Boolean] :sanitize Weather or not to sanetize input (true)
def send_message(opts = {})
recipients = opts.delete(:to)
subject = opts.delete(:subject)
body = opts.delete(:body)
attachment = opts.delete(:attatchment)
sanitize = opts.delete(:sanitize) ? true : false
if subject.blank?
subject = "Message from #{self.send(Mailboxer.name_method.to_sym)}"
end
convo = Conversation.new(subject: subject)
message = self.messages.new(body: body, subject: subject, attachment: attachment)
message.conversation = convo
message.recipients = recipients.is_a?(Array) ? recipients : [recipients]
message.recipients = message.recipients.uniq
message.deliver(false, sanitize)
end
end
end
end
class User
acts_as_messagable
include Concerns::MessagableWithoutSubject
include Concerns::MessagableWithData
has_one :profile
end
@damien
Copy link
Author

damien commented Jul 12, 2012

All this stuff lets me so something like:

u = User.last
u.send_mesage_with_data(to: User.first, body: "Look ma, no subject! Profile attatched!", data: u.profile)

m = Message.last
m.message_data # => [#<Profile:0x007fe1d5dbb4b0>]
m.message_data.class # => ActiveRecord::Relation

@ericchernuka
Copy link

This is a perfect example of what I was looking for. Thanks!
Where did you end up storing these? 'app/concerns'?

@jesalg
Copy link

jesalg commented May 13, 2014

I found this from the data attachments issue #66 you raised in the mailboxer repo. Too bad it's not part of the gem but this is great. Exactly what I was looking for. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment