summaryrefslogtreecommitdiff
path: root/app/models/message.rb
blob: 2478f3f600f5c39bff9186d7aa704a4795e4be21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Message < CouchRest::Model::Base

  use_database :messages

  property :text, String
  property :user_ids_to_show, [String]
  property :user_ids_have_shown, [String] # is this necessary to store?

  timestamps!

  design do
    own_path = Pathname.new(File.dirname(__FILE__))
    load_views(own_path.join('..', 'designs', 'message'))
  end

  def mark_as_read_by(user)
    user_ids_to_show.delete(user.id)
    # is it necessary to keep track of what users have already seen it?
    user_ids_have_shown << user.id unless read_by?(user)
  end

  def read_by?(user)
    user_ids_have_shown.include?(user.id)
  end

  def unread_by?(user)
    user_ids_to_show.include?(user.id)
  end

  def as_json(*args, &block)
    {"id" => id, "text" => text}.as_json(*args, &block)
  end
end