Created
July 12, 2012 23:06
-
-
Save damien/3101717 to your computer and use it in GitHub Desktop.
Extending Mailboxer through Concerns
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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 User | |
acts_as_messagable | |
include Concerns::MessagableWithoutSubject | |
include Concerns::MessagableWithData | |
has_one :profile | |
end |
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
This is a perfect example of what I was looking for. Thanks!
Where did you end up storing these? 'app/concerns'?