Class: SentNotification

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/sent_notification.rb

Overview

Schema Information

Table name: sent_notifications

id            :integer          not null, primary key
project_id    :integer
noteable_id   :integer
noteable_type :string
recipient_id  :integer
commit_id     :string
reply_key     :string           not null
line_code     :string

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(reply_key) ⇒ Object


31
32
33
# File 'app/models/sent_notification.rb', line 31

def for(reply_key)
  find_by(reply_key: reply_key)
end

.record(noteable, recipient_id, reply_key, params = {}) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/sent_notification.rb', line 35

def record(noteable, recipient_id, reply_key, params = {})
  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

  params.reverse_merge!(
    project:        noteable.project,
    noteable_type:  noteable.class.name,
    noteable_id:    noteable_id,
    commit_id:      commit_id,
    recipient_id:   recipient_id,
    reply_key:      reply_key
  )

  create(params)
end

.record_note(note, recipient_id, reply_key, params = {}) ⇒ Object


58
59
60
61
62
# File 'app/models/sent_notification.rb', line 58

def record_note(note, recipient_id, reply_key, params = {})
  params[:line_code] = note.line_code

  record(note.noteable, recipient_id, reply_key, params)
end

.reply_keyObject


27
28
29
# File 'app/models/sent_notification.rb', line 27

def reply_key
  SecureRandom.hex(16)
end

Instance Method Details

#for_commit?Boolean

Returns:

  • (Boolean)

69
70
71
# File 'app/models/sent_notification.rb', line 69

def for_commit?
  noteable_type == "Commit"
end

#noteableObject


73
74
75
76
77
78
79
# File 'app/models/sent_notification.rb', line 73

def noteable
  if for_commit?
    project.commit(commit_id) rescue nil
  else
    super
  end
end

#to_paramObject


81
82
83
# File 'app/models/sent_notification.rb', line 81

def to_param
  self.reply_key
end

#unsubscribable?Boolean

Returns:

  • (Boolean)

65
66
67
# File 'app/models/sent_notification.rb', line 65

def unsubscribable?
  !for_commit?
end