Commit 71e7b398 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor creating notification setting with defaults

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 630c86a7
...@@ -102,12 +102,7 @@ class ProjectsController < Projects::ApplicationController ...@@ -102,12 +102,7 @@ class ProjectsController < Projects::ApplicationController
@membership = @project.team.find_member(current_user.id) @membership = @project.team.find_member(current_user.id)
if @membership if @membership
@notification_setting = current_user.notification_settings.find_or_initialize_by(source: @project) @notification_setting = current_user.notification_settings.find_or_create_for(@project)
unless @notification_setting.persisted?
@notification_setting.set_defaults
@notification_setting.save
end
end end
end end
......
...@@ -163,12 +163,7 @@ class Member < ActiveRecord::Base ...@@ -163,12 +163,7 @@ class Member < ActiveRecord::Base
end end
def create_notification_setting def create_notification_setting
notification_setting = user.notification_settings.find_or_initialize_by(source: source) user.notification_setting.find_or_create_for(source)
unless notification_setting.persisted?
notification_setting.set_defaults
notification_setting.save
end
end end
def notification_setting def notification_setting
......
...@@ -15,6 +15,17 @@ class NotificationSetting < ActiveRecord::Base ...@@ -15,6 +15,17 @@ class NotificationSetting < ActiveRecord::Base
scope :for_groups, -> { where(source_type: 'Namespace') } scope :for_groups, -> { where(source_type: 'Namespace') }
scope :for_projects, -> { where(source_type: 'Project') } scope :for_projects, -> { where(source_type: 'Project') }
def self.find_or_create_for(source)
setting = find_or_initialize_by(source: source)
unless setting.persisted?
setting.set_defaults
setting.save
end
setting
end
def set_defaults def set_defaults
self.level = :global self.level = :global
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