Skip to content

Commit

Permalink
refactor PreviewContent to be a class
Browse files Browse the repository at this point in the history
  • Loading branch information
Harriethw committed Nov 25, 2024
1 parent bab3030 commit 5a7cd8f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds govuk-body govuk-!-margin-bottom-0">
<p><strong>Document title: </strong><%= @preview_content[:title] %></p>
<p><strong>Document title: </strong><%= @preview_content.title %></p>
</div>
</div>
<hr class="govuk-section-break govuk-!-margin-bottom-8 govuk-section-break--visible">
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<iframe id="preview" style="width:100%;height:80vh;" srcdoc="<%= @preview_content[:html] %>"></iframe>
<iframe id="preview" style="width:100%;height:80vh;" srcdoc="<%= @preview_content.html %>"></iframe>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down Expand Up @@ -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

0 comments on commit 5a7cd8f

Please sign in to comment.