3
3
require "active_support/core_ext/object/try"
4
4
5
5
module ActionText
6
+ # = Action Text \Attachment
7
+ #
8
+ # Attachments serialize attachables to HTML or plain text.
9
+ #
10
+ # class Person < ApplicationRecord
11
+ # include ActionText::Attachable
12
+ # end
13
+ #
14
+ # attachable = Person.create! name: "Javan"
15
+ # attachment = ActionText::Attachment.from_attachable(attachable)
16
+ # attachment.to_html # => "<action-text-attachment sgid=\"BAh7CEk..."
6
17
class Attachment
7
18
include Attachments ::TrixConversion , Attachments ::Minification , Attachments ::Caching
8
19
@@ -69,6 +80,31 @@ def with_full_attributes
69
80
self . class . from_attributes ( full_attributes , attachable )
70
81
end
71
82
83
+ # Converts the attachment to plain text.
84
+ #
85
+ # attachable = ActiveStorage::Blob.find_by filename: "racecar.jpg"
86
+ # attachment = ActionText::Attachment.from_attachable(attachable)
87
+ # attachment.to_plain_text # => "[racecar.jpg]"
88
+ #
89
+ # Use the +caption+ when set:
90
+ #
91
+ # attachment = ActionText::Attachment.from_attachable(attachable, caption: "Vroom vroom")
92
+ # attachment.to_plain_text # => "[Vroom vroom]"
93
+ #
94
+ # The presentation can be overridden by implementing the
95
+ # +attachable_plain_text_representation+ method:
96
+ #
97
+ # class Person < ApplicationRecord
98
+ # include ActionText::Attachable
99
+ #
100
+ # def attachable_plain_text_representation
101
+ # "[#{name}]"
102
+ # end
103
+ # end
104
+ #
105
+ # attachable = Person.create! name: "Javan"
106
+ # attachment = ActionText::Attachment.from_attachable(attachable)
107
+ # attachment.to_plain_text # => "[Javan]"
72
108
def to_plain_text
73
109
if respond_to? ( :attachable_plain_text_representation )
74
110
attachable_plain_text_representation ( caption )
@@ -77,6 +113,11 @@ def to_plain_text
77
113
end
78
114
end
79
115
116
+ # Converts the attachment to HTML.
117
+ #
118
+ # attachable = Person.create! name: "Javan"
119
+ # attachment = ActionText::Attachment.from_attachable(attachable)
120
+ # attachment.to_html # => "<action-text-attachment sgid=\"BAh7CEk...
80
121
def to_html
81
122
HtmlConversion . node_to_html ( node )
82
123
end
0 commit comments