aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJavan Makhmali <javan@javan.us>2018-10-04 08:00:38 -0400
committerJavan Makhmali <javan@javan.us>2018-10-04 08:00:38 -0400
commitd43d7f197005a10349ee08e482fffc94a772cf72 (patch)
tree955e09ca7a50907d2b213955274a3d8389ec4654
parentb4c71643af11cb94c25d7aa994090579dd43f943 (diff)
downloadrails-d43d7f197005a10349ee08e482fffc94a772cf72.tar.gz
rails-d43d7f197005a10349ee08e482fffc94a772cf72.tar.bz2
rails-d43d7f197005a10349ee08e482fffc94a772cf72.zip
Delegate string methods to content
Closes #11
-rw-r--r--app/models/action_text/rich_text.rb8
-rw-r--r--lib/action_text/attribute.rb2
-rw-r--r--test/unit/model_test.rb14
3 files changed, 15 insertions, 9 deletions
diff --git a/app/models/action_text/rich_text.rb b/app/models/action_text/rich_text.rb
index 94cf595fac..3cedef4d5f 100644
--- a/app/models/action_text/rich_text.rb
+++ b/app/models/action_text/rich_text.rb
@@ -1,11 +1,13 @@
# The RichText record holds the content produced by the Trix editor in a serialized `body` attribute.
# It also holds all the references to the embedded files, which are stored using Active Storage.
-# This record is then associated with the Active Record model the application desires to have
+# This record is then associated with the Active Record model the application desires to have
# rich text content using the `has_rich_text` class method.
class ActionText::RichText < ActiveRecord::Base
self.table_name = "action_text_rich_texts"
serialize :body, ActionText::Content
+ delegate :to_s, :to_plain_text, :nil?, to: :body
+ delegate :blank?, :empty?, :present?, to: :to_s
belongs_to :record, polymorphic: true, touch: true
has_many_attached :embeds
@@ -13,8 +15,4 @@ class ActionText::RichText < ActiveRecord::Base
before_save do
self.embeds = body.attachments.map(&:attachable) if body.present?
end
-
- def to_s
- body.to_s.html_safe
- end
end
diff --git a/lib/action_text/attribute.rb b/lib/action_text/attribute.rb
index 8426726a38..8439073c00 100644
--- a/lib/action_text/attribute.rb
+++ b/lib/action_text/attribute.rb
@@ -12,7 +12,7 @@ module ActionText
#
# message = Message.create!(content: "<h1>Funny times!</h1>")
# message.content.to_s # => "<h1>Funny times!</h1>"
- # message.content.body.to_plain_text # => "Funny times!"
+ # message.content.to_plain_text # => "Funny times!"
#
# The dependent RichText model will also automatically process attachments links as sent via the Trix-powered editor.
# These attachments are associated with the RichText model using Active Storage.
diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb
index bdfe88adc8..a9a49cbffd 100644
--- a/test/unit/model_test.rb
+++ b/test/unit/model_test.rb
@@ -1,14 +1,22 @@
require 'test_helper'
class ActionText::ModelTest < ActiveSupport::TestCase
+ test "html conversion" do
+ message = Message.new(subject: "Greetings", content: "<h1>Hello world</h1>")
+ assert_equal %Q(<div class="trix-content">\n <h1>Hello world</h1>\n</div>\n), "#{message.content}"
+ end
+
test "plain text conversion" do
message = Message.new(subject: "Greetings", content: "<h1>Hello world</h1>")
- assert_equal "Hello world", message.content.body.to_plain_text
+ assert_equal "Hello world", message.content.to_plain_text
end
test "without content" do
message = Message.create!(subject: "Greetings")
- assert message.content.body.nil?
+ assert message.content.nil?
+ assert message.content.blank?
+ assert message.content.empty?
+ assert_not message.content.present?
end
test "embed extraction" do
@@ -19,6 +27,6 @@ class ActionText::ModelTest < ActiveSupport::TestCase
test "saving content" do
message = Message.create!(subject: "Greetings", content: "<h1>Hello world</h1>")
- assert_equal "Hello world", message.content.body.to_plain_text
+ assert_equal "Hello world", message.content.to_plain_text
end
end