Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2016-03-29 14:52:42 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2016-03-30 11:44:20 +0300
commit71e7b398431506c8bac2e8e6014b0f3891a41f95 (patch)
tree31d30ecd7d71de5614cc79e5213789cb3c5f7539 /app
parent630c86a7a36cee36ed6b9c93644a6cb51e2b3b23 (diff)
Refactor creating notification setting with defaults
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app')
-rw-r--r--app/controllers/projects_controller.rb7
-rw-r--r--app/models/member.rb7
-rw-r--r--app/models/notification_setting.rb11
3 files changed, 13 insertions, 12 deletions
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index 77122f59128..e2dc6309d26 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -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
diff --git a/app/models/member.rb b/app/models/member.rb
index cbcc54c0250..747d0f16d8d 100644
--- a/app/models/member.rb
+++ b/app/models/member.rb
@@ -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
diff --git a/app/models/notification_setting.rb b/app/models/notification_setting.rb
index 287862a01bc..13a8995b036 100644
--- a/app/models/notification_setting.rb
+++ b/app/models/notification_setting.rb
@@ -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