Skip to content

Commit

Permalink
Merge pull request #4259 from alphagov/fix-content-store-response-type
Browse files Browse the repository at this point in the history
Fix content store response type
  • Loading branch information
richardTowers authored Oct 9, 2024
2 parents 8710711 + a99e5da commit 1cc9c15
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
24 changes: 14 additions & 10 deletions app/models/content_item.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
class ContentItem
attr_reader :content_store_response, :body, :image, :description, :document_type, :title, :base_path, :locale
attr_reader :content_store_response, :content_store_hash, :body, :image, :description, :document_type, :title, :base_path, :locale

def initialize(content_store_response)
# SCAFFOLDING: remove the override_content_store_hash parameter when full landing page
# content items including block details are available from content-store
def initialize(content_store_response, override_content_store_hash: nil)
@content_store_response = content_store_response
@body = content_store_response.dig("details", "body")
@image = content_store_response.dig("details", "image")
@description = content_store_response["description"]
@document_type = content_store_response["document_type"]
@title = content_store_response["title"]
@base_path = content_store_response["base_path"]
@locale = content_store_response["locale"]
@content_store_hash = override_content_store_hash || content_store_response.to_hash

@body = content_store_hash.dig("details", "body")
@image = content_store_hash.dig("details", "image")
@description = content_store_hash["description"]
@document_type = content_store_hash["document_type"]
@title = content_store_hash["title"]
@base_path = content_store_hash["base_path"]
@locale = content_store_hash["locale"]
end

delegate :to_h, to: :content_store_response
alias_method :to_h, :content_store_hash
delegate :cache_control, to: :content_store_response
end
4 changes: 2 additions & 2 deletions app/models/content_item_factory.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ContentItemFactory
def self.build(content_hash)
content_item_class(content_hash["document_type"]).new(content_hash)
def self.build(content_store_response)
content_item_class(content_store_response["document_type"]).new(content_store_response)
end

def self.content_item_class(document_type)
Expand Down
17 changes: 12 additions & 5 deletions app/models/landing_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ def initialize(content_store_response)
if content_store_response.dig("details", "blocks")
super(content_store_response)
else
super(content_store_response.deep_merge(load_additional_content(content_store_response["base_path"])))
super(
content_store_response,
override_content_store_hash: load_additional_content(content_store_response)
)
end

@blocks = (@content_store_response.dig("details", "blocks") || []).map { |block_hash| BlockFactory.build(block_hash) }
@blocks = (content_store_hash.dig("details", "blocks") || []).map { |block_hash| BlockFactory.build(block_hash) }
end

private

# SCAFFOLDING: can be removed (and reference above) when full content items
# including block details are available from content-store
def load_additional_content(base_path)
def load_additional_content(content_store_response)
base_path = content_store_response["base_path"]
file_slug = base_path.split("/").last.gsub("-", "_")
filename = Rails.root.join("#{ADDITIONAL_CONTENT_PATH}/#{file_slug}.yaml")
return { "details" => {} } unless File.exist?(filename)
content_hash = content_store_response.to_hash
return content_hash unless File.exist?(filename)

{ "details" => YAML.load(File.read(filename)) }
content_hash.deep_merge(
"details" => YAML.load_file(filename),
)
end
end
40 changes: 22 additions & 18 deletions spec/models/landing_page_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
RSpec.describe LandingPage do
let(:content_item) do
{
"base_path" => "/landing-page",
"title" => "Landing Page",
"description" => "A landing page example",
"locale" => "en",
"document_type" => "landing_page",
"schema_name" => "landing_page",
"publishing_app" => "whitehall",
"rendering_app" => "frontend",
"update_type" => "major",
"details" => {},
"routes" => [
{
"type" => "exact",
"path" => "/landing-page",
},
],
}
http_response = instance_double(RestClient::Response)
allow(http_response).to receive(:body).and_return(
{
"base_path" => "/landing-page",
"title" => "Landing Page",
"description" => "A landing page example",
"locale" => "en",
"document_type" => "landing_page",
"schema_name" => "landing_page",
"publishing_app" => "whitehall",
"rendering_app" => "frontend",
"update_type" => "major",
"details" => {},
"routes" => [
{
"type" => "exact",
"path" => "/landing-page",
},
],
}.to_json,
)
GdsApi::Response.new(http_response)
end

describe "#blocks" do
Expand Down

0 comments on commit 1cc9c15

Please sign in to comment.