Skip to content

Commit

Permalink
Fix updating rich text via nested attributes
Browse files Browse the repository at this point in the history
Closes rails#35159.
  • Loading branch information
georgeclaghorn authored Mar 17, 2019
1 parent 7971fc4 commit c399f7d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
7 changes: 2 additions & 5 deletions actiontext/lib/action_text/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ def #{name}=(body)
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
Expand Down
3 changes: 3 additions & 0 deletions actiontext/test/dummy/app/models/message.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Message < ApplicationRecord
has_rich_text :content
has_rich_text :body

has_one :review
accepts_nested_attributes_for :review
end
5 changes: 5 additions & 0 deletions actiontext/test/dummy/app/models/review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Review < ApplicationRecord
belongs_to :message

has_rich_text :content
end
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion actiontext/test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,5 +61,11 @@
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
16 changes: 15 additions & 1 deletion actiontext/test/unit/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c399f7d

Please sign in to comment.