diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-04-26 17:23:24 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-04-26 17:23:24 +0000 |
commit | 8d73ddf73ffc2cefbe16ca06970377e13d4bead0 (patch) | |
tree | ecb8fbb4d8330fe936c43127f2b921ad24eec2e8 /lib/plugins/acts_as_watchable | |
parent | 26868d8b140406cce8c2494efbac7f8f85f452d0 (diff) | |
download | redmine-8d73ddf73ffc2cefbe16ca06970377e13d4bead0.tar.gz redmine-8d73ddf73ffc2cefbe16ca06970377e13d4bead0.zip |
Moved Rails plugins required by the core to lib/plugins.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9533 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/plugins/acts_as_watchable')
-rw-r--r-- | lib/plugins/acts_as_watchable/init.rb | 3 | ||||
-rw-r--r-- | lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb | 85 |
2 files changed, 88 insertions, 0 deletions
diff --git a/lib/plugins/acts_as_watchable/init.rb b/lib/plugins/acts_as_watchable/init.rb new file mode 100644 index 000000000..f39cc7d18 --- /dev/null +++ b/lib/plugins/acts_as_watchable/init.rb @@ -0,0 +1,3 @@ +# Include hook code here +require File.dirname(__FILE__) + '/lib/acts_as_watchable' +ActiveRecord::Base.send(:include, Redmine::Acts::Watchable) diff --git a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb new file mode 100644 index 000000000..5420da0fe --- /dev/null +++ b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb @@ -0,0 +1,85 @@ +# ActsAsWatchable +module Redmine + module Acts + module Watchable + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + def acts_as_watchable(options = {}) + return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods) + class_eval do + has_many :watchers, :as => :watchable, :dependent => :delete_all + has_many :watcher_users, :through => :watchers, :source => :user, :validate => false + + named_scope :watched_by, lambda { |user_id| + { :include => :watchers, + :conditions => ["#{Watcher.table_name}.user_id = ?", user_id] } + } + attr_protected :watcher_ids, :watcher_user_ids + end + send :include, Redmine::Acts::Watchable::InstanceMethods + alias_method_chain :watcher_user_ids=, :uniq_ids + end + end + + module InstanceMethods + def self.included(base) + base.extend ClassMethods + end + + # Returns an array of users that are proposed as watchers + def addable_watcher_users + users = self.project.users.sort - self.watcher_users + if respond_to?(:visible?) + users.reject! {|user| !visible?(user)} + end + users + end + + # Adds user as a watcher + def add_watcher(user) + self.watchers << Watcher.new(:user => user) + end + + # Removes user from the watchers list + def remove_watcher(user) + return nil unless user && user.is_a?(User) + Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}" + end + + # Adds/removes watcher + def set_watcher(user, watching=true) + watching ? add_watcher(user) : remove_watcher(user) + end + + # Overrides watcher_user_ids= to make user_ids uniq + 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 + + # Returns true if object is watched by +user+ + def watched_by?(user) + !!(user && self.watcher_user_ids.detect {|uid| uid == user.id }) + end + + # Returns an array of watchers' email addresses + def watcher_recipients + notified = watcher_users.active + notified.reject! {|user| user.mail_notification == 'none'} + + if respond_to?(:visible?) + notified.reject! {|user| !visible?(user)} + end + notified.collect(&:mail).compact + end + + module ClassMethods; end + end + end + end +end |