-
Notifications
You must be signed in to change notification settings - Fork 32
/
user_notification_decorator.rb
39 lines (31 loc) · 971 Bytes
/
user_notification_decorator.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
class UserNotificationDecorator < BaseDecorator
def initialize(user_notification)
@object = user_notification
end
delegate :seen!, to: :object
def subject_name
fetch_relation(:subject)
end
def translated_action
object_module = @object.action.split('.').first
I18n.t("#{object_module}.events.#{@object.action.tr('.', '_')}", subject_name: subject_name)
end
def active?
@object.subject.present? && @object.subject.respond_to?(:active?) && @object.subject.active?
end
def url
@object.subject.to_url(host: @object.site.domain)
end
private
def fetch_relation(relation_name)
if object.send(relation_name).present?
object.send(relation_name).try(:name) || object.send(relation_name).try(:title)
else
if object.send("#{relation_name}_type").present?
"(deleted) #{object.send("#{relation_name}_type")} - #{object.send("#{relation_name}_id")}"
else
"-"
end
end
end
end