From 879670983d01e1f6901feeaddf807e646e6973d1 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 21 Jul 2024 01:41:08 +0200 Subject: [PATCH] Make rich_text searchable and renderable in pdf --- app/helpers/pdf_helper.rb | 8 ++++++++ app/models/action_text/rich_text.rb | 7 +++++++ app/models/check.rb | 2 +- app/views/reports/show.pdf.prawn | 6 +++--- ...40720231941_add_body_text_to_action_text_rich_texts.rb | 5 +++++ db/schema.rb | 3 ++- 6 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 app/helpers/pdf_helper.rb create mode 100644 app/models/action_text/rich_text.rb create mode 100644 db/migrate/20240720231941_add_body_text_to_action_text_rich_texts.rb diff --git a/app/helpers/pdf_helper.rb b/app/helpers/pdf_helper.rb new file mode 100644 index 0000000..9ebed48 --- /dev/null +++ b/app/helpers/pdf_helper.rb @@ -0,0 +1,8 @@ +module PdfHelper + def prepare_rich_text(rich_text) + %w[div p del blockquote pre code].each do |tag| + rich_text = rich_text.to_s.gsub(%r{<#{tag}.*?>|}, '') + end + rich_text + end +end diff --git a/app/models/action_text/rich_text.rb b/app/models/action_text/rich_text.rb new file mode 100644 index 0000000..5be8526 --- /dev/null +++ b/app/models/action_text/rich_text.rb @@ -0,0 +1,7 @@ +module ActionText + class RichText < Record + before_save do + self.body_text = body.to_plain_text + end + end +end diff --git a/app/models/check.rb b/app/models/check.rb index 582b491..c22a9b1 100644 --- a/app/models/check.rb +++ b/app/models/check.rb @@ -6,6 +6,6 @@ class Check < ApplicationRecord has_rich_text :success_criterion_html scope :search, lambda { |term| - joins("INNER JOIN action_text_rich_texts ON action_text_rich_texts.record_id = checks.id AND record_type = 'Check'").where('checks.name LIKE :term OR action_text_rich_texts.body LIKE :term', term: "%#{term}%") + joins("INNER JOIN action_text_rich_texts ON action_text_rich_texts.record_id = checks.id AND record_type = 'Check'").where('checks.name LIKE :term OR action_text_rich_texts.body_text LIKE :term', term: "%#{term}%") } end diff --git a/app/views/reports/show.pdf.prawn b/app/views/reports/show.pdf.prawn index 4202127..120e0a0 100644 --- a/app/views/reports/show.pdf.prawn +++ b/app/views/reports/show.pdf.prawn @@ -3,12 +3,12 @@ prawn_document do |pdf| @report.elements.each do |element| pdf.text element.title pdf.text element.path - pdf.text element.description_html, inline_format: true + pdf.text prepare_rich_text(element.description_html), inline_format: true element.success_criteria.each do |success_criterion| pdf.text success_criterion.title - pdf.text success_criterion.description_html, inline_format: true - pdf.text success_criterion.comment_html, inline_format: true + pdf.text prepare_rich_text(success_criterion.description_html), inline_format: true + pdf.text prepare_rich_text(success_criterion.comment_html), inline_format: true end end end diff --git a/db/migrate/20240720231941_add_body_text_to_action_text_rich_texts.rb b/db/migrate/20240720231941_add_body_text_to_action_text_rich_texts.rb new file mode 100644 index 0000000..833d513 --- /dev/null +++ b/db/migrate/20240720231941_add_body_text_to_action_text_rich_texts.rb @@ -0,0 +1,5 @@ +class AddBodyTextToActionTextRichTexts < ActiveRecord::Migration[7.1] + def change + add_column :action_text_rich_texts, :body_text, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index a84b6ee..0fe2343 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_07_16_182333) do +ActiveRecord::Schema[7.1].define(version: 2024_07_20_231941) do create_table "action_text_rich_texts", force: :cascade do |t| t.string "name", null: false t.text "body" @@ -18,6 +18,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_16_182333) do t.bigint "record_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.text "body_text" t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true end