module PrivateExtension::PrivateMessages::PrivateMessageExtensions::ActMethods

Public Instance Methods

is_private_message(options = {}) click to toggle source

Sets up a model to be a private message model, defining the parent class as specified in :class_name (typically “User”) Provides the following instance methods:

  • sender - the sender of the message.

  • recipient - the recipient of the message.

Also adds a named scopes of :read and :unread, to get, well, read and unread messages.

# File lib/private_message_extensions.rb, line 15
def is_private_message(options = {})
  options[:class_name] ||= 'User'

  unless included_modules.include? InstanceMethods
    belongs_to :sender,
               :class_name => options[:class_name],
               :foreign_key => 'sender_id'
    belongs_to :recipient,
               :class_name => options[:class_name],
               :foreign_key => 'recipient_id'

    extend ClassMethods
    include InstanceMethods
  end

  scope :already_read, -> { where("read_at IS NOT NULL") }
  scope :unread, -> { where("read_at IS NULL") }
end