Module: PrivateExtension::PrivateMessages::PrivateMessageExtensions::ActMethods

Defined in:
lib/private_message_extensions.rb

Instance Method Summary collapse

Instance Method Details

#is_private_message(options = {}) ⇒ Object

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.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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