mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
3431c0b3ee
commit
531d7dd584
6 changed files with 6 additions and 27 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue