-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor PreviewContent to be a class
- Loading branch information
Showing
5 changed files
with
77 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
lib/engines/content_block_manager/app/services/content_block_manager/preview_content.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 3 additions & 3 deletions
6
...ager/app/views/content_block_manager/content_block/editions/host_content/preview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters