File tree Expand file tree Collapse file tree 3 files changed +23
-2
lines changed Expand file tree Collapse file tree 3 files changed +23
-2
lines changed Original file line number Diff line number Diff line change
1
+ * Change ` ActionText::RichText#embeds ` assignment from ` before_save ` to ` before_validation `
2
+
3
+ * Sean Doyle*
1
4
2
5
Please check [ 8-0-stable] ( https://github.com/rails/rails/blob/8-0-stable/actiontext/CHANGELOG.md ) for previous changes.
Original file line number Diff line number Diff line change @@ -48,10 +48,13 @@ class RichText < Record
48
48
##
49
49
# :method: embeds
50
50
#
51
- # Returns the `ActiveStorage::Blob`s of the embedded files.
51
+ # Returns the `ActiveStorage::Attachment` records from the embedded files.
52
+ #
53
+ # Attached `ActiveStorage::Blob` records are extracted from the `body`
54
+ # in a # [before_validation](/classes/ActiveModel/Validations/Callbacks/ClassMethods.html#method-i-before_validation) callback.
52
55
has_many_attached :embeds
53
56
54
- before_save do
57
+ before_validation do
55
58
self . embeds = body . attachables . grep ( ActiveStorage ::Blob ) . uniq if body . present?
56
59
end
57
60
Original file line number Diff line number Diff line change @@ -61,6 +61,21 @@ class ActionText::ModelTest < ActiveSupport::TestCase
61
61
end
62
62
end
63
63
64
+ test "embed extraction occurs before validation" do
65
+ blob = create_file_blob ( filename : "racecar.jpg" , content_type : "image/jpeg" )
66
+ content = ActionText ::Content . new . append_attachables ( blob )
67
+ message = Message . build ( subject : "Greetings" , content : content )
68
+
69
+ assert_changes -> { message . content . embeds . empty? } , from : true , to : false do
70
+ message . content . validate
71
+ end
72
+
73
+ embeds = message . content . embeds
74
+ assert_kind_of ActiveStorage ::Attached ::Many , embeds
75
+ assert_kind_of ActiveStorage ::Attachment , embeds . first
76
+ assert_equal blob , embeds . first . blob
77
+ end
78
+
64
79
test "saving content" do
65
80
message = Message . create! ( subject : "Greetings" , content : "<h1>Hello world</h1>" )
66
81
assert_equal "Hello world" , message . content . to_plain_text
You can’t perform that action at this time.
0 commit comments