]> source.dussan.org Git - redmine.git/commitdiff
Override watcher_user_ids= to make ids uniq (#10538).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 25 Mar 2012 19:52:24 +0000 (19:52 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 25 Mar 2012 19:52:24 +0000 (19:52 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9269 e93f8b46-1217-0410-a6f0-8f06a7374b81

test/unit/watcher_test.rb
vendor/plugins/acts_as_watchable/lib/acts_as_watchable.rb

index 35651179e608601abecbc656653d9a516f5be8e1..564cd0503711bc3088110faf15ef7a9529496178 100644 (file)
@@ -81,6 +81,13 @@ class WatcherTest < ActiveSupport::TestCase
     assert issue.watched_by?(User.find(1))
   end
 
+  def test_watcher_user_ids_should_make_ids_uniq
+    issue = Issue.new(:project => Project.find(1), :tracker_id => 1, :subject => "test", :author => User.find(2))
+    issue.watcher_user_ids = ['1', '3', '1']
+    issue.save!
+    assert_equal 2, issue.watchers.count
+  end
+
   def test_addable_watcher_users
     addable_watcher_users = @issue.addable_watcher_users
     assert_kind_of Array, addable_watcher_users
index 96cb3a1574df70c0a6fbc84cc65887cc3f2efc81..5420da0fef517d82c6ce9feb9765c3ecb9d21085 100644 (file)
@@ -9,8 +9,6 @@ module Redmine
       module ClassMethods
         def acts_as_watchable(options = {})
           return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods)
-          send :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
@@ -21,6 +19,8 @@ module Redmine
             }
             attr_protected :watcher_ids, :watcher_user_ids
           end
+          send :include, Redmine::Acts::Watchable::InstanceMethods
+          alias_method_chain :watcher_user_ids=, :uniq_ids
         end
       end
 
@@ -54,6 +54,14 @@ module Redmine
           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 })