Project

General

Profile

Feature #4347 » 0001-auto-watch-issues-a-user-contributes-to.patch

rebased on master / 20210420 (corrected patch) - Jens Krämer, 2021-04-21 05:07

View differences:

app/helpers/users_helper.rb
42 42
     [l('label_last_tab_visited'), 'last_tab_visited']]
43 43
  end
44 44

  
45
  def auto_watch_on_options
46
    UserPreference::AUTO_WATCH_ON_OPTIONS.map {|o| [l("label_auto_watch_on_#{o}"), o]}.to_h
47
  end
48

  
45 49
  def change_status_link(user)
46 50
    url = {:controller => 'users', :action => 'update', :id => user, :page => params[:page], :status => params[:status], :tab => nil}
47 51

  
app/models/journal.rb
59 59
      end
60 60
  )
61 61
  before_create :split_private_notes
62
  before_create :add_watcher
62 63
  after_create_commit :send_notification
63 64

  
64 65
  scope :visible, (lambda do |*args|
......
324 325
    true
325 326
  end
326 327

  
328
  def add_watcher
329
    if user &&
330
        user.allowed_to?(:add_issue_watchers, project) &&
331
        user.pref.auto_watch_on?('add_note') &&
332
        !Watcher.any_watched?(Array.wrap(journalized), user)
333
      journalized.set_watcher(user, true)
334
    end
335
  end
336

  
327 337
  def send_notification
328 338
    if notify? &&
329 339
        (
app/models/user_preference.rb
37 37
    'textarea_font',
38 38
    'recently_used_projects',
39 39
    'history_default_tab',
40
    'toolbar_language_options')
40
    'toolbar_language_options',
41
    'auto_watch_on',
42
  )
41 43

  
42 44
  TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
43 45
  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]
46
  AUTO_WATCH_ON_OPTIONS = ['add_note']
44 47

  
45 48
  def initialize(attributes=nil, *args)
46 49
    super
......
54 57
      unless attributes && attributes.key?(:no_self_notified)
55 58
        self.no_self_notified = Setting.default_users_no_self_notified
56 59
      end
60
      unless attributes && attributes.key?(:auto_watch_on)
61
        self.auto_watch_on = AUTO_WATCH_ON_OPTIONS
62
      end
57 63
    end
58 64
    self.others ||= {}
59 65
  end
......
116 122
    self[:toolbar_language_options] = languages.join(',')
117 123
  end
118 124

  
125
  def auto_watch_on; self[:auto_watch_on] || []; end
126
  def auto_watch_on=(values); self[:auto_watch_on]=values; end
127
  def auto_watch_on?(action); self.auto_watch_on.include?(action.to_s); end
128

  
119 129
  # Returns the names of groups that are displayed on user's page
120 130
  # Example:
121 131
  #   preferences.my_page_groups
app/views/my/account.html.erb
58 58
  <%= render :partial => 'users/mail_notifications' %>
59 59
</fieldset>
60 60

  
61
<fieldset class="box">
62
  <legend><%=l(:label_auto_watch_on)%></legend>
63
  <%= render :partial => 'users/auto_watch_on' %>
64
</fieldset>
65

  
61 66
<fieldset class="box tabular">
62 67
  <legend><%=l(:label_preferences)%></legend>
63 68
  <%= render :partial => 'users/preferences' %>
app/views/users/_auto_watch_on.html.erb
1
<%= labelled_fields_for :pref, @user.pref do |pref_fields| %>
2
  <%= pref_fields.collection_check_boxes :auto_watch_on, auto_watch_on_options, :last, :first, :checked => @user.pref.auto_watch_on do |b| %>
3
    <p><%= b.check_box %> <%= b.label %></p>
4
  <% end %>
5
<% end %>
app/views/users/_form.html.erb
66 66
  <%= render :partial => 'users/mail_notifications' %>
67 67
</fieldset>
68 68

  
69
<fieldset class="box">
70
  <legend><%=l(:label_auto_watch_on)%></legend>
71
  <%= render :partial => 'users/auto_watch_on' %>
72
</fieldset>
73

  
69 74
<fieldset class="box tabular">
70 75
  <legend><%=l(:label_preferences)%></legend>
71 76
  <%= render :partial => 'users/preferences' %>
config/locales/en.yml
951 951
  label_downloads_abbr: D/L
952 952
  label_optional_description: Optional description
953 953
  label_add_another_file: Add another file
954
  label_auto_watch_on: Auto watch
955
  label_auto_watch_on_add_note: Issues I contributed to
954 956
  label_preferences: Preferences
955 957
  label_chronological_order: In chronological order
956 958
  label_reverse_chronological_order: In reverse chronological order
test/unit/journal_test.rb
120 120
    end
121 121
  end
122 122

  
123
  def test_create_should_add_wacher
124
    user = User.first
125
    user.pref.auto_watch_on=['add_note']
126
    user.save
127
    journal = Journal.new(:journalized => Issue.first, :notes => 'notes', :user => user)
128

  
129
    assert_difference 'Watcher.count', 1 do
130
      assert_equal true, journal.save
131
    end
132
  end
133

  
134
  def test_create_should_not_add_watcher
135
    user = User.first
136
    user.pref.auto_watch_on=[]
137
    user.save
138
    journal = Journal.new(:journalized => Issue.first, :notes => 'notes', :user => user)
139

  
140
    assert_no_difference 'Watcher.count' do
141
      assert_equal true, journal.save
142
    end
143
  end
144

  
123 145
  def test_visible_scope_for_anonymous
124 146
    # Anonymous user should see issues of public projects only
125 147
    journals = Journal.visible(User.anonymous).to_a
test/unit/user_preference_test.rb
57 57
    end
58 58
  end
59 59

  
60
  def test_auto_watch_on_should_default_to_setting
61
    preference = UserPreference.new
62
    assert_equal ['add_note'], preference.auto_watch_on
63
  end
64

  
60 65
  def test_create
61 66
    user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
62 67
    user.login = "newuser"
(5-5/5)