diff --git a/app/models/user.rb b/app/models/user.rb index 0b3027cab..3a1cc065f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -261,12 +261,16 @@ class User < Principal notified_projects_ids end - # Only users that belong to more than 1 project can select projects for which they are notified def valid_notification_options + self.class.valid_notification_options(self) + end + + # Only users that belong to more than 1 project can select projects for which they are notified + def self.valid_notification_options(user=nil) # Note that @user.membership.size would fail since AR ignores # :include association option when doing a count - if memberships.length < 1 - MAIL_NOTIFICATION_OPTIONS.delete_if {|option| option.first == 'selected'} + if user.nil? || user.memberships.length < 1 + MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'} else MAIL_NOTIFICATION_OPTIONS end diff --git a/app/views/settings/_notifications.rhtml b/app/views/settings/_notifications.rhtml index 4cc81c931..26c11ad9a 100644 --- a/app/views/settings/_notifications.rhtml +++ b/app/views/settings/_notifications.rhtml @@ -8,7 +8,7 @@

<%= setting_check_box :plain_text_mail %>

-

<%= setting_select(:default_notification_option, User::MAIL_NOTIFICATION_OPTIONS.collect {|o| [l(o.last), o.first.to_s]}) %>

+

<%= setting_select(:default_notification_option, User.valid_notification_options.collect {|o| [l(o.last), o.first.to_s]}) %>

diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index a213332b3..9bb1ae62d 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -1,5 +1,5 @@ -# redMine - project management software -# Copyright (C) 2006 Jean-Philippe Lang +# Redmine - project management software +# Copyright (C) 2006-2011 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -303,6 +303,19 @@ class UserTest < ActiveSupport::TestCase assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?} end + def test_valid_notification_options + # without memberships + assert_equal 5, User.find(7).valid_notification_options.size + # with memberships + assert_equal 6, User.find(2).valid_notification_options.size + end + + def test_valid_notification_options_class_method + assert_equal 5, User.valid_notification_options.size + assert_equal 5, User.valid_notification_options(User.find(7)).size + assert_equal 6, User.valid_notification_options(User.find(2)).size + end + def test_mail_notification_all @jsmith.mail_notification = 'all' @jsmith.notified_project_ids = []