diff options
Diffstat (limited to 'lib/action_text/attribute.rb')
-rw-r--r-- | lib/action_text/attribute.rb | 15 |
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 |