From 0fd8ba6ffff07a4556ec47840ef8c9dbd10e5e8b Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Tue, 31 Jul 2018 20:49:35 -0400 Subject: Prefer the ASt API --- app/models/action_text/rich_text.rb | 7 +++---- test/fixtures/files/racecar.jpg | Bin 0 -> 1124062 bytes test/test_helper.rb | 7 +++++++ test/unit/content_test.rb | 6 ++++++ 4 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/files/racecar.jpg diff --git a/app/models/action_text/rich_text.rb b/app/models/action_text/rich_text.rb index 6747def9a5..7c0fdf240d 100644 --- a/app/models/action_text/rich_text.rb +++ b/app/models/action_text/rich_text.rb @@ -2,15 +2,14 @@ class ActionText::RichText < ActiveRecord::Base self.table_name = "action_text_rich_texts" serialize :body, ActionText::Content - + belongs_to :record, polymorphic: true, touch: true has_many_attached :embeds - after_save do - self.embeds_blobs = body.attachments.map(&:attachable) + before_save do + self.embeds = body.attachments.map(&:attachable) end - def to_s body.to_s.html_safe end diff --git a/test/fixtures/files/racecar.jpg b/test/fixtures/files/racecar.jpg new file mode 100644 index 0000000000..934b4caa22 Binary files /dev/null and b/test/fixtures/files/racecar.jpg differ diff --git a/test/test_helper.rb b/test/test_helper.rb index 94fec6b8be..79fde51549 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -19,3 +19,10 @@ if ActiveSupport::TestCase.respond_to?(:fixture_path=) ActiveSupport::TestCase.file_fixture_path = ActiveSupport::TestCase.fixture_path + "/files" ActiveSupport::TestCase.fixtures :all end + +class ActiveSupport::TestCase + private + def create_file_blob(filename:, content_type:, metadata: nil) + ActiveStorage::Blob.create_after_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata + end +end diff --git a/test/unit/content_test.rb b/test/unit/content_test.rb index b1a06e2cfa..f1acf054db 100644 --- a/test/unit/content_test.rb +++ b/test/unit/content_test.rb @@ -6,5 +6,11 @@ module ActionText message = Message.new(subject: "Greetings", content: "

Hello world

") assert_equal "Hello world", message.content.body.to_plain_text 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)) + assert_equal "racecar.jpg", message.content.embeds.first.filename.to_s + end end end -- cgit v1.2.3