summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2016-03-28 23:22:28 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2016-03-30 10:44:20 +0200
commit4ca73f56cb59b86f25b55ff02800571fb82c742f (patch)
tree6c2354e732e9efc95ef8138bc4cf3d0004557917
parentb8f38437900cdddac9d19d5c48a2a8e5bb037f41 (diff)
downloadgitlab-ce-4ca73f56cb59b86f25b55ff02800571fb82c742f.tar.gz
Small refactoring and cleanup of notification logic
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r--app/helpers/notifications_helper.rb12
-rw-r--r--app/models/member.rb2
-rw-r--r--app/models/members/group_member.rb1
-rw-r--r--app/models/members/project_member.rb1
-rw-r--r--app/models/notification.rb46
-rw-r--r--spec/models/notification_setting_spec.rb1
-rw-r--r--spec/services/notification_service_spec.rb9
7 files changed, 8 insertions, 64 deletions
diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb
index a0e91a63d2d..8816cc5d164 100644
--- a/app/helpers/notifications_helper.rb
+++ b/app/helpers/notifications_helper.rb
@@ -22,16 +22,12 @@ module NotificationsHelper
def notification_title(level)
case level.to_sym
- when :disabled
- 'Disabled'
when :participating
'Participate'
- when :watch
- 'Watch'
when :mention
'On mention'
- when :global
- 'Global'
+ else
+ level.to_s.titlecase
end
end
@@ -50,10 +46,6 @@ module NotificationsHelper
end
end
- def notification_label(setting)
- notification_title(setting.level)
- end
-
def active_level_for(setting, level)
'active' if setting.level == level
end
diff --git a/app/models/member.rb b/app/models/member.rb
index e665ba6fb75..799f28c3fdf 100644
--- a/app/models/member.rb
+++ b/app/models/member.rb
@@ -62,6 +62,8 @@ class Member < ActiveRecord::Base
delegate :name, :username, :email, to: :user, prefix: true
+ default_value_for :notification_level, NotificationSetting.levels[:global]
+
class << self
def find_by_invite_token(invite_token)
invite_token = Devise.token_generator.digest(self, :invite_token, invite_token)
diff --git a/app/models/members/group_member.rb b/app/models/members/group_member.rb
index 65d2ea00570..9fb474a1a93 100644
--- a/app/models/members/group_member.rb
+++ b/app/models/members/group_member.rb
@@ -24,7 +24,6 @@ class GroupMember < Member
# Make sure group member points only to group as it source
default_value_for :source_type, SOURCE_TYPE
- default_value_for :notification_level, Notification::N_GLOBAL
validates_format_of :source_type, with: /\ANamespace\z/
default_scope { where(source_type: SOURCE_TYPE) }
diff --git a/app/models/members/project_member.rb b/app/models/members/project_member.rb
index 560d1690e14..07ddb02ae9d 100644
--- a/app/models/members/project_member.rb
+++ b/app/models/members/project_member.rb
@@ -27,7 +27,6 @@ class ProjectMember < Member
# Make sure project member points only to project as it source
default_value_for :source_type, SOURCE_TYPE
- default_value_for :notification_level, Notification::N_GLOBAL
validates_format_of :source_type, with: /\AProject\z/
default_scope { where(source_type: SOURCE_TYPE) }
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 8a90b456cc2..3805bde88b0 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -1,35 +1,6 @@
class Notification
- #
- # Notification levels
- #
- N_DISABLED = 0
- N_PARTICIPATING = 1
- N_WATCH = 2
- N_GLOBAL = 3
- N_MENTION = 4
-
attr_accessor :target
- class << self
- def notification_levels
- [N_DISABLED, N_MENTION, N_PARTICIPATING, N_WATCH]
- end
-
- def options_with_labels
- {
- disabled: N_DISABLED,
- participating: N_PARTICIPATING,
- watch: N_WATCH,
- mention: N_MENTION,
- global: N_GLOBAL
- }
- end
-
- def project_notification_levels
- [N_DISABLED, N_MENTION, N_PARTICIPATING, N_WATCH, N_GLOBAL]
- end
- end
-
delegate :disabled?, :participating?, :watch?, :global?, :mention?, to: :target
def initialize(target)
@@ -39,21 +10,4 @@ class Notification
def level
target.notification_level
end
-
- def to_s
- case level
- when N_DISABLED
- 'Disabled'
- when N_PARTICIPATING
- 'Participating'
- when N_WATCH
- 'Watching'
- when N_MENTION
- 'On mention'
- when N_GLOBAL
- 'Global'
- else
- # do nothing
- end
- end
end
diff --git a/spec/models/notification_setting_spec.rb b/spec/models/notification_setting_spec.rb
index f9d668ed75b..f31b2a3cd6f 100644
--- a/spec/models/notification_setting_spec.rb
+++ b/spec/models/notification_setting_spec.rb
@@ -3,6 +3,7 @@ require 'rails_helper'
RSpec.describe NotificationSetting, type: :model do
describe "Associations" do
it { is_expected.to belong_to(:user) }
+ it { is_expected.to belong_to(:source) }
end
describe "Validation" do
diff --git a/spec/services/notification_service_spec.rb b/spec/services/notification_service_spec.rb
index c01851a8a24..c4d52584a4b 100644
--- a/spec/services/notification_service_spec.rb
+++ b/spec/services/notification_service_spec.rb
@@ -88,12 +88,9 @@ describe NotificationService, services: true do
note.project.namespace_id = group.id
note.project.group.add_user(@u_watcher, GroupMember::MASTER)
note.project.save
- user_project = note.project.project_members.find_by_user_id(@u_watcher.id)
- user_project.notification.level = :participating
- user_project.save
- group_member = note.project.group.group_members.find_by_user_id(@u_watcher.id)
- group_member.notification.level = :global
- group_member.notification.save
+
+ @u_watcher.notification_settings.find_by(source: note.project).participating!
+ @u_watcher.notification_settings.find_by(source: note.project.group).global!
ActionMailer::Base.deliveries.clear
end