Reuse `User#notification_settings_for` when it's possible

parent 9a44d697
class Groups::NotificationSettingsController < Groups::ApplicationController class Groups::NotificationSettingsController < Groups::ApplicationController
def update def update
notification_setting = group.notification_settings.find_by(user_id: current_user) notification_setting = current_user.notification_settings_for(group)
saved = notification_setting.update_attributes(notification_setting_params) saved = notification_setting.update_attributes(notification_setting_params)
render json: { saved: saved } render json: { saved: saved }
......
class Projects::NotificationSettingsController < Projects::ApplicationController class Projects::NotificationSettingsController < Projects::ApplicationController
def create def create
notification_setting = project.notification_settings.new(notification_setting_params) notification_setting = current_user.notification_settings_for(project)
notification_setting.user = current_user saved = notification_setting.update_attributes(notification_setting_params)
saved = notification_setting.save
render json: { saved: saved } render json: { saved: saved }
end end
def update def update
notification_setting = project.notification_settings.find_by(user_id: current_user) notification_setting = current_user.notification_settings_for(project)
saved = notification_setting.update_attributes(notification_setting_params) saved = notification_setting.update_attributes(notification_setting_params)
render json: { saved: saved } render json: { saved: saved }
......
...@@ -167,7 +167,7 @@ class Member < ActiveRecord::Base ...@@ -167,7 +167,7 @@ class Member < ActiveRecord::Base
end end
def notification_setting def notification_setting
@notification_setting ||= user.notification_settings.find_by(source: source) @notification_setting ||= user.notification_settings_for(source)
end end
private private
......
...@@ -355,10 +355,10 @@ class NotificationService ...@@ -355,10 +355,10 @@ class NotificationService
users.reject do |user| users.reject do |user|
next user.notification_level == level unless project next user.notification_level == level unless project
setting = user.notification_settings.find_by(source: project) setting = user.notification_settings_for(project)
if !setting && project.group if !setting && project.group
setting = user.notification_settings.find_by(source: project.group) setting = user.notification_settings_for(project.group)
end end
# reject users who globally set mention notification and has no setting per project/group # reject users who globally set mention notification and has no setting per project/group
......
...@@ -89,8 +89,8 @@ describe NotificationService, services: true do ...@@ -89,8 +89,8 @@ describe NotificationService, services: true do
note.project.group.add_user(@u_watcher, GroupMember::MASTER) note.project.group.add_user(@u_watcher, GroupMember::MASTER)
note.project.save note.project.save
@u_watcher.notification_settings.find_by(source: note.project).participating! @u_watcher.notification_settings_for(note.project).participating!
@u_watcher.notification_settings.find_by(source: note.project.group).global! @u_watcher.notification_settings_for(note.project.group).global!
ActionMailer::Base.deliveries.clear ActionMailer::Base.deliveries.clear
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment