提交 e791736a 编写于 作者: G George Claghorn

Deduplicate ActionText::RichText embeds

Fix that an ActiveRecord::RecordNotUnique error would be raised when saving rich-text content with the same file attached multiple times.
上级 5686c11c
......@@ -15,7 +15,7 @@ class RichText < ActiveRecord::Base
has_many_attached :embeds
before_save do
self.embeds = body.attachables.grep(ActiveStorage::Blob) if body.present?
self.embeds = body.attachables.grep(ActiveStorage::Blob).uniq if body.present?
end
def to_plain_text
......
......@@ -44,6 +44,15 @@ class ActionText::ModelTest < ActiveSupport::TestCase
assert_equal [ActiveStorage::Attachment], message.content.embeds.map(&:class)
end
test "embed extraction deduplicates file attachments" do
blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg")
content = ActionText::Content.new("Hello world").append_attachables([ blob, blob ])
assert_nothing_raised do
Message.create!(subject: "Greetings", content: content)
end
end
test "saving content" do
message = Message.create!(subject: "Greetings", content: "<h1>Hello world</h1>")
assert_equal "Hello world", message.content.to_plain_text
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册