Module: Redmine::Acts::Watchable::InstanceMethods

Defined in:
lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



32
33
34
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 32

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#add_watcher(user) ⇒ Object

Adds user as a watcher



46
47
48
49
50
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 46

def add_watcher(user)
  # Rails does not reset the has_many :through association
  watcher_users.reset
  self.watchers << Watcher.new(:user => user)
end

#addable_watcher_usersObject

Returns an array of users that are proposed as watchers



37
38
39
40
41
42
43
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 37

def addable_watcher_users
  users = self.project.principals.assignable_watchers.sort - self.watcher_users
  if respond_to?(:visible?)
    users.reject! {|user| user.is_a?(User) && !visible?(user)}
  end
  users
end

#notified_watchersObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 86

def notified_watchers
  notified = watcher_users.active.to_a
  notified = notified.map {|n| n.is_a?(Group) ? n.users.active : n}.flatten
  notified.uniq!
  notified.reject! {|user| user.mail.blank? || user.mail_notification == 'none'}
  if respond_to?(:visible?)
    notified.reject! {|user| !visible?(user)}
  end
  notified
end

#remove_watcher(user) ⇒ Object

Removes user from the watchers list



53
54
55
56
57
58
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 53

def remove_watcher(user)
  return nil unless user && (user.is_a?(User) || user.is_a?(Group))
  # Rails does not reset the has_many :through association
  watcher_users.reset
  watchers.where(:user_id => user.id).delete_all
end

#set_watcher(user, watching = true) ⇒ Object

Adds/removes watcher



61
62
63
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 61

def set_watcher(user, watching=true)
  watching ? add_watcher(user) : remove_watcher(user)
end

#watched_by?(principal) ⇒ Boolean

Returns true if object is watched by principal, that is either by a given group, or by a given user or any of their groups

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 76

def watched_by?(principal)
  return false unless principal

  user_ids = Array(principal.id)
  user_ids |= principal.group_ids if principal.is_a?(User)
  user_ids.compact!

  (self.watcher_user_ids & user_ids).any?
end

#watcher_recipientsObject

Returns an array of watchers’ email addresses



98
99
100
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 98

def watcher_recipients
  notified_watchers.collect(&:mail)
end

#watcher_user_ids=(user_ids) ⇒ Object

Overrides watcher_user_ids= to make user_ids uniq



66
67
68
69
70
71
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 66

def watcher_user_ids=(user_ids)
  if user_ids.is_a?(Array)
    user_ids = user_ids.uniq
  end
  super user_ids
end