/
base.rb
92 lines (75 loc) · 2.07 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true
module Bullet
module Notification
class Base
attr_accessor :notifier, :url
attr_reader :base_class, :associations, :path
def initialize(base_class, association_or_associations, path = nil)
@base_class = base_class
@associations =
association_or_associations.is_a?(Array) ? association_or_associations : [association_or_associations]
@path = path
end
def title
raise NoMethodError, 'no method title defined'
end
def body
raise NoMethodError, 'no method body defined'
end
def call_stack_messages
''
end
def whoami
@user ||=
ENV['USER'].presence ||
(
begin
`whoami`.chomp
rescue StandardError
''
end
)
@user.present? ? "user: #{@user}" : ''
end
def body_with_caller
"#{body}\n#{call_stack_messages}\n"
end
def notify_inline
notifier.inline_notify(notification_data)
end
def notify_out_of_channel
notifier.out_of_channel_notify(notification_data)
end
def short_notice
parts = []
parts << whoami.presence unless Bullet.skip_user_in_notification
parts << url
parts << title
parts << body
parts.compact.join(' ')
end
def notification_data
hash = {}
hash[:user] = whoami unless Bullet.skip_user_in_notification
hash[:url] = url
hash[:title] = title
hash[:body] = body_with_caller
hash
end
def eql?(other)
self.class == other.class && klazz_associations_str == other.klazz_associations_str
end
def hash
[self.class, klazz_associations_str].hash
end
protected
def klazz_associations_str
" #{@base_class} => [#{@associations.map(&:inspect).join(', ')}]"
end
def associations_str
".includes(#{@associations.map { |a| a.to_s.to_sym }
.inspect})"
end
end
end
end