-
Notifications
You must be signed in to change notification settings - Fork 10
/
content_change_presenter.rb
85 lines (64 loc) · 1.71 KB
/
content_change_presenter.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
require "redcarpet/render_strip"
class ContentChangePresenter
include Callable
EMAIL_DATE_FORMAT = "%l:%M%P, %-d %B %Y".freeze
def initialize(content_change, subscription)
@content_change = content_change
@subscription = subscription
end
def call
[
title_markdown,
description_markdown,
change_note_markdown.rstrip,
footnote_markdown,
].compact.join("\n\n")
end
private
attr_reader :content_change, :subscription
delegate :title, :description, :change_note, :footnote, to: :content_change
def content_url
PublicUrls.url_for(
base_path: content_change.base_path,
utm_source: content_change.id,
utm_content: subscription.frequency,
)
end
def public_updated_at_header
I18n.t!("emails.public_updated_at_header")
end
def public_updated_at
content_change.public_updated_at.strftime(EMAIL_DATE_FORMAT).strip
end
def strip_markdown(string)
markdown_stripper.render(string).gsub(/$\n/, "")
end
def markdown_stripper
@markdown_stripper ||= Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
end
def title_markdown
"# [#{title}](#{content_url})"
end
def description_header
I18n.t!("emails.description_header")
end
def description_markdown
return nil if description.blank?
"#{description_header}\n#{strip_markdown(description)}"
end
def change_note_header
I18n.t!("emails.change_note_header")
end
def change_note_markdown
<<~CHANGE_NOTE
#{change_note_header}
#{strip_markdown(change_note)}
#{public_updated_at_header}
#{public_updated_at}
CHANGE_NOTE
end
def footnote_markdown
return nil if footnote.blank?
strip_markdown(footnote)
end
end