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

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

Overview

Since:

  • 0.5.1

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_watcher(user) ⇒ Object

Adds user as a watcher



42
43
44
45
46
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 42

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



33
34
35
36
37
38
39
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 33

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

#notified_watchersObject

Since:

  • 2.2.0



74
75
76
77
78
79
80
81
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 74

def notified_watchers
  notified = watcher_users.active.to_a
  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



49
50
51
52
53
54
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 49

def remove_watcher(user)
  return nil unless user && user.is_a?(User)
  # 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



57
58
59
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 57

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

#watched_by?(user) ⇒ Boolean

Returns true if object is watched by user

Returns:

  • (Boolean)


70
71
72
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 70

def watched_by?(user)
  !!(user && self.watcher_user_ids.detect {|uid| uid == user.id })
end

#watcher_recipientsObject

Returns an array of watchers' email addresses



84
85
86
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 84

def watcher_recipients
  notified_watchers.collect(&:mail)
end

#watcher_user_ids_with_uniq_ids=(user_ids) ⇒ Object

Deprecated.

Removed at 3.4.0

Overrides watcher_user_ids= to make user_ids uniq



62
63
64
65
66
67
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 62

def watcher_user_ids_with_uniq_ids=(user_ids)
  if user_ids.is_a?(Array)
    user_ids = user_ids.uniq
  end
  send :watcher_user_ids_without_uniq_ids=, user_ids
end