@@ -7,6 +7,16 @@ module ActionText
7
7
# It also holds all the references to the embedded files, which are stored using Active Storage.
8
8
# This record is then associated with the Active Record model the application desires to have
9
9
# 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
+ #
10
20
class RichText < Record
11
21
self . table_name = "action_text_rich_texts"
12
22
@@ -20,10 +30,26 @@ class RichText < Record
20
30
self . embeds = body . attachables . grep ( ActiveStorage ::Blob ) . uniq if body . present?
21
31
end
22
32
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!"
23
37
def to_plain_text
24
38
body &.to_plain_text . to_s
25
39
end
26
40
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>
27
53
def to_trix_html
28
54
body &.to_trix_html
29
55
end
0 commit comments