Use default_value_for to set default NotificationSetting#level

This commit is contained in:
Douglas Barbosa Alexandre 2016-04-08 17:24:27 -03:00
parent 069724cef5
commit ee497599cc
2 changed files with 6 additions and 9 deletions

View File

@ -106,7 +106,6 @@ class ProjectsController < Projects::ApplicationController
if @membership
@notification_setting = current_user.notification_settings.find_or_initialize_by(source: @project)
@notification_setting.set_defaults unless @notification_setting.persisted?
end
end

View File

@ -1,4 +1,10 @@
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 :source, polymorphic: true
@ -8,9 +14,6 @@ class NotificationSetting < ActiveRecord::Base
validates :user_id, uniqueness: { scope: [:source_type, :source_id],
message: "already exists in source",
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_projects, -> { where(source_type: 'Project') }
@ -19,14 +22,9 @@ class NotificationSetting < ActiveRecord::Base
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
end