2016-03-28 07:41:00 -04:00
|
|
|
class NotificationSetting < ActiveRecord::Base
|
2016-04-11 09:23:40 -04:00
|
|
|
enum level: { disabled: 0, participating: 1, watch: 2, global: 3, mention: 4 }
|
2016-04-08 16:24:27 -04:00
|
|
|
|
|
|
|
default_value_for :level, NotificationSetting.levels[:global]
|
|
|
|
|
2016-03-28 07:41:00 -04: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 12:17:42 -04:00
|
|
|
|
|
|
|
scope :for_groups, -> { where(source_type: 'Namespace') }
|
|
|
|
scope :for_projects, -> { where(source_type: 'Project') }
|
|
|
|
|
2016-03-29 07:52:42 -04: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 07:41:00 -04:00
|
|
|
end
|