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
@membership = @project.team.find_member(current_user.id)
if @membership
@notification_setting = current_user.notification_settings.find_or_initialize_by(source: @project)
unless @notification_setting.persisted?
@notification_setting.set_defaults
@notification_setting.save
end
@notification_setting = current_user.notification_settings.find_or_create_for(@project)
end
end
......
......@@ -163,12 +163,7 @@ class Member < ActiveRecord::Base
end
def create_notification_setting
notification_setting = user.notification_settings.find_or_initialize_by(source: source)
unless notification_setting.persisted?
notification_setting.set_defaults
notification_setting.save
end
user.notification_setting.find_or_create_for(source)
end
def notification_setting
......
......@@ -15,6 +15,17 @@ class NotificationSetting < ActiveRecord::Base
scope :for_groups, -> { where(source_type: 'Namespace') }
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
self.level = :global
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