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
diff options
context:
space:
mode:
authorFelipe Artur <felipefac@gmail.com>2016-05-31 23:51:46 +0300
committerFelipe Artur <felipefac@gmail.com>2016-06-15 17:47:50 +0300
commit89a2c8730b5d1f69703b0879d5b6b7bd5d766695 (patch)
tree456c480b86f74c4f47f334def338dbba527e9031 /app/models/notification_setting.rb
parent8bfbafbb6b2166d3709187cf6b1cb7ff5f627d52 (diff)
Implement custom notification level options
Diffstat (limited to 'app/models/notification_setting.rb')
-rw-r--r--app/models/notification_setting.rb37
1 files changed, 36 insertions, 1 deletions
diff --git a/app/models/notification_setting.rb b/app/models/notification_setting.rb
index 0ce87968e46..d41fc7073c6 100644
--- a/app/models/notification_setting.rb
+++ b/app/models/notification_setting.rb
@@ -1,5 +1,5 @@
class NotificationSetting < ActiveRecord::Base
- enum level: { global: 3, watch: 2, mention: 4, participating: 1, disabled: 0 }
+ enum level: { global: 3, watch: 2, mention: 4, participating: 1, disabled: 0, custom: 5 }
default_value_for :level, NotificationSetting.levels[:global]
@@ -15,6 +15,24 @@ class NotificationSetting < ActiveRecord::Base
scope :for_groups, -> { where(source_type: 'Namespace') }
scope :for_projects, -> { where(source_type: 'Project') }
+ EMAIL_EVENTS = [
+ :new_note,
+ :new_issue,
+ :reopen_issue,
+ :close_issue,
+ :reassign_issue,
+ :new_merge_request,
+ :reopen_merge_request,
+ :close_merge_request,
+ :reassign_merge_request,
+ :merge_merge_request
+ ]
+
+ store :events, accessors: EMAIL_EVENTS, coder: JSON
+
+ before_create :set_events
+ before_save :events_to_boolean
+
def self.find_or_create_for(source)
setting = find_or_initialize_by(source: source)
@@ -24,4 +42,21 @@ class NotificationSetting < ActiveRecord::Base
setting
end
+
+ # Set all event attributes to false when level is not custom or being initialized for UX reasons
+ def set_events
+ return if custom?
+
+ EMAIL_EVENTS.each do |event|
+ events[event] = false
+ end
+ end
+
+ # Validates store accessors values as boolean
+ # It is a text field so it does not cast correct boolean values in JSON
+ def events_to_boolean
+ EMAIL_EVENTS.each do |event|
+ events[event] = ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(events[event])
+ end
+ end
end