Mailboxer Gem – 限制消息传递

Mailboxer允许您连接多个模型,如gem页面中的示例所示。 Mailboxer Github页面

您可以在任何其他模型中使用Mailboxer,并在多个不同的模型中使用它。 如果您的应用程序中有鸭子和圆形,并且您想要将消息交换为相同的消息,只需将acts_as_messageable添加到每个消息中,您就可以发送鸭鸭,鸭圆形,圆形鸭和圆柱形圆形消息。

我们如何才能将消息传递限制在duck-cylon之间,反之亦然? 那么,只有一只鸭子可以发起对话而且一个圆圈可以回复? 并且,没有鸭鸭和圆筒式对话可能吗?

您可以向模型添加模块

class Duck < ActiveRecord::Base acts_as_messageable include mailboxer_filter end 

 class Cylon < ActiveRecord::Base acts_as_messageable include mailboxer_filter end 

你的模块......

 module MalboxerFilter def initiator? self.class == Duck end def replyer? self.class == Cylon end def send_message_filtered(beta, body, subject) self.send_message(beta, body, subject) if initiator? && beta.replyer? end def reply_to_sender_filtered(*args) self.reply_to_sender(*args) if replyer? end end 

然后在您的应用中使用send_message_filteredreply_to_sender_filtered 。 如果您需要它,这可能会更复杂......如果Cylon尝试发起消息或者Duck尝试回复,可能会引发exception。