From 891736ef391bb12ecaa245af0bfc73094b62b721 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Mon, 11 Jul 2016 18:10:01 +0000 Subject: [PATCH] Merged r15609 (#23278). git-svn-id: http://svn.redmine.org/redmine/branches/3.2-stable@15638 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb | 4 ++++ test/unit/mail_handler_test.rb | 5 +++-- test/unit/watcher_test.rb | 9 +++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb index 19af8e56c..4bbba854a 100644 --- a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb +++ b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb @@ -40,12 +40,16 @@ module Redmine # Adds user as a watcher def add_watcher(user) + # Rails does not reset the has_many :through association + watcher_users.reset 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) + # Rails does not reset the has_many :through association + watcher_users.reset watchers.where(:user_id => user.id).delete_all end diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index 271586f49..0afc524c6 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -266,12 +266,13 @@ class MailHandlerTest < ActiveSupport::TestCase end def test_add_issue_with_cc + user = User.find_by_mail('dlopper@somenet.foo') issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'}) assert issue.is_a?(Issue) assert !issue.new_record? - issue.reload - assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo')) + assert issue.watched_by?(user) assert_equal 1, issue.watcher_user_ids.size + assert_include user, issue.watcher_users.to_a end def test_add_issue_from_additional_email_address diff --git a/test/unit/watcher_test.rb b/test/unit/watcher_test.rb index f4d91794d..8ffca148a 100644 --- a/test/unit/watcher_test.rb +++ b/test/unit/watcher_test.rb @@ -60,6 +60,15 @@ class WatcherTest < ActiveSupport::TestCase assert_kind_of User, watcher_users.first end + def test_watcher_users_should_be_reloaded_after_adding_a_watcher + issue = Issue.find(2) + user = User.generate! + + assert_difference 'issue.watcher_users.to_a.size' do + issue.add_watcher user + end + end + def test_watcher_users_should_not_validate_user User.where(:id => 1).update_all("firstname = ''") @user.reload -- 2.39.5