summaryrefslogtreecommitdiff
path: root/app/models/sent_notification.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/sent_notification.rb')
-rw-r--r--app/models/sent_notification.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/app/models/sent_notification.rb b/app/models/sent_notification.rb
new file mode 100644
index 00000000000..a3d24669b52
--- /dev/null
+++ b/app/models/sent_notification.rb
@@ -0,0 +1,29 @@
+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
+
+ validates :noteable_id, presence: true, if: ->(n) { n.noteable_type.present? && n.noteable_type != 'Commit' }
+ validates :commit_id, presence: true, if: ->(n) { n.noteable_type == 'Commit' }
+
+ def self.for(reply_key)
+ find_by(reply_key: reply_key)
+ end
+
+ def for_commit?
+ noteable_type == "Commit"
+ end
+
+ def noteable
+ if for_commit?
+ project.commit(commit_id)
+ else
+ super
+ end
+ rescue
+ nil
+ end
+end