Module: PrivateMessages::IsAPrivateMessage::ClassMethods

Defined in:
lib/private_messages/is_a_private_message.rb

Instance Method Summary collapse

Instance Method Details

#is_private_message(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/private_messages/is_a_private_message.rb', line 10

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, :conditions => "read_at IS NOT NULL"
    #scope :unread, :conditions => "read_at IS NULL"
     scope :already_read, -> { where("read_at IS NOT NULL") }
     scope :unread, -> { where("read_at IS NULL") }
end

#read_message(id, reader) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/private_messages/is_a_private_message.rb', line 30

def read_message(id, reader)
    message = where("id=? and (sender_id = ? OR recipient_id = ?)", id, reader, reader).first()
    #message = self.find(id, :conditions => ["sender_id = ? OR recipient_id = ?", reader, reader]).first()
    if message.read_at.nil? && reader == message.recipient
      message.read_at = Time.now
      message.save!
    end
    message
end