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
-
#add_watcher(user) ⇒ Object
Adds user as a watcher.
-
#addable_watcher_users ⇒ Object
Returns an array of users that are proposed as watchers.
- #notified_watchers ⇒ Object
-
#remove_watcher(user) ⇒ Object
Removes user from the watchers list.
-
#set_watcher(user, watching = true) ⇒ Object
Adds/removes watcher.
-
#valid_watcher?(user) ⇒ Boolean
true if user can be added as a watcher.
-
#visible_watcher_users(user = User.current) ⇒ Object
array of watchers that the given user is allowed to see.
-
#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. -
#watcher_recipients ⇒ Object
Returns an array of watchers’ email addresses.
-
#watcher_user_ids=(user_ids) ⇒ Object
Overrides watcher_user_ids= to make user_ids uniq.
Class Method Details
.included(base) ⇒ Object
50 51 52 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 50 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#add_watcher(user) ⇒ Object
Adds user as a watcher
82 83 84 85 86 87 88 89 90 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 82 def add_watcher(user) if persisted? # Rails does not reset the has_many :through association watcher_users.reset self.watchers << Watcher.new(:user => user) else self.watcher_users << user end end |
#addable_watcher_users ⇒ Object
Returns an array of users that are proposed as watchers
55 56 57 58 59 60 61 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 55 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_watchers ⇒ Object
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 131 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
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 93 def remove_watcher(user) return nil unless user && (user.is_a?(User) || user.is_a?(Group)) if persisted? # Rails does not reset the has_many :through association watcher_users.reset watchers.where(:user_id => user.id).delete_all else watcher_users.delete(user) end end |
#set_watcher(user, watching = true) ⇒ Object
Adds/removes watcher
106 107 108 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 106 def set_watcher(user, watching=true) watching ? add_watcher(user) : remove_watcher(user) end |
#valid_watcher?(user) ⇒ Boolean
true if user can be added as a watcher
74 75 76 77 78 79 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 74 def valid_watcher?(user) return true unless respond_to?(:visible?) return true unless user.is_a?(User) visible?(user) end |
#visible_watcher_users(user = User.current) ⇒ Object
array of watchers that the given user is allowed to see
64 65 66 67 68 69 70 71 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 64 def visible_watcher_users(user = User.current) if user.allowed_to?(:"view_#{self.class.name.underscore}_watchers", project) watcher_users else # without permission, the user can only see themselves (if they're a watcher) watcher_users & [user] end 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
121 122 123 124 125 126 127 128 129 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 121 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_recipients ⇒ Object
Returns an array of watchers’ email addresses
143 144 145 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 143 def watcher_recipients notified_watchers.collect(&:mail) end |
#watcher_user_ids=(user_ids) ⇒ Object
Overrides watcher_user_ids= to make user_ids uniq
111 112 113 114 115 116 |
# File 'lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb', line 111 def watcher_user_ids=(user_ids) if user_ids.is_a?(Array) user_ids = user_ids.uniq end super user_ids end |