# Should be after_create but would be called before previous after_save callbacks
after_save :after_create_from_copy, :create_parent_issue_journal
after_destroy :update_parent_attributes, :create_parent_issue_journal
+ # add_auto_watcher needs to run before sending notifications, thus it needs
+ # to be added after send_notification (after_ callbacks are run in inverse order)
+ # https://api.rubyonrails.org/v5.2.3/classes/ActiveSupport/Callbacks/ClassMethods.html#method-i-set_callback
after_create_commit :send_notification
+ after_create_commit :add_auto_watcher
# Returns a SQL conditions string used to find all issues visible by the specified user
def self.visible_condition(user, options={})
end
end
+ def add_auto_watcher
+ if author &&
+ author.allowed_to?(:add_issue_watchers, project) &&
+ author.pref.auto_watch_on?('issue_created') &&
+ self.watcher_user_ids.exclude?(author.id)
+ self.set_watcher(author, true)
+ end
+ end
+
def send_notification
if notify? && Setting.notified_events.include?('issue_added')
Mailer.deliver_issue_add(self)
TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
DEFAULT_TOOLBAR_LANGUAGE_OPTIONS = %w[c cpp csharp css diff go groovy html java javascript objc perl php python r ruby sass scala shell sql swift xml yaml]
- AUTO_WATCH_ON_OPTIONS = ['issue_contributed_to']
+ AUTO_WATCH_ON_OPTIONS = %w[issue_created issue_contributed_to]
def initialize(attributes=nil, *args)
super
label_optional_description: Optional description
label_add_another_file: Add another file
label_auto_watch_on: Auto watch
+ label_auto_watch_on_issue_created: Issues I created
label_auto_watch_on_issue_contributed_to: Issues I contributed to
label_preferences: Preferences
label_chronological_order: In chronological order
assert_equal [5], issue2.filter_projects_scope('').ids.sort
end
+ def test_create_should_add_watcher
+ user = User.first
+ user.pref.auto_watch_on=['issue_created']
+ user.pref.save
+ issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_add_watcher')
+
+ assert_difference 'Watcher.count', 1 do
+ assert_equal true, issue.save
+ end
+ end
+
+ def test_create_should_add_author_watcher_only_once
+ user = User.first
+ user.pref.auto_watch_on=['issue_created']
+ user.pref.save
+ issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_add_watcher')
+ issue.watcher_user_ids = [user.id]
+
+ assert_difference 'Watcher.count', 1 do
+ assert_equal true, issue.save
+ end
+ end
+
+ def test_create_should_not_add_watcher
+ user = User.first
+ user.pref.auto_watch_on=[]
+ user.pref.save
+ issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_not_add_watcher')
+
+ assert_no_difference 'Watcher.count' do
+ assert_equal true, issue.save
+ end
+ end
+
def test_like_should_escape_query
issue = Issue.generate!(:subject => "asdf")
r = Issue.like('as_f')
def test_auto_watch_on_should_default_to_setting
preference = UserPreference.new
- assert_equal ['issue_contributed_to'], preference.auto_watch_on
+ assert_equal %w[issue_created issue_contributed_to], preference.auto_watch_on
end
def test_create