diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2018-09-12 15:51:06 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2018-09-12 15:51:06 -0700 |
commit | 531d7dd584267e81ba57d4de7f0fe21b18a83cbb (patch) | |
tree | 1e65cf6bda11cc03bdcebdc9183a84abb5aef58c | |
parent | 3431c0b3eeaf284901bd9aa4265c578207d4c820 (diff) | |
download | rails-531d7dd584267e81ba57d4de7f0fe21b18a83cbb.tar.gz rails-531d7dd584267e81ba57d4de7f0fe21b18a83cbb.tar.bz2 rails-531d7dd584267e81ba57d4de7f0fe21b18a83cbb.zip |
Never mind on allowing blank
More hassle than its worth. Just account for the fact that rich text can be blank instead, but continue to create the record.
-rw-r--r-- | app/models/action_text/rich_text.rb | 4 | ||||
-rw-r--r-- | db/migrate/201805281641_create_action_text_tables.rb | 2 | ||||
-rw-r--r-- | lib/action_text/attribute.rb | 13 | ||||
-rw-r--r-- | test/dummy/db/migrate/2018052816_create_action_text_tables.rb | 2 | ||||
-rw-r--r-- | test/dummy/db/schema.rb | 2 | ||||
-rw-r--r-- | test/unit/content_test.rb | 10 |
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)) |