gitlab-org--gitlab-foss/app/models/sent_notification.rb

64 lines
1.4 KiB
Ruby
Raw Normal View History

2015-09-06 10:48:48 -04:00
# == Schema Information
#
# Table name: sent_notifications
#
# id :integer not null, primary key
# project_id :integer
# noteable_id :integer
# noteable_type :string(255)
# recipient_id :integer
# commit_id :string(255)
# reply_key :string(255) not null
#
2015-08-18 18:46:36 -04:00
class SentNotification < ActiveRecord::Base
belongs_to :project
belongs_to :noteable, polymorphic: true
belongs_to :recipient, class_name: "User"
validate :project, :recipient, :reply_key, presence: true
validate :reply_key, uniqueness: true
2015-08-18 20:02:26 -04:00
validates :noteable_id, presence: true, unless: :for_commit?
validates :commit_id, presence: true, if: :for_commit?
2015-08-18 18:46:36 -04:00
class << self
def for(reply_key)
find_by(reply_key: reply_key)
end
def record(noteable, recipient_id, reply_key)
return unless reply_key
noteable_id = nil
commit_id = nil
if noteable.is_a?(Commit)
commit_id = noteable.id
else
noteable_id = noteable.id
end
create(
project: noteable.project,
noteable_type: noteable.class.name,
noteable_id: noteable_id,
commit_id: commit_id,
recipient_id: recipient_id,
reply_key: reply_key
)
end
2015-08-18 18:46:36 -04:00
end
def for_commit?
noteable_type == "Commit"
end
def noteable
if for_commit?
2015-08-18 20:02:26 -04:00
project.commit(commit_id) rescue nil
2015-08-18 18:46:36 -04:00
else
super
end
end
end