Skip to content

Commit f1c553c

Browse files
authored
Merge pull request rails#48137 from p8/actiontext/document-actiontext-attachment
Add documentation for ActionText::Attachment [ci-skip]
2 parents 6a665a5 + 344bb00 commit f1c553c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

actiontext/lib/action_text/attachment.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
require "active_support/core_ext/object/try"
44

55
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..."
617
class Attachment
718
include Attachments::TrixConversion, Attachments::Minification, Attachments::Caching
819

@@ -69,6 +80,31 @@ def with_full_attributes
6980
self.class.from_attributes(full_attributes, attachable)
7081
end
7182

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]"
72108
def to_plain_text
73109
if respond_to?(:attachable_plain_text_representation)
74110
attachable_plain_text_representation(caption)
@@ -77,6 +113,11 @@ def to_plain_text
77113
end
78114
end
79115

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...
80121
def to_html
81122
HtmlConversion.node_to_html(node)
82123
end

0 commit comments

Comments
 (0)