diff --git a/app/models/group.rb b/app/models/group.rb index 2e7d3526..2b29a8b9 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -22,6 +22,11 @@ class Group < Principal validates_uniqueness_of :lastname, :case_sensitive => false validates_length_of :lastname, :maximum => 30 + # Returns an array of all of the email addresses of the group's users + def mails + users.collect(&:mail) + end + def to_s lastname.to_s end diff --git a/app/models/watcher.rb b/app/models/watcher.rb index bc1bed6b..aa0fc92a 100644 --- a/app/models/watcher.rb +++ b/app/models/watcher.rb @@ -14,8 +14,8 @@ class Watcher < ActiveRecord::Base belongs_to :watchable, :polymorphic => true - belongs_to :user - + belongs_to :user, :class_name => 'Principal', :foreign_key => 'user_id' + validates_presence_of :user validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id] diff --git a/test/unit/watcher_test.rb b/test/unit/watcher_test.rb index c850849e..4e7cb54c 100644 --- a/test/unit/watcher_test.rb +++ b/test/unit/watcher_test.rb @@ -125,4 +125,34 @@ class WatcherTest < ActiveSupport::TestCase assert Issue.find(1).watched_by?(user) assert !Issue.find(4).watched_by?(user) end + + context "group watch" do + setup do + @group = Group.generate! + Member.generate!(:project => Project.find(1), :roles => [Role.find(1)], :principal => @group) + @group.users << @user = User.find(1) + @group.users << @user2 = User.find(2) + end + + should "be valid" do + assert @issue.add_watcher(@group) + @issue.reload + assert @issue.watchers.detect { |w| w.user == @group } + end + + should "add all group members to recipients" do + @issue.watchers.delete_all + @issue.reload + + assert @issue.watcher_recipients.empty? + assert @issue.add_watcher(@group) + + @user.save + @issue.reload + assert @issue.watcher_recipients.include?(@user.mail) + assert @issue.watcher_recipients.include?(@user2.mail) + + end + + end end diff --git a/vendor/plugins/acts_as_watchable/lib/acts_as_watchable.rb b/vendor/plugins/acts_as_watchable/lib/acts_as_watchable.rb index 2b158383..5fba3d76 100644 --- a/vendor/plugins/acts_as_watchable/lib/acts_as_watchable.rb +++ b/vendor/plugins/acts_as_watchable/lib/acts_as_watchable.rb @@ -64,7 +64,14 @@ module Redmine if respond_to?(:visible?) notified.reject! {|user| !visible?(user)} end - notified.collect(&:mail).compact + + notified.collect {|w| + if w.respond_to?(:mail) && w.mail.present? # Single mail + w.mail + elsif w.respond_to?(:mails) && w.mails.present? # Multiple mail + w.mails + end + }.flatten.compact end module ClassMethods; end