diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2018-09-12 10:58:23 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2018-09-12 10:58:23 -0700 |
commit | 3431c0b3eeaf284901bd9aa4265c578207d4c820 (patch) | |
tree | bad6de7844972fa8e39666e87f875b03676dd6a3 | |
parent | 0fd8ba6ffff07a4556ec47840ef8c9dbd10e5e8b (diff) | |
download | rails-3431c0b3eeaf284901bd9aa4265c578207d4c820.tar.gz rails-3431c0b3eeaf284901bd9aa4265c578207d4c820.tar.bz2 rails-3431c0b3eeaf284901bd9aa4265c578207d4c820.zip |
Ensure blank rich text records aren't saved or required
-rw-r--r-- | app/models/action_text/rich_text.rb | 2 | ||||
-rw-r--r-- | lib/action_text/attribute.rb | 15 | ||||
-rw-r--r-- | lib/action_text/content.rb | 2 | ||||
-rw-r--r-- | test/unit/content_test.rb | 13 |
4 files changed, 30 insertions, 2 deletions
diff --git a/app/models/action_text/rich_text.rb b/app/models/action_text/rich_text.rb index 7c0fdf240d..445b444867 100644 --- a/app/models/action_text/rich_text.rb +++ b/app/models/action_text/rich_text.rb @@ -6,6 +6,8 @@ class ActionText::RichText < ActiveRecord::Base belongs_to :record, polymorphic: true, touch: true has_many_attached :embeds + validate { errors.add(:body, "is missing") if body.blank? } + before_save do self.embeds = body.attachments.map(&:attachable) end diff --git a/lib/action_text/attribute.rb b/lib/action_text/attribute.rb index 299c26b29f..67ade42eb1 100644 --- a/lib/action_text/attribute.rb +++ b/lib/action_text/attribute.rb @@ -37,7 +37,20 @@ module ActionText scope :"with_rich_text_#{name}", -> { includes("rich_text_#{name}") } scope :"with_rich_text_#{name}_and_embeds", -> { includes("rich_text_#{name}": { embeds_attachments: :blob }) } - after_save { public_send(name).save if public_send(name).changed? } + before_save do + # If there's no body set, we need to reset the rich text record such that it is not autosaved. + public_send("#{name}=", nil) if public_send(name).body.blank? + end + + after_save do + rich_text = public_send(name) + + if rich_text.changed? && rich_text.body.present? + rich_text.save + elsif rich_text.persisted? && rich_text.body.blank? + rich_text.destroy + end + end end end end diff --git a/lib/action_text/content.rb b/lib/action_text/content.rb index c296896ac4..22b52cff1c 100644 --- a/lib/action_text/content.rb +++ b/lib/action_text/content.rb @@ -4,7 +4,7 @@ module ActionText attr_reader :fragment - delegate :blank?, :empty?, :html_safe, :present?, to: :to_s + delegate :blank?, :empty?, :html_safe, :present?, to: :to_html # Delegating to to_html to avoid including the layout def initialize(content = nil) @fragment = ActionText::Attachment.fragment_by_canonicalizing_attachments(content) diff --git a/test/unit/content_test.rb b/test/unit/content_test.rb index f1acf054db..60fb39610b 100644 --- a/test/unit/content_test.rb +++ b/test/unit/content_test.rb @@ -7,6 +7,19 @@ module ActionText assert_equal "Hello world", message.content.body.to_plain_text end + test "creating a model with rich text content will not create a rich text record" do + message = Message.create!(subject: "Greetings") + assert message.content.body.nil? + end + + test "removing content removes the rich text record" do + message = Message.create!(subject: "Greetings", content: "<h1>Hello world</h1>") + + assert_difference -> { ActionText::RichText.all.count }, -1 do + message.update!(content: "") + end + end + test "embed extraction" do blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg") message = Message.create!(subject: "Greetings", content: ActionText::Content.new("Hello world").append_attachables(blob)) |