Use default_value_for to set default NotificationSetting#level

parent 069724ce
...@@ -106,7 +106,6 @@ class ProjectsController < Projects::ApplicationController ...@@ -106,7 +106,6 @@ class ProjectsController < Projects::ApplicationController
if @membership if @membership
@notification_setting = current_user.notification_settings.find_or_initialize_by(source: @project) @notification_setting = current_user.notification_settings.find_or_initialize_by(source: @project)
@notification_setting.set_defaults unless @notification_setting.persisted?
end end
end end
......
class NotificationSetting < ActiveRecord::Base class NotificationSetting < ActiveRecord::Base
# Notification level
# Note: When adding an option, it MUST go on the end of the array.
enum level: [:disabled, :participating, :watch, :global, :mention]
default_value_for :level, NotificationSetting.levels[:global]
belongs_to :user belongs_to :user
belongs_to :source, polymorphic: true belongs_to :source, polymorphic: true
...@@ -8,9 +14,6 @@ class NotificationSetting < ActiveRecord::Base ...@@ -8,9 +14,6 @@ class NotificationSetting < ActiveRecord::Base
validates :user_id, uniqueness: { scope: [:source_type, :source_id], validates :user_id, uniqueness: { scope: [:source_type, :source_id],
message: "already exists in source", message: "already exists in source",
allow_nil: true } allow_nil: true }
# Notification level
# Note: When adding an option, it MUST go on the end of the array.
enum level: [:disabled, :participating, :watch, :global, :mention]
scope :for_groups, -> { where(source_type: 'Namespace') } scope :for_groups, -> { where(source_type: 'Namespace') }
scope :for_projects, -> { where(source_type: 'Project') } scope :for_projects, -> { where(source_type: 'Project') }
...@@ -19,14 +22,9 @@ class NotificationSetting < ActiveRecord::Base ...@@ -19,14 +22,9 @@ class NotificationSetting < ActiveRecord::Base
setting = find_or_initialize_by(source: source) setting = find_or_initialize_by(source: source)
unless setting.persisted? unless setting.persisted?
setting.set_defaults
setting.save setting.save
end end
setting setting
end end
def set_defaults
self.level = :global
end
end 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