aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_text/attribute.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2018-09-12 10:58:23 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2018-09-12 10:58:23 -0700
commit3431c0b3eeaf284901bd9aa4265c578207d4c820 (patch)
treebad6de7844972fa8e39666e87f875b03676dd6a3 /lib/action_text/attribute.rb
parent0fd8ba6ffff07a4556ec47840ef8c9dbd10e5e8b (diff)
downloadrails-3431c0b3eeaf284901bd9aa4265c578207d4c820.tar.gz
rails-3431c0b3eeaf284901bd9aa4265c578207d4c820.tar.bz2
rails-3431c0b3eeaf284901bd9aa4265c578207d4c820.zip
Ensure blank rich text records aren't saved or required
Diffstat (limited to 'lib/action_text/attribute.rb')
-rw-r--r--lib/action_text/attribute.rb15
1 files changed, 14 insertions, 1 deletions
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