watcher.rb
770 Bytes
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
34
35
36
37
38
39
40
41
42
class Watcher
include Mongoid::Document
include Mongoid::Timestamps
field :email
embedded_in :app, inverse_of: :watchers
belongs_to :user
validate :ensure_user_or_email
before_validation :clear_unused_watcher_type
attr_accessor :watcher_type
def watcher_type
@watcher_type ||= email.present? ? 'email' : 'user'
end
def label
user ? user.name : email
end
def address
user.try(:email) || email
end
protected
def ensure_user_or_email
errors.add(:base, "You must specify either a user or an email address") unless user.present? || email.present?
end
def clear_unused_watcher_type
case watcher_type
when 'user'
self.email = nil
when 'email'
self.user = self.user_id = nil
end
end
end