Project

General

Profile

Defect #39186 » 0001-Fix-watcher-handling-on-unsaved-objects.patch

Holger Just, 2023-10-12 19:23

View differences:

lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
44 44

  
45 45
        # Adds user as a watcher
46 46
        def add_watcher(user)
47
          # Rails does not reset the has_many :through association
48
          watcher_users.reset
49
          self.watchers << Watcher.new(:user => user)
47
          if persisted?
48
            # Rails does not reset the has_many :through association
49
            watcher_users.reset
50
            self.watchers << Watcher.new(:user => user)
51
          else
52
            self.watcher_users << user
53
          end
50 54
        end
51 55

  
52 56
        # Removes user from the watchers list
53 57
        def remove_watcher(user)
54 58
          return nil unless user && (user.is_a?(User) || user.is_a?(Group))
55
          # Rails does not reset the has_many :through association
56
          watcher_users.reset
57
          watchers.where(:user_id => user.id).delete_all
59
          if persisted?
60
            # Rails does not reset the has_many :through association
61
            watcher_users.reset
62
            watchers.where(:user_id => user.id).delete_all
63
          else
64
            watcher_users.delete(user)
65
          end
58 66
        end
59 67

  
60 68
        # Adds/removes watcher
test/unit/watcher_test.rb
124 124
    end
125 125
  end
126 126

  
127
  def test_add_watcher_with_unsaved_object
128
    issue = Issue.new(project: Project.find(1), tracker_id: 1, subject: "test", author: User.find(2))
129
    refute issue.persisted?
130

  
131
    issue.add_watcher(@user)
132
    assert issue.watched_by?(@user)
133

  
134
    assert_equal [@user.id], issue.watcher_user_ids
135
    assert_equal [@user], issue.watcher_users
136

  
137
    assert_equal [nil], issue.watcher_ids
138
    assert_equal 1, issue.watchers.size
139

  
140
    issue.save!
141
    assert 1, Watcher.where(watchable: issue).count
142
  end
143

  
144
  def test_remove_watcher_with_unsaved_object
145
    issue = Issue.new(project: Project.find(1), tracker_id: 1, subject: "test", author: User.find(2))
146
    refute issue.persisted?
147

  
148
    issue.add_watcher(@user)
149
    assert_equal [@user], issue.watcher_users
150

  
151
    issue.remove_watcher(@user)
152
    refute issue.watched_by?(@user)
153

  
154
    assert_equal [], issue.watcher_user_ids
155
    assert_equal [], issue.watcher_users
156

  
157
    assert_equal [], issue.watcher_ids
158
    assert_equal [], issue.watchers
159

  
160
    issue.save!
161
    assert 0, Watcher.where(watchable: issue).count
162
  end
163

  
127 164
  def test_addable_watcher_users_should_not_include_user_that_cannot_view_the_object
128 165
    issue = Issue.new(:project => Project.find(1), :is_private => true)
129 166
    assert_nil issue.addable_watcher_users.detect {|user| user.is_a?(User) && !issue.visible?(user)}
(1-1/2)