From 5a7cd8f495b71707438d0a08b0477a01e0841e73 Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Mon, 25 Nov 2024 14:37:53 +0000 Subject: [PATCH] refactor PreviewContent to be a class --- .../editions/host_content_controller.rb | 2 +- .../get_preview_content.rb | 70 ------------------- .../content_block_manager/preview_content.rb | 67 ++++++++++++++++++ .../editions/host_content/preview.html.erb | 6 +- ...ontent_test.rb => preview_content_test.rb} | 12 ++-- 5 files changed, 77 insertions(+), 80 deletions(-) delete mode 100644 lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb create mode 100644 lib/engines/content_block_manager/app/services/content_block_manager/preview_content.rb rename lib/engines/content_block_manager/test/unit/app/services/{get_preview_content_test.rb => preview_content_test.rb} (84%) diff --git a/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb b/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb index bf628a8b20d..fb104b315f8 100644 --- a/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb +++ b/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb @@ -2,6 +2,6 @@ class ContentBlockManager::ContentBlock::Editions::HostContentController < Conte def preview host_content_id = params[:host_content_id] content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id]) - @preview_content = ContentBlockManager::GetPreviewContent.new(content_id: host_content_id, content_block_edition:).preview_content + @preview_content = ContentBlockManager::PreviewContent.for_content_id(content_id: host_content_id, content_block_edition:) end end diff --git a/lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb b/lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb deleted file mode 100644 index f43dcd83982..00000000000 --- a/lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb +++ /dev/null @@ -1,70 +0,0 @@ -require "net/http" -require "json" -require "uri" - -module ContentBlockManager - class GetPreviewContent - def initialize(content_id:, content_block_edition:) - @content_id = content_id - @content_block_edition = content_block_edition - end - - def preview_content - { - title: content_item["title"], - html:, - } - end - - private - - def html - @html ||= preview_html - end - - def content_item - @content_item ||= begin - response = Services.publishing_api.get_content(@content_id) - response.parsed_content - end - end - - def frontend_base_path - Rails.env.development? ? Plek.external_url_for("government-frontend") : Plek.website_root - end - - def frontend_path - frontend_base_path + content_item["base_path"] - end - - def preview_html - uri = URI(frontend_path) - nokogiri_html = Nokogiri::HTML.parse(Net::HTTP.get(uri)) - replace_existing_content_blocks(nokogiri_html) - end - - def replace_existing_content_blocks(nokogiri_html) - replace_blocks(nokogiri_html) - style_blocks(nokogiri_html) - nokogiri_html - end - - def replace_blocks(nokogiri_html) - content_block_spans(nokogiri_html).each do |span| - span.replace @content_block_edition.render - end - end - - BLOCK_STYLE = "background-color: yellow;".freeze - - def style_blocks(nokogiri_html) - content_block_spans(nokogiri_html).each do |span| - span["style"] = "background-color: yellow;" - end - end - - def content_block_spans(nokogiri_html) - nokogiri_html.css("span[data-content-id=\"#{@content_block_edition.document.content_id}\"]") - end - end -end diff --git a/lib/engines/content_block_manager/app/services/content_block_manager/preview_content.rb b/lib/engines/content_block_manager/app/services/content_block_manager/preview_content.rb new file mode 100644 index 00000000000..e4c51ba3502 --- /dev/null +++ b/lib/engines/content_block_manager/app/services/content_block_manager/preview_content.rb @@ -0,0 +1,67 @@ +require "net/http" +require "json" +require "uri" + +module ContentBlockManager + class PreviewContent < Data.define(:title, :html) + class << self + def for_content_id(content_id:, content_block_edition:) + @content_id = content_id + @content_block_edition = content_block_edition + PreviewContent.new(title: content_item["title"], html:) + end + + private + + def html + @html ||= preview_html + end + + def content_item + @content_item ||= begin + response = Services.publishing_api.get_content(@content_id) + response.parsed_content + end + end + + def frontend_base_path + Rails.env.development? ? Plek.external_url_for("government-frontend") : Plek.website_root + end + + def frontend_path + frontend_base_path + content_item["base_path"] + end + + def preview_html + uri = URI(frontend_path) + nokogiri_html = Nokogiri::HTML.parse(Net::HTTP.get(uri)) + replace_existing_content_blocks(nokogiri_html) + end + + def replace_existing_content_blocks(nokogiri_html) + replace_blocks(nokogiri_html) + style_blocks(nokogiri_html) + nokogiri_html + end + + def replace_blocks(nokogiri_html) + @preview_content_block_render ||= @content_block_edition.render + content_block_spans(nokogiri_html).each do |span| + span.replace @preview_content_block_render + end + end + + BLOCK_STYLE = "background-color: yellow;".freeze + + def style_blocks(nokogiri_html) + content_block_spans(nokogiri_html).each do |span| + span["style"] = BLOCK_STYLE + end + end + + def content_block_spans(nokogiri_html) + nokogiri_html.css("span[data-content-id=\"#{@content_block_edition.document.content_id}\"]") + end + end + end +end diff --git a/lib/engines/content_block_manager/app/views/content_block_manager/content_block/editions/host_content/preview.html.erb b/lib/engines/content_block_manager/app/views/content_block_manager/content_block/editions/host_content/preview.html.erb index 57639866e5b..5126c7bb2ae 100644 --- a/lib/engines/content_block_manager/app/views/content_block_manager/content_block/editions/host_content/preview.html.erb +++ b/lib/engines/content_block_manager/app/views/content_block_manager/content_block/editions/host_content/preview.html.erb @@ -1,16 +1,16 @@ <% content_for :page_title, "Preview content block in host document" %> <% content_for :context, "Preview content block" %> -<% content_for :title, @preview_content[:title] %> +<% content_for :title, @preview_content.title %> <% content_for :title_margin_bottom, 0 %>
-

Document title: <%= @preview_content[:title] %>

+

Document title: <%= @preview_content.title %>


- +
diff --git a/lib/engines/content_block_manager/test/unit/app/services/get_preview_content_test.rb b/lib/engines/content_block_manager/test/unit/app/services/preview_content_test.rb similarity index 84% rename from lib/engines/content_block_manager/test/unit/app/services/get_preview_content_test.rb rename to lib/engines/content_block_manager/test/unit/app/services/preview_content_test.rb index 56cde5d78bd..d3a4cfc2b9e 100644 --- a/lib/engines/content_block_manager/test/unit/app/services/get_preview_content_test.rb +++ b/lib/engines/content_block_manager/test/unit/app/services/preview_content_test.rb @@ -1,9 +1,9 @@ require "test_helper" -class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase +class ContentBlockManager::PreviewContentTest < ActiveSupport::TestCase extend Minitest::Spec::DSL - let(:described_class) { ContentBlockManager::GetPreviewContent } + let(:described_class) { ContentBlockManager::PreviewContent } let(:host_content_id) { SecureRandom.uuid } let(:preview_content_id) { SecureRandom.uuid } let(:host_title) { "Test" } @@ -42,13 +42,13 @@ class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase html: Nokogiri::HTML.parse(expected_html), } - actual_content = ContentBlockManager::GetPreviewContent.new( + actual_content = ContentBlockManager::PreviewContent.for_content_id( content_id: host_content_id, content_block_edition: block_to_preview, - ).preview_content + ) - assert_equal expected_content[:title], actual_content[:title] - assert_equal expected_content[:html].to_s, actual_content[:html].to_s + assert_equal expected_content[:title], actual_content.title + assert_equal expected_content[:html].to_s, actual_content.html.to_s end end end