module PrivateExtension::PrivateMessages::HasPrivateMessagesExtensions::ActMethods

Public Instance Methods

has_private_messages(options = {}) click to toggle source

Sets up a model have private messages, defining the child class as specified in :class_name (typically “Messages”). Provided the following instance messages:

  • sent_messages - returns a collection of messages for which this object is the sender.

  • received_messages - returns a collection of messages for which this object is the recipient.

# File lib/has_private_messages_extensions.rb, line 13
def has_private_messages(options = {})
  options[:class_name] ||= 'Message'

  unless included_modules.include? InstanceMethods
    class_attribute :options
    table_name = options[:class_name].constantize.table_name

    has_many :sent_messages, -> { where("#{table_name}.sender_deleted = ?", false).order("#{table_name}.created_at DESC") },
             :class_name => options[:class_name],
             :foreign_key => 'sender_id'

    has_many :received_messages, -> { where("#{table_name}.recipient_deleted = ?", false).order("#{table_name}.created_at DESC") },
             :class_name => options[:class_name],
             :foreign_key => 'recipient_id'


    extend ClassMethods
    include InstanceMethods
  end
  self.options = options
end