parent
7971fc4b49
commit
c399f7d07a
|
@ -34,14 +34,11 @@ module ActionText
|
|||
end
|
||||
CODE
|
||||
|
||||
has_one :"rich_text_#{name}", -> { where(name: name) }, class_name: "ActionText::RichText", as: :record, inverse_of: :record, dependent: :destroy
|
||||
has_one :"rich_text_#{name}", -> { where(name: name) },
|
||||
class_name: "ActionText::RichText", as: :record, inverse_of: :record, autosave: true, dependent: :destroy
|
||||
|
||||
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 do
|
||||
public_send(name).save if public_send(name).changed?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
class Message < ApplicationRecord
|
||||
has_rich_text :content
|
||||
has_rich_text :body
|
||||
|
||||
has_one :review
|
||||
accepts_nested_attributes_for :review
|
||||
end
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
class Review < ApplicationRecord
|
||||
belongs_to :message
|
||||
|
||||
has_rich_text :content
|
||||
end
|
|
@ -0,0 +1,8 @@
|
|||
class CreateReviews < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
create_table :reviews do |t|
|
||||
t.belongs_to :message, null: false
|
||||
t.string :author_name, null: false
|
||||
end
|
||||
end
|
||||
end
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2019_03_05_172303) do
|
||||
ActiveRecord::Schema.define(version: 2019_03_17_200724) do
|
||||
|
||||
create_table "action_text_rich_texts", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
|
@ -61,5 +61,11 @@ ActiveRecord::Schema.define(version: 2019_03_05_172303) do
|
|||
t.datetime "updated_at", precision: 6, null: false
|
||||
end
|
||||
|
||||
create_table "reviews", force: :cascade do |t|
|
||||
t.integer "message_id", null: false
|
||||
t.string "author_name", null: false
|
||||
t.index ["message_id"], name: "index_reviews_on_message_id"
|
||||
end
|
||||
|
||||
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
||||
end
|
||||
|
|
|
@ -49,8 +49,22 @@ class ActionText::ModelTest < ActiveSupport::TestCase
|
|||
assert_equal "Hello world", message.content.to_plain_text
|
||||
end
|
||||
|
||||
test "save body" do
|
||||
test "saving body" do
|
||||
message = Message.create(subject: "Greetings", body: "<h1>Hello world</h1>")
|
||||
assert_equal "Hello world", message.body.to_plain_text
|
||||
end
|
||||
|
||||
test "saving content via nested attributes" do
|
||||
message = Message.create! subject: "Greetings", content: "<h1>Hello world</h1>",
|
||||
review_attributes: { author_name: "Marcia", content: "Nice work!" }
|
||||
assert_equal "Nice work!", message.review.content.to_plain_text
|
||||
end
|
||||
|
||||
test "updating content via nested attributes" do
|
||||
message = Message.create! subject: "Greetings", content: "<h1>Hello world</h1>",
|
||||
review_attributes: { author_name: "Marcia", content: "Nice work!" }
|
||||
|
||||
message.update! review_attributes: { id: message.review.id, content: "Great work!" }
|
||||
assert_equal "Great work!", message.review.reload.content.to_plain_text
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue