Skip to content

Commit bd8aeea

Browse files
authored
Merge pull request rails#47953 from p8/actiontext/document-rich-text-methods
Document ActionText::RichText methods [ci-skip]
2 parents 950ffb9 + e0d77a9 commit bd8aeea

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

actiontext/app/models/action_text/rich_text.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ module ActionText
77
# It also holds all the references to the embedded files, which are stored using Active Storage.
88
# This record is then associated with the Active Record model the application desires to have
99
# rich text content using the +has_rich_text+ class method.
10+
#
11+
# class Message < ActiveRecord::Base
12+
# has_rich_text :content
13+
# end
14+
#
15+
# message = Message.create!(content: "<h1>Funny times!</h1>")
16+
# message.content #=> #<ActionText::RichText....
17+
# message.content.to_s # => "<h1>Funny times!</h1>"
18+
# message.content.to_plain_text # => "Funny times!"
19+
#
1020
class RichText < Record
1121
self.table_name = "action_text_rich_texts"
1222

@@ -20,10 +30,26 @@ class RichText < Record
2030
self.embeds = body.attachables.grep(ActiveStorage::Blob).uniq if body.present?
2131
end
2232

33+
# Returns the +body+ attribute as plain text with all HTML tags removed.
34+
#
35+
# message = Message.create!(content: "<h1>Funny times!</h1>")
36+
# message.content.to_plain_text # => "Funny times!"
2337
def to_plain_text
2438
body&.to_plain_text.to_s
2539
end
2640

41+
# Returns the +body+ attribute in a format that makes it editable in the Trix
42+
# editor. Previews of attachments are rendered inline.
43+
#
44+
# content = "<h1>Funny Times!</h1><figure data-trix-attachment='{\"sgid\":\"..."\}'></figure>"
45+
# message = Message.create!(content: content)
46+
# message.content.to_trix_html # =>
47+
# # <div class="trix-content">
48+
# # <h1>Funny times!</h1>
49+
# # <figure data-trix-attachment='{\"sgid\":\"..."\}'>
50+
# # <img src="http://example.org/rails/active_storage/.../funny.jpg">
51+
# # </figure>
52+
# # </div>
2753
def to_trix_html
2854
body&.to_trix_html
2955
end

0 commit comments

Comments
 (0)