2016-03-28 11:41:00 +00:00
|
|
|
class NotificationSetting < ActiveRecord::Base
|
2016-04-08 20:24:27 +00:00
|
|
|
# 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]
|
|
|
|
|
2016-03-28 11:41:00 +00:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :source, polymorphic: true
|
|
|
|
|
|
|
|
validates :user, presence: true
|
|
|
|
validates :source, presence: true
|
|
|
|
validates :level, presence: true
|
|
|
|
validates :user_id, uniqueness: { scope: [:source_type, :source_id],
|
|
|
|
message: "already exists in source",
|
|
|
|
allow_nil: true }
|
2016-03-28 16:17:42 +00:00
|
|
|
|
|
|
|
scope :for_groups, -> { where(source_type: 'Namespace') }
|
|
|
|
scope :for_projects, -> { where(source_type: 'Project') }
|
|
|
|
|
2016-03-29 11:52:42 +00:00
|
|
|
def self.find_or_create_for(source)
|
|
|
|
setting = find_or_initialize_by(source: source)
|
|
|
|
|
|
|
|
unless setting.persisted?
|
|
|
|
setting.save
|
|
|
|
end
|
|
|
|
|
|
|
|
setting
|
|
|
|
end
|
2016-03-28 11:41:00 +00:00
|
|
|
end
|