Refactor creating notification setting with defaults
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
parent
630c86a7a3
commit
71e7b39843
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue