BigW Consortium Gitlab

notification_setting.rb 814 Bytes
Newer Older
1
class NotificationSetting < ActiveRecord::Base
2
  enum level: { global: 3, watch: 2, mention: 4, participating: 1, disabled: 0 }
3 4 5

  default_value_for :level, NotificationSetting.levels[:global]

6 7 8 9 10 11 12 13
  belongs_to :user
  belongs_to :source, polymorphic: true

  validates :user, presence: true
  validates :level, presence: true
  validates :user_id, uniqueness: { scope: [:source_type, :source_id],
                                    message: "already exists in source",
                                    allow_nil: true }
14 15 16 17

  scope :for_groups, -> { where(source_type: 'Namespace') }
  scope :for_projects, -> { where(source_type: 'Project') }

18 19 20 21 22 23 24 25 26
  def self.find_or_create_for(source)
    setting = find_or_initialize_by(source: source)

    unless setting.persisted?
      setting.save
    end

    setting
  end
27
end