Module: Photon::Delivers::Core::InstanceMethods

Defined in:
lib/photon/delivers/core.rb

Instance Method Summary collapse

Instance Method Details

#read(message) ⇒ Object



43
44
45
# File 'lib/photon/delivers/core.rb', line 43

def read(message)
  change_read_status(message, true)
end

#read_messagesObject



38
39
40
41
# File 'lib/photon/delivers/core.rb', line 38

def read_messages
  through_string = Photon.through.to_s
  constantize_model(Photon.using).where("#{through_string}.#{self.class.to_s.downcase}_id = ? AND #{through_string}.read IS ?", self.id, true).joins(Photon.through)
end

#reply_to(message, opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/photon/delivers/core.rb', line 51

def reply_to(message, opts = {})
  if message.nil? || opts[:body].nil? || opts[:subject].nil?
    false
  else
    delivery_object = constantize_model(Photon.through).where("#{self.class.to_s.downcase}_id = ? AND #{Photon.using.to_s.singularize}_id = ?", self.id, message.id).first
    self.send_message(:to => message.sender, :message_thread_id => delivery_object.message_thread_id, :subject => opts[:subject], :body => opts[:body])
  end
end

#send_message(opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/photon/delivers/core.rb', line 13

def send_message(opts={})
  if opts[:to].nil? || opts[:subject].nil? || opts[:body].nil?
    false
  else
    opts[:message_thread_id] = opts[:message_thread_id] || constantize_model(Photon.thread).create.id
    message = constantize_model(Photon.using).create!(:sender => self,
                                          :users => [opts[:to]].flatten,
                                          :subject => opts[:subject],
                                          :body => opts[:body]
                                         )

    message.deliveries.update_all({:message_thread_id => opts[:message_thread_id], :scope_id => opts[:scope_id], :scope_type => opts[:scope_type]})
    return message
  end
end

#sent_messagesObject



29
30
31
# File 'lib/photon/delivers/core.rb', line 29

def sent_messages
  constantize_model(Photon.using).where(:sender_id => self.id)
end

#unread(message) ⇒ Object



47
48
49
# File 'lib/photon/delivers/core.rb', line 47

def unread(message)
  change_read_status(message, false)
end

#unread_countObject



60
61
62
63
# File 'lib/photon/delivers/core.rb', line 60

def unread_count
  through_string = Photon.through.to_s
  constantize_model(Photon.using).where("#{through_string}.#{self.class.to_s.downcase}_id = ? AND #{through_string}.read IS NOT ?", self.id, true).joins(Photon.through).count
end

#unread_messagesObject



33
34
35
36
# File 'lib/photon/delivers/core.rb', line 33

def unread_messages
  through_string = Photon.through.to_s
  constantize_model(Photon.using).where("#{through_string}.#{self.class.to_s.downcase}_id = ? AND #{through_string}.read IS NOT ?", self.id, true).joins(Photon.through)
end