]> source.dussan.org Git - redmine.git/commitdiff
Update notified_project_ids while saving record.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 13 May 2013 17:11:38 +0000 (17:11 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 13 May 2013 17:11:38 +0000 (17:11 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11840 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/my_controller.rb
app/controllers/users_controller.rb
app/models/user.rb
app/views/users/_mail_notifications.html.erb
test/functional/users_controller_test.rb

index 92853067bbb125605560ab94e99762beb4d69155..5328991b32bcd0855d886b8fb64d031701f5d46e 100644 (file)
@@ -55,7 +55,6 @@ class MyController < ApplicationController
       @user.pref.attributes = params[:pref]
       if @user.save
         @user.pref.save
-        @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
         set_language_if_valid @user.language
         flash[:notice] = l(:notice_account_updated)
         redirect_to my_account_path
index 5dd154dc07684b61f7f92afd4c1c1a3caf414bde..3ea65c7aa0ba871acb546279b5c6bf78323e737b 100644 (file)
@@ -94,7 +94,6 @@ class UsersController < ApplicationController
     if @user.save
       @user.pref.attributes = params[:pref]
       @user.pref.save
-      @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
 
       Mailer.account_information(@user, @user.password).deliver if params[:send_information]
 
@@ -141,7 +140,6 @@ class UsersController < ApplicationController
 
     if @user.save
       @user.pref.save
-      @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
 
       if was_activated
         Mailer.account_activated(@user).deliver
index 25cfebac6c84c30f06fb5bb0ecc09b321d34bac8..738554d85ceb11c433993307c9a350708f5feea2 100644 (file)
@@ -105,6 +105,7 @@ class User < Principal
   before_create :set_mail_notification
   before_save   :generate_password_if_needed, :update_hashed_password
   before_destroy :remove_references_before_destroy
+  after_save :update_notified_project_ids
 
   scope :in_group, lambda {|group|
     group_id = group.is_a?(Group) ? group.id : group.to_i
@@ -133,6 +134,8 @@ class User < Principal
     @name = nil
     @projects_by_role = nil
     @membership_by_project_id = nil
+    @notified_projects_ids = nil
+    @notified_projects_ids_changed = false
     base_reload(*args)
   end
 
@@ -325,11 +328,19 @@ class User < Principal
   end
 
   def notified_project_ids=(ids)
-    Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id])
-    Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty?
-    @notified_projects_ids = nil
-    notified_projects_ids
+    @notified_projects_ids_changed = true
+    @notified_projects_ids = ids
+  end
+
+  # Updates per project notifications (after_save callback)
+  def update_notified_project_ids
+    if @notified_projects_ids_changed
+      ids = (mail_notification == 'selected' ? Array.wrap(notified_projects_ids).reject(&:blank?) : [])
+      members.update_all(:mail_notification => false)
+      members.where(:project_id => ids).update_all(:mail_notification => true) if ids.any?
+    end
   end
+  private :update_notified_project_ids
 
   def valid_notification_options
     self.class.valid_notification_options(self)
@@ -550,6 +561,7 @@ class User < Principal
     'lastname',
     'mail',
     'mail_notification',
+    'notified_project_ids',
     'language',
     'custom_field_values',
     'custom_fields',
index a31f6304babae88a3f6dd65e85753773ef015aa0..51024fb9a65b95b0ad574b3e6adeba6d1d0433e8 100644 (file)
   <%= render_project_nested_lists(@user.projects) do |project|
         content_tag('label', 
           check_box_tag(
-            'notified_project_ids[]',
+            'user[notified_project_ids][]',
              project.id,
              @user.notified_projects_ids.include?(project.id)
           ) + ' ' + h(project.name)
         )
       end %>
+  <%= hidden_field_tag 'user[notified_project_ids][]', '' %>
   <p><em class="info"><%= l(:text_user_mail_option) %></em></p>
 <% end %>
 
index bf8f444af833a3b34e879ca93185b6a9f93bcf3d..5959da10ea891fefdace9072fda3ce201222f437 100644 (file)
@@ -364,17 +364,13 @@ class UsersControllerTest < ActionController::TestCase
     u = User.find(2)
     assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort
     assert_equal [1, 2, 5], u.notified_projects_ids.sort
-    assert_tag :tag => 'input',
-               :attributes => {
-                  :id    => 'notified_project_ids_',
-                  :value => 1,
-                }
+    assert_select 'input[name=?][value=?]', 'user[notified_project_ids][]', '1'
     assert_equal 'all', u.mail_notification
     put :update, :id => 2,
         :user => {
-           :mail_notification => 'selected',
-         },
-        :notified_project_ids => [1, 2]
+          :mail_notification => 'selected',
+          :notified_project_ids => [1, 2]
+        }
     u = User.find(2)
     assert_equal 'selected', u.mail_notification
     assert_equal [1, 2], u.notified_projects_ids.sort