aboutsummaryrefslogtreecommitdiffstats
path: root/actiontext
diff options
context:
space:
mode:
Diffstat (limited to 'actiontext')
-rw-r--r--actiontext/CHANGELOG.md9
-rw-r--r--actiontext/app/helpers/action_text/content_helper.rb23
-rw-r--r--actiontext/lib/action_text/attachable.rb4
-rw-r--r--actiontext/lib/action_text/attribute.rb9
-rw-r--r--actiontext/lib/action_text/gem_version.rb4
-rw-r--r--actiontext/lib/templates/installer.rb13
-rw-r--r--actiontext/package.json2
-rw-r--r--actiontext/test/dummy/app/models/message.rb3
-rw-r--r--actiontext/test/dummy/app/models/page.rb4
-rw-r--r--actiontext/test/dummy/app/models/review.rb5
-rw-r--r--actiontext/test/dummy/config/locales/en.yml2
-rw-r--r--actiontext/test/dummy/config/routes.rb2
-rw-r--r--actiontext/test/dummy/db/migrate/20190305172303_create_pages.rb9
-rw-r--r--actiontext/test/dummy/db/migrate/20190317200724_create_reviews.rb8
-rw-r--r--actiontext/test/dummy/db/schema.rb14
-rw-r--r--actiontext/test/test_helper.rb2
-rw-r--r--actiontext/test/unit/attachment_test.rb8
-rw-r--r--actiontext/test/unit/model_test.rb24
18 files changed, 114 insertions, 31 deletions
diff --git a/actiontext/CHANGELOG.md b/actiontext/CHANGELOG.md
index 203800da1f..b165f8b323 100644
--- a/actiontext/CHANGELOG.md
+++ b/actiontext/CHANGELOG.md
@@ -1,10 +1,3 @@
-## Rails 6.0.0.beta2 (February 25, 2019) ##
-* No changes.
-
-## Rails 6.0.0.beta1 (January 18, 2019) ##
-
-* Added to Rails.
-
- *DHH*
+Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/actiontext/CHANGELOG.md) for previous changes.
diff --git a/actiontext/app/helpers/action_text/content_helper.rb b/actiontext/app/helpers/action_text/content_helper.rb
index 2005033d5c..ed2887d865 100644
--- a/actiontext/app/helpers/action_text/content_helper.rb
+++ b/actiontext/app/helpers/action_text/content_helper.rb
@@ -4,20 +4,27 @@ require "rails-html-sanitizer"
module ActionText
module ContentHelper
- SANITIZER = Rails::Html::Sanitizer.white_list_sanitizer
- ALLOWED_TAGS = SANITIZER.allowed_tags + [ ActionText::Attachment::TAG_NAME, "figure", "figcaption" ]
- ALLOWED_ATTRIBUTES = SANITIZER.allowed_attributes + ActionText::Attachment::ATTRIBUTES
+ mattr_accessor(:sanitizer) { Rails::Html::Sanitizer.white_list_sanitizer.new }
+ mattr_accessor(:allowed_tags) { sanitizer.class.allowed_tags + [ ActionText::Attachment::TAG_NAME, "figure", "figcaption" ] }
+ mattr_accessor(:allowed_attributes) { sanitizer.class.allowed_attributes + ActionText::Attachment::ATTRIBUTES }
+ mattr_accessor(:scrubber)
def render_action_text_content(content)
- content = content.render_attachments do |attachment|
+ sanitize_action_text_content(render_action_text_attachments(content))
+ end
+
+ def sanitize_action_text_content(content)
+ sanitizer.sanitize(content.to_html, tags: allowed_tags, attributes: allowed_attributes, scrubber: scrubber).html_safe
+ end
+
+ def render_action_text_attachments(content)
+ content.render_attachments do |attachment|
unless attachment.in?(content.gallery_attachments)
attachment.node.tap do |node|
node.inner_html = render(attachment, in_gallery: false).chomp
end
end
- end
-
- content = content.render_attachment_galleries do |attachment_gallery|
+ end.render_attachment_galleries do |attachment_gallery|
render(layout: attachment_gallery, object: attachment_gallery) do
attachment_gallery.attachments.map do |attachment|
attachment.node.inner_html = render(attachment, in_gallery: true).chomp
@@ -25,8 +32,6 @@ module ActionText
end.join("").html_safe
end.chomp
end
-
- sanitize content.to_html, tags: ALLOWED_TAGS, attributes: ALLOWED_ATTRIBUTES
end
end
end
diff --git a/actiontext/lib/action_text/attachable.rb b/actiontext/lib/action_text/attachable.rb
index 38cd24aa8d..3343bcc308 100644
--- a/actiontext/lib/action_text/attachable.rb
+++ b/actiontext/lib/action_text/attachable.rb
@@ -67,6 +67,10 @@ module ActionText
super.merge(attachable_sgid: attachable_sgid)
end
+ def to_trix_content_attachment_partial_path
+ to_partial_path
+ end
+
def to_rich_text_attributes(attributes = {})
attributes.dup.tap do |attrs|
attrs[:sgid] = attachable_sgid
diff --git a/actiontext/lib/action_text/attribute.rb b/actiontext/lib/action_text/attribute.rb
index f226dd21bd..ddc6822a4c 100644
--- a/actiontext/lib/action_text/attribute.rb
+++ b/actiontext/lib/action_text/attribute.rb
@@ -26,7 +26,7 @@ module ActionText
def has_rich_text(name)
class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}
- self.rich_text_#{name} ||= ActionText::RichText.new(name: "#{name}", record: self)
+ rich_text_#{name} || build_rich_text_#{name}
end
def #{name}=(body)
@@ -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
diff --git a/actiontext/lib/action_text/gem_version.rb b/actiontext/lib/action_text/gem_version.rb
index da2a9212d3..8b4103712c 100644
--- a/actiontext/lib/action_text/gem_version.rb
+++ b/actiontext/lib/action_text/gem_version.rb
@@ -8,9 +8,9 @@ module ActionText
module VERSION
MAJOR = 6
- MINOR = 0
+ MINOR = 1
TINY = 0
- PRE = "beta2"
+ PRE = "alpha"
STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
end
diff --git a/actiontext/lib/templates/installer.rb b/actiontext/lib/templates/installer.rb
index a8000eb9fc..a15ada92bb 100644
--- a/actiontext/lib/templates/installer.rb
+++ b/actiontext/lib/templates/installer.rb
@@ -29,4 +29,17 @@ if APPLICATION_PACK_PATH.exist?
append_to_file APPLICATION_PACK_PATH, "\n#{line}"
end
end
+else
+ warn <<~WARNING
+ WARNING: Action Text can't locate your JavaScript bundle to add its package dependencies.
+
+ Add these lines to any bundles:
+
+ require("trix")
+ require("@rails/actiontext")
+
+ Alternatively, install and setup the webpacker gem then rerun `bin/rails action_text:install`
+ to have these dependencies added automatically.
+
+ WARNING
end
diff --git a/actiontext/package.json b/actiontext/package.json
index 6408d38765..594cfe16eb 100644
--- a/actiontext/package.json
+++ b/actiontext/package.json
@@ -1,6 +1,6 @@
{
"name": "@rails/actiontext",
- "version": "6.0.0-beta2",
+ "version": "6.1.0-alpha",
"description": "Edit and display rich text in Rails applications",
"main": "app/javascript/actiontext/index.js",
"files": [
diff --git a/actiontext/test/dummy/app/models/message.rb b/actiontext/test/dummy/app/models/message.rb
index 9ea4dbfe78..7bce50753c 100644
--- a/actiontext/test/dummy/app/models/message.rb
+++ b/actiontext/test/dummy/app/models/message.rb
@@ -1,4 +1,7 @@
class Message < ApplicationRecord
has_rich_text :content
has_rich_text :body
+
+ has_one :review
+ accepts_nested_attributes_for :review
end
diff --git a/actiontext/test/dummy/app/models/page.rb b/actiontext/test/dummy/app/models/page.rb
new file mode 100644
index 0000000000..dfebf282a7
--- /dev/null
+++ b/actiontext/test/dummy/app/models/page.rb
@@ -0,0 +1,4 @@
+class Page < ApplicationRecord
+ include ActionText::Attachable
+end
+
diff --git a/actiontext/test/dummy/app/models/review.rb b/actiontext/test/dummy/app/models/review.rb
new file mode 100644
index 0000000000..e54a37685d
--- /dev/null
+++ b/actiontext/test/dummy/app/models/review.rb
@@ -0,0 +1,5 @@
+class Review < ApplicationRecord
+ belongs_to :message
+
+ has_rich_text :content
+end
diff --git a/actiontext/test/dummy/config/locales/en.yml b/actiontext/test/dummy/config/locales/en.yml
index decc5a8573..cf9b342d0a 100644
--- a/actiontext/test/dummy/config/locales/en.yml
+++ b/actiontext/test/dummy/config/locales/en.yml
@@ -27,7 +27,7 @@
# 'true': 'foo'
#
# To learn more, please read the Rails Internationalization guide
-# available at http://guides.rubyonrails.org/i18n.html.
+# available at https://guides.rubyonrails.org/i18n.html.
en:
hello: "Hello world"
diff --git a/actiontext/test/dummy/config/routes.rb b/actiontext/test/dummy/config/routes.rb
index 30b05169b3..1fc667e242 100644
--- a/actiontext/test/dummy/config/routes.rb
+++ b/actiontext/test/dummy/config/routes.rb
@@ -1,4 +1,4 @@
Rails.application.routes.draw do
resources :messages
- # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
+ # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
diff --git a/actiontext/test/dummy/db/migrate/20190305172303_create_pages.rb b/actiontext/test/dummy/db/migrate/20190305172303_create_pages.rb
new file mode 100644
index 0000000000..3a71e55d94
--- /dev/null
+++ b/actiontext/test/dummy/db/migrate/20190305172303_create_pages.rb
@@ -0,0 +1,9 @@
+class CreatePages < ActiveRecord::Migration[6.0]
+ def change
+ create_table :pages do |t|
+ t.string :title
+
+ t.timestamps
+ end
+ end
+end
diff --git a/actiontext/test/dummy/db/migrate/20190317200724_create_reviews.rb b/actiontext/test/dummy/db/migrate/20190317200724_create_reviews.rb
new file mode 100644
index 0000000000..96e0eab287
--- /dev/null
+++ b/actiontext/test/dummy/db/migrate/20190317200724_create_reviews.rb
@@ -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
diff --git a/actiontext/test/dummy/db/schema.rb b/actiontext/test/dummy/db/schema.rb
index 2388986835..03e99b29d2 100644
--- a/actiontext/test/dummy/db/schema.rb
+++ b/actiontext/test/dummy/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2018_10_03_185713) 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
@@ -49,11 +49,23 @@ ActiveRecord::Schema.define(version: 2018_10_03_185713) do
t.datetime "updated_at", precision: 6, null: false
end
+ create_table "pages", force: :cascade do |t|
+ t.string "title"
+ t.datetime "created_at", precision: 6, null: false
+ t.datetime "updated_at", precision: 6, null: false
+ end
+
create_table "people", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
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
diff --git a/actiontext/test/test_helper.rb b/actiontext/test/test_helper.rb
index 196fba8c99..51a207f76a 100644
--- a/actiontext/test/test_helper.rb
+++ b/actiontext/test/test_helper.rb
@@ -31,3 +31,5 @@ class ActiveSupport::TestCase
ActiveStorage::Blob.create_after_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata
end
end
+
+require_relative "../../tools/test_common"
diff --git a/actiontext/test/unit/attachment_test.rb b/actiontext/test/unit/attachment_test.rb
index 65e9d69e84..026078dcec 100644
--- a/actiontext/test/unit/attachment_test.rb
+++ b/actiontext/test/unit/attachment_test.rb
@@ -32,7 +32,8 @@ class ActionText::AttachmentTest < ActiveSupport::TestCase
assert_equal attachable.byte_size, trix_attachment.attributes["filesize"]
assert_equal "Captioned", trix_attachment.attributes["caption"]
- assert_nil trix_attachment.attributes["content"]
+ assert_not_nil attachable.to_trix_content_attachment_partial_path
+ assert_not_nil trix_attachment.attributes["content"]
end
test "converts to TrixAttachment with content" do
@@ -49,6 +50,11 @@ class ActionText::AttachmentTest < ActiveSupport::TestCase
assert_not_nil trix_attachment.attributes["content"]
end
+ test "defaults trix partial to model partial" do
+ attachable = Page.create! title: "Homepage"
+ assert_equal "pages/page", attachable.to_trix_content_attachment_partial_path
+ end
+
private
def attachment_from_html(html)
ActionText::Content.new(html).attachments.first
diff --git a/actiontext/test/unit/model_test.rb b/actiontext/test/unit/model_test.rb
index d56363adc0..af53f88caa 100644
--- a/actiontext/test/unit/model_test.rb
+++ b/actiontext/test/unit/model_test.rb
@@ -49,8 +49,30 @@ 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
+
+ test "building content lazily on existing record" do
+ message = Message.create!(subject: "Greetings")
+
+ assert_no_difference -> { ActionText::RichText.count } do
+ assert_kind_of ActionText::RichText, message.content
+ end
+ end
end