Module: UsersMessages

Defined in:
lib/generators/fetty/messages/templates/lib/users_messages.rb

Instance Method Summary collapse

Instance Method Details

#empty_messages(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/generators/fetty/messages/templates/lib/users_messages.rb', line 58

def empty_messages(options = {})
  if options.empty? or options[:inbox] or options[:outbox]
    self.inbox.update_all(:deleted => true)
    self.outbox.update_all(:deleted => true)
  elsif options[:trash]
    self.trash.delete_all
  end
end

#inbox(options = {}) ⇒ Object



41
42
43
44
45
# File 'lib/generators/fetty/messages/templates/lib/users_messages.rb', line 41

def inbox(options = {})
  options[:deleted] = false
  options[:copies] = false
  self.messages.where(options)
end

#outbox(options = {}) ⇒ Object



47
48
49
50
51
# File 'lib/generators/fetty/messages/templates/lib/users_messages.rb', line 47

def outbox(options = {})
  options[:deleted] = false
  options[:copies] = true
  self.messages.where(options)
end

#send_message(options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/fetty/messages/templates/lib/users_messages.rb', line 10

def send_message(options)
  unless options[:recipients].nil?
    transaction do
      recipients = options[:recipients]
      options.delete(:recipients)

      options[:subject_id] = Message.sequence_subject_id if options[:subject_id].nil?

      recipients.each do |rec|
        # => create message copies
        options[:user_id] = self.id
        options[:sender_id] = self.id
        options[:recipient_id] = rec.id
        options[:copies] = true
        Message.create(options)
        # => create message
        options[:user_id] = rec.id 
        options[:sender_id] = self.id
        options[:recipient_id] = rec.id
        options[:copies] = false
        options[:parent_id] = Message.next_parent_id(options[:parent_id]) unless options[:parent_id].nil?
        Message.create(options)
      end
    end
  else
    raise "Required recipients"
  end
rescue Exception => e
  raise e 
end

#send_message?(options) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
# File 'lib/generators/fetty/messages/templates/lib/users_messages.rb', line 3

def send_message?(options)
  send_message(options)
  true                        
rescue Exception => e
  false
end

#trash(options = {}) ⇒ Object



53
54
55
56
# File 'lib/generators/fetty/messages/templates/lib/users_messages.rb', line 53

def trash(options = {})
  options[:deleted] = true
  self.messages.where(options)
end