aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/action_text/rich_text.rb4
-rw-r--r--db/migrate/201805281641_create_action_text_tables.rb2
-rw-r--r--lib/action_text/attribute.rb13
-rw-r--r--test/dummy/db/migrate/2018052816_create_action_text_tables.rb2
-rw-r--r--test/dummy/db/schema.rb2
-rw-r--r--test/unit/content_test.rb10
6 files changed, 6 insertions, 27 deletions
diff --git a/app/models/action_text/rich_text.rb b/app/models/action_text/rich_text.rb
index 445b444867..0b6043bd74 100644
--- a/app/models/action_text/rich_text.rb
+++ b/app/models/action_text/rich_text.rb
@@ -6,10 +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)
+ self.embeds = body.attachments.map(&:attachable) if body.present?
end
def to_s
diff --git a/db/migrate/201805281641_create_action_text_tables.rb b/db/migrate/201805281641_create_action_text_tables.rb
index 872987749a..ed97ad46c0 100644
--- a/db/migrate/201805281641_create_action_text_tables.rb
+++ b/db/migrate/201805281641_create_action_text_tables.rb
@@ -2,7 +2,7 @@ class CreateActionTextTables < ActiveRecord::Migration[5.2]
def change
create_table :action_text_rich_texts do |t|
t.string :name, null: false
- t.text :body, limit: 16777215, null: false
+ t.text :body, limit: 16777215
t.references :record, null: false, polymorphic: true, index: false
t.datetime :created_at, null: false
diff --git a/lib/action_text/attribute.rb b/lib/action_text/attribute.rb
index 67ade42eb1..8426726a38 100644
--- a/lib/action_text/attribute.rb
+++ b/lib/action_text/attribute.rb
@@ -37,19 +37,8 @@ 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 }) }
- 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
+ public_send(name).save if public_send(name).changed?
end
end
end
diff --git a/test/dummy/db/migrate/2018052816_create_action_text_tables.rb b/test/dummy/db/migrate/2018052816_create_action_text_tables.rb
index 872987749a..ed97ad46c0 100644
--- a/test/dummy/db/migrate/2018052816_create_action_text_tables.rb
+++ b/test/dummy/db/migrate/2018052816_create_action_text_tables.rb
@@ -2,7 +2,7 @@ class CreateActionTextTables < ActiveRecord::Migration[5.2]
def change
create_table :action_text_rich_texts do |t|
t.string :name, null: false
- t.text :body, limit: 16777215, null: false
+ t.text :body, limit: 16777215
t.references :record, null: false, polymorphic: true, index: false
t.datetime :created_at, null: false
diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb
index 30bf844e9c..5179d7269f 100644
--- a/test/dummy/db/schema.rb
+++ b/test/dummy/db/schema.rb
@@ -14,7 +14,7 @@ ActiveRecord::Schema.define(version: 2018_02_12_164506) do
create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
- t.text "body", limit: 16777215, null: false
+ t.text "body", limit: 16777215
t.string "record_type", null: false
t.integer "record_id", null: false
t.datetime "created_at", null: false
diff --git a/test/unit/content_test.rb b/test/unit/content_test.rb
index 60fb39610b..59beb323cf 100644
--- a/test/unit/content_test.rb
+++ b/test/unit/content_test.rb
@@ -7,19 +7,11 @@ 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
+ test "without content" 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))